UV 12436 Rip Van Winkle's code (Interval Update, interval query)

Source: Internet
Author: User

For an interval of 250000, you are given four operations: Operation A, number from St to Ed, plus 1, 2 ,..., st-ed + 1. Operation B, the number in the range from St to Ed, plus, St-ed + 1, St-ed,..., 1, respectively. In operation C, assign the number from St to Ed to X. Operation S: queries the sum of the numbers in the range from St to Ed.

Because operation a and operation B are both operations in fact all of the same series, you can consider them together. Or line segment tree. For Operation AB, the node of the online segment tree records the value Add1 to be added to the left endpoint and the value Add2 to be added to the right endpoint, at the same time, the tolerances in this section are recorded (they are marked by the lazy operation of the Operation AB ). For Operation C, a flag is used in the node of the online segment tree to indicate whether all the numbers in the current interval are the same, in addition, a valu is used to indicate the value of this number when the number in the interval is exactly the same, that is, when the flag is 1 (all are the tags of the lazy operation that operates C ).

It is easy to get. The addition of the arithmetic difference sequence or the arithmetic difference sequence can be used to perform multiple AB operations on a range. For the C operation, because the values in the range are forcibly assigned to X, all the previous AB operations are invalid. That is, several variables that represent the AB operation should be taken at this time, all values of ADD1, Add2, and step are 0. When passing the record value to the subinterval, the mark of the C operation should be given priority, because when the mark of the Operation AB and operation C both exist, it must have occurred that the C operation was performed first, and then the AB operation was performed. If at this time, the AB mark is first passed down, and then the C mark is passed, the result will be incorrect.

Wa has made two mistakes for a long time:

1. at the beginning, only ADD1 and Add2 are recorded, and then the value of the lazy operation is passed to the subinterval, that is, when ADD1 and Add2 are passed, they are divided into two intervals and set to (Add1, mid1), (mid2, Add2), get mid1 directly through (Add1 + Add2)/2, and then add or subtract one by one based on whether the value is increment or decrease, in fact, the tolerances should be added or subtracted here, which is also why the tolerances step is added to the node of the online segment tree.

2. the mark of Operation C is only valu but not flag. It is incorrectly considered that as long as the Valu value is not zero, there is a value to be passed down. In fact, operation C can assign all intervals to zero. At this time, valu is equal to zero and will not be passed down, resulting in an error. Therefore, an additional flag is added.

PS: In some OJ, * 2 is a little slow, and <1 is ..

#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int N=250005;typedef long long LL;struct node{    bool flag;int left,right;LL sum,add1,add2,valu,step;LL mid(){return left+(right-left)/2;}LL len(){return right-left+1;}void changeAB(LL a,LL b,LL k){add1+=a;    add2+=b;    step+=k;sum+=(a+b)*len()/2;}void changeC(LL a){    flag=1;     valu=a;add1=0;     add2=0;     step=0;        sum=valu*len();}};struct Segtree{node tree[N*4];void down(int ind){if(tree[ind].flag){tree[ind*2].changeC(tree[ind].valu);tree[ind*2+1].changeC(tree[ind].valu);tree[ind].flag=0;}if(tree[ind].add1||tree[ind].add2||tree[ind].step){    LL add1=tree[ind].add1,add2=tree[ind].add2;    LL k=tree[ind].step;    LL mid=add1+k*(tree[ind*2].len()-1);tree[ind*2].changeAB(add1,mid,k);tree[ind*2+1].changeAB(mid+k,add2,k);tree[ind].add1=0;tree[ind].add2=0;tree[ind].step=0;}}void build(LL left,LL right,LL ind){tree[ind].left=left;tree[ind].right=right;tree[ind].add1=0;tree[ind].add2=0;tree[ind].sum=0;tree[ind].valu=0;tree[ind].step=0;if(left!=right){LL mid=tree[ind].mid();build(left,mid,ind*2);build(mid+1,right,ind*2+1);}}void updataAB(LL be,LL end,LL ind,LL step){LL left=tree[ind].left,right=tree[ind].right;if(be<=left&&right<=end){    LL st,ed;    if(step>=0) {st=left-be+1;ed=right-be+1;}    else {st=end-left+1;ed=end-right+1;}    tree[ind].changeAB(st,ed,step);}else{down(ind);LL mid=tree[ind].mid();if(be<=mid) updataAB(be,end,ind*2,step);if(end>mid) updataAB(be,end,ind*2+1,step);tree[ind].sum=tree[ind*2].sum+tree[ind*2+1].sum;}}void updataC(LL be,LL end,LL ind,LL valu){LL left=tree[ind].left,right=tree[ind].right;if(be<=left&&right<=end) tree[ind].changeC(valu);else{down(ind);LL mid=tree[ind].mid();if(be<=mid) updataC(be,end,ind*2,valu);if(end>mid) updataC(be,end,ind*2+1,valu);tree[ind].sum=tree[ind*2].sum+tree[ind*2+1].sum;}}LL query(LL be,LL end,LL ind){LL left=tree[ind].left,right=tree[ind].right;if(be<=left&&right<=end) return tree[ind].sum;else{down(ind);LL mid=tree[ind].mid();LL sum1=0,sum2=0;if(be<=mid) sum1=query(be,end,ind*2);if(end>mid) sum2=query(be,end,ind*2+1);return sum1+sum2;}}}seg;int main(){    int n;    while(scanf("%d",&n)!=EOF)    {seg.build(1,N-5,1);for(int i=0;i<n;i++){char str[10];LL a,b,c;scanf("%s",str);if(str[0]=='A'){scanf("%lld%lld",&a,&b);seg.updataAB(a,b,1,1);}else if(str[0]=='B'){scanf("%lld%lld",&a,&b);seg.updataAB(a,b,1,-1);}else if(str[0]=='C'){scanf("%lld%lld%lld",&a,&b,&c);seg.updataC(a,b,1,c);}else{scanf("%lld%lld",&a,&b);printf("%lld\n",seg.query(a,b,1));}}}return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.