1013ms G++代码 #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<cmath> using namespace std; const int NUM = 100010; typedef struct node { int left,mid,right,key; }node; node T[ NUM<<2 ]; int flag[ NUM<<2 ]; int a[NUM]; void create(int u,int l,int r) { T[u].left = l; T[u].right = r; T[u].mid = (l+r)>>1; flag[ u ] = 0; if(l == r) { T[u].key = 1; return; } int mid = (l+r)>>1; create(u+u,l,mid); create(u+u+1,mid+1,r); T[u].key = T[u+u].key+T[u+u+1].key; } void PushDown(int u,int m)//把当前结点的信息更新给儿子结点 { if (flag[u] !=0 ) { flag[u+u] = flag[u]; flag[u+u+1] = flag[u]; T[u+u].key = flag[u] * (m - (m >> 1)); T[u+u+1].key = flag[u] * (m >> 1); flag[u] = 0; } } void change(int from,int to,int newKey ,int i)//延迟更新 { if(from <= T[i].left && to >= T[i].right) { T[i].key = newKey*(T[i].right - T[i].left +1); flag[i] = newKey; return ; } PushDown(i,T[i].right - T[i].left + 1); if(from <= T[i].mid) change( from , to ,newKey, i+i ); if(to > T[i].mid) change( from , to ,newKey, i+i+1 ); T[i].key = T[i+i].key + T[i+i+1].key; } int main() { int n,t,q,x,y,ca=1,nn,z; while(scanf("%d",&t) !=EOF) { while(t--) { scanf("%d",&n); scanf("%d",&q); create(1,1,n); for(int i = 1; i <= q; i++) { scanf("%d %d %d",&x,&y,&z); change(x,y,z,1); } printf("Case %d: The total value of the hook is %d.\n",ca++,T[1].key); } } return 0; }
本文出自 “Qero” 博客,请务必保留此出处http://8590696.blog.51cto.com/8580696/1358901
原文:http://8590696.blog.51cto.com/8580696/1358901