hdu 1698 线段树(修改线段值)

http://acm.hdu.edu.cn/showproblem.php?pid=1698

/*修改函数Update():在修改区间的时候,如果正好和区间对应,则改变当前点的value值,如果不对应, 则要修改的区间必是当前区间的子区间,再当前区间的两个子区间修改就行了, 关键点是:先将当前区间value值赋给它的两个子区间,同时当前区间的value值要赋零,表示当前区间里的保存的值不是同一个值。 */ #include<iostream> using namespace std; struct node { int left,right; int val,num; }a[300000]; void creat(int s,int t,int step) { a[step].left=s; a[step].right=t; a[step].num=s-t+1; if(a[step].left==a[step].right) { a[step].val=1;return;} int mid=(s+t)/2; a[step].val=0; creat(s,mid,2*step); creat(mid+1,t,2*step+1); } /*TLE!!! int update(int step,int s,int t,int v)//更新线段!!! { if(a[step].left==a[step].right) return a[step].val=v; int mid=(a[step].left+a[step].right)/2; if(mid<s)//更新右子树 return a[step].val=update(2*step+1,s,t,v) + a[2*step].val; else if(mid>=t)//更新左子树 return a[step].val=update(2*step,s,t,v) + a[2*step+1].val; else return a[step].val=update(2*step,s,t,v)+update(2*step+1,s,t,v); } */ void update(int s,int t,int step,int value) { if(s<=a[step].left && a[step].right<=t) { a[step].val=value; a[step].num=(a[step].right-a[step].left+1)*value; return ; } else { if(a[step].val>0) { a[2*step].val=a[step].val; a[2*step].num=(a[2*step].right-a[2*step].left+1)*a[2*step].val; a[2*step+1].val=a[step].val; a[2*step+1].num=(a[2*step+1].right-a[2*step+1].left+1)*a[2*step+1].val; a[step].val=0; } if(s<=a[2*step].right) update(s,t,2*step,value); if(t>=a[2*step+1].left) update(s,t,2*step+1,value); a[step].num=a[2*step].num+a[2*step+1].num; } } int main() { //freopen("a.txt","r",stdin); int ca; int k; scanf("%d",&ca); for(k=1;k<=ca;k++) { int n,m; scanf("%d%d",&n,&m); int i; creat(1,n,1); for(i=1;i<=m;i++) { int x,y,z; scanf("%d%d%d",&x,&y,&z); update(x,y,1,z); } printf("Case %d: The total value of the hook is %d./n",k,a[1].num); } return 0; }

你可能感兴趣的:(hook)