Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 20406 Accepted Submission(s): 10221
1 10 2 1 5 2 5 9 3
Case 1: The total value of the hook is 24.
#include <cstdio> #include <algorithm> #define INF 0x7f7f7f7f #define Max 600000 #include <cstdlib> using namespace std; int c[Max]; int setv[Max], sumv[Max]; int _sum; int a, b, v;//记录查询区间 修改值 bool flagv[Max];//标记 void maintain(int o, int L, int R)//维护 { if (flagv[o]) { sumv[o] = (R-L+1)*setv[o]; } else if (R > L) { sumv[o] = sumv[o*2] + sumv[o*2+1]; } } void build(int o, int L, int R)//建树 { if (L == R) { //scanf("%lld", c+o);//输入n个数 sumv[o] = setv[o] = c[o] = 1; } else { int M = L + (R-L)/2; build(o<<1, L, M); build(o*2+1, M+1, R); maintain(o, L, R); } flagv[o] = false;//置0 } inline void pushdown(int o) { if (flagv[o]) { setv[o<<1] = setv[o<<1|1] = setv[o]; flagv[o<<1] = flagv[o<<1|1] = true; flagv[o] = false; } } void update(int o, int L, int R)//更新 { if (a <= L && b >= R) { setv[o] = v; flagv[o] = true; } else { int M = L + (R-L)/2; pushdown(o); if (a <= M) update(o<<1, L, M); else maintain(o<<1, L, M); if (b > M) update(o<<1|1, M+1, R); else maintain(o<<1|1, M+1, R); } maintain(o, L, R); } void query(int o, int L, int R) //查询 { if (flagv[o]) { _sum += setv[o]*(min(b, R) - max(a, L) + 1); } else if (a <= L && b >= R) { _sum += sumv[o]; } else { int M = L + (R-L)/2; if (a <= M) query(o<<1, L, M); if (b > M) query(o<<1|1, M+1, R); } } int main() { int t, n, m, j=1; scanf("%d",&t); while(t--) { scanf("%d", &n); build(1, 1, n); scanf("%d", &m); while(m--) { scanf("%d%d%d", &a, &b, &v); update(1, 1, n); } _sum=0; a = 1; b = n; query(1, 1, n); printf("Case %d: The total value of the hook is %d.\n", j++, _sum); } return 0; }
#include <cstdio> #define MAX 100000+10 int col[MAX<<2]; int sum[MAX<<2]; void PushUp(int o) { sum[o] = sum[o<<1] + sum[o<<1|1]; } void PushDown(int o, int m) { if (col[o]) { col[o<<1] = col[o<<1|1] = col[o]; sum[o<<1] = (m - (m >> 1)) * col[o]; sum[o<<1|1] = (m >> 1) * col[o]; col[o] = 0; } } void build(int o, int l, int r)//建树 { col[o] = 0; sum[o] = 1; if (l == r) return ; int mid = (l + r) >> 1; build(o<<1,l, mid); build(o<<1|1, mid+1, r); PushUp(o); } void update(int o, int l, int r, int L, int R, int v)//更新 { if (L <= l && r <= R) { col[o] = v; sum[o] = v * (r - l + 1); return ; } PushDown(o , r - l + 1); int mid = (l + r) >> 1; if (L <= mid) update(o<<1, l, mid, L , R , v); if (R > mid) update(o<<1|1, mid+1, r, L , R , v); PushUp(o); } int query(int o, int l, int r, int L, int R) //查询 { if(L <= l && R >= r) { return sum[o]; } PushDown(o, r-l+1); int mid = (r+l) >> 1; int res = 0; if(L <= mid) res += query(o<<1, l, mid, L, R); if(R > mid) res += query(o<<1|1, mid+1 , r, L, R); return res; } int main() { int t , n , m, j=1; int a, b, v; scanf("%d", &t); while(t--) { scanf("%d%d", &n, &m); build(1 , 1 , n); while (m --) { scanf("%d%d%d", &a, &b, &v); update(1 , 1 , n, a, b, v); } printf("Case %d: The total value of the hook is %d.\n",j++ , query(1, 1, n, 1, n)); } return 0; }
#include<cstdio> #include<map> #define N 100001 using namespace std; map<int,int>m; map<int,int>::iterator it,jt; int a[N],x[N],y[N],num[N]; int main() { int i,t,tt=0,n,q,ans; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&q); m.clear(); for(i=1;i<=n;i++) m[i]=1; for(i=0;i<q;i++) scanf("%d%d%d",x+i,y+i,num+i); for(i=0;i<=n;i++) a[i]=1; for(i=q-1;i>=0;i--) for(it=m.lower_bound(x[i]);it!=m.end();) if(it->first<=y[i]) { a[it->first]=num[i]; jt=it++; m.erase(jt); } else break; ans=0; for(i=1;i<=n;i++) ans+=a[i]; printf("Case %d: The total value of the hook is %d.n",++tt,ans); } return 0; }