标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 23450 Accepted Submission(s): 11742
1 #include<stdio.h> 2 const int MAXN=100010; 3 struct Node{ 4 int l,r; 5 int sum,lazy,val;//val不能全局变量。。。 6 }; 7 Node tree[MAXN<<2]; 8 #define NOW tree[root].sum=tree[root<<1].sum+tree[root<<1|1].sum 9 #define lson root<<1,tree[root].l,mid 10 #define rson root<<1|1,mid+1,tree[root].r 11 void build(int root,int l,int r){ 12 tree[root].l=l; 13 tree[root].r=r; 14 tree[root].lazy=0; 15 tree[root].val=0; 16 if(l==r)tree[root].sum=1; 17 else{ 18 int mid=(l+r)>>1; 19 build(lson); 20 build(rson); 21 NOW; 22 } 23 } 24 void update(int root,int l,int r,int v){ 25 if(l==tree[root].l&&r==tree[root].r){ 26 tree[root].lazy=1; 27 tree[root].val=v; 28 tree[root].sum=(r-l+1)*v; 29 } 30 else{ 31 int mid=(tree[root].l+tree[root].r)>>1; 32 if(tree[root].lazy==1){ 33 tree[root].lazy=0; 34 update(lson,tree[root].val); 35 update(rson,tree[root].val); 36 tree[root].val=0; 37 } 38 if(r<=mid)update(root<<1,l,r,v); 39 else if(l>mid)update(root<<1|1,l,r,v); 40 else{//这个不能少了,代表l,r在tree的两个孩子节点内,也就是找到了l,r的区间; 41 update(root<<1,l,mid,v); 42 update(root<<1|1,mid+1,r,v); 43 } 44 NOW; 45 } 46 } 47 int main(){ 48 int T,N,Q,flot=0; 49 scanf("%d",&T); 50 while(T--){ 51 scanf("%d",&N); 52 scanf("%d",&Q); 53 build(1,1,N); 54 while(Q--){ 55 int x,y,z; 56 scanf("%d%d%d",&x,&y,&z); 57 update(1,x,y,z); 58 } 59 printf("Case %d: The total value of the hook is %d.\n",++flot,tree[1].sum); 60 } 61 return 0; 62 }
标签:
原文地址:http://www.cnblogs.com/handsomecui/p/4814792.html