标签:
1 10 2 1 5 2 5 9 3
Case 1: The total value of the hook is 24.
//795MS 6224K #include<stdio.h> #include<algorithm> #include<string.h> #define M 100007 using namespace std; struct node { int l,r,mid,val,type; }tree[M<<2]; int s[M]; void build(int left,int right,int i)//建树 { tree[i].l=left;tree[i].r=right; tree[i].mid=(left+right)>>1;tree[i].type=0; if(left==right){tree[i].val=1; return;} build(left,tree[i].mid,i*2); build(tree[i].mid+1,right,i*2+1); tree[i].val=tree[i*2].val+tree[i*2+1].val; } void update(int left,int right,int val,int i) { if(tree[i].l>=left&&tree[i].r<=right){tree[i].val=(right-left+1)*val;tree[i].type=val; return;} else { if(tree[i].type) { tree[i*2].type=tree[i].type; tree[i*2].val=(tree[i*2].r-tree[i*2].l+1)*tree[i].type; tree[i*2+1].type=tree[i].type; tree[i*2+1].val=(tree[i*2+1].r-tree[i*2+1].l+1)*tree[i].type; tree[i].type=0; } if(tree[i].mid<left)update(left,right,val,2*i+1); else if(tree[i].mid>=right)update(left,right,val,2*i); else { update(left,tree[i].mid,val,2*i); update(tree[i].mid+1,right,val,2*i+1); } tree[i].val=tree[i*2].val+tree[i*2+1].val; } } int main() { int n,m,t,cas=1; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); build(1,n,1); int a,b; int c; while(m--) { scanf("%d%d%d",&a,&b,&c); update(a,b,c,1); } printf("Case %d: The total value of the hook is %d.\n",cas++,tree[1].val); } }
HDU 1689 Just a Hook 线段树区间更新求和
标签:
原文地址:http://blog.csdn.net/crescent__moon/article/details/43538709