Hihocoder 1080 segment Tree (interval update)

Source: Internet
Author: User

Topic Link: http://hihocoder.com/problemset/problem/1080, two operation segment tree (interval update).

This problem a period of time has been stuck to me, then also the foundation is not solid do not come out, today again think of actually still relatively simple, also can only blame oneself too weak.

 This is a pit on the pit. There are two operations: set and add, so the lazy tag array requires two, but one thing to consider is the sequence of set and add on an interval--if there is simultaneous presence of an interval set and an add tag, Then it should be handled if: a case set before add, then in the normal order to be able to, and the other case add before set, then the add operation is canceled, the direct set operation is OK.

So when updating an interval of a segment tree, if it is a set operation, the tag of the add is cleaned out, and if it is an add operation, it is done normally. This ensures that the two tags appear at the same time as the set and then add, so that there will be no error.

#include <iostream>#include<cstdio>#include<vector>#include<cmath>#include<string>#include<string.h>#include<algorithm>using namespacestd;#defineLL __int64#defineEPS 1e-8#defineINF Int_max#defineLson L, M, RT << 1#defineRson m + 1, R, RT << 1 | 1Const intMOD =10000007; Const intMAXN =100000+5;Const intN =100000+5;intSUM[MAXN <<2], COL_ADD[MAXN <<2], COL_SET[MAXN <<2];voidPushup (intRT) {Sum[rt]= Sum[rt <<1] + sum[rt <<1|1];}voidBuildintLintRintRT) {    if(L = =r) {scanf ("%d", &Sum[rt]); return; }    intm = (L + r) >>1;    Build (Lson);    Build (Rson); Pushup (RT);}voidPushdown (intRtintLen) {            //in the Pushdown () function, the set operation is performed before the add operation.    if(Col_set[rt]) {col_set[rt<<1] = Col_set[rt <<1|1] =Col_set[rt]; Col_add[rt<<1] = Col_add[rt <<1|1] =0;//also clear out the add mark, then less this sentence debug for a long timeSum[rt <<1] = (Len-(Len >>1)) *Col_set[rt]; Sum[rt<<1|1] = (Len >>1) *Col_set[rt]; COL_SET[RT]=0; }    if(Col_add[rt]! =0) {Col_add[rt<<1] +=Col_add[rt]; Col_add[rt<<1|1] +=Col_add[rt]; Sum[rt<<1] + = (len-(Len >>1)) *Col_add[rt]; Sum[rt<<1|1] + = (len >>1) *Col_add[rt]; COL_ADD[RT]=0; } }voidUpdateintLintRintXintChintLintRintRT) {    if(l <= l && R >=r) {if(CH) {SUM[RT]= (R-l +1) *x; COL_SET[RT]=x; COL_ADD[RT]=0;//clear the Add tag}Else{Sum[rt]+ = (r-l +1) *x; COL_ADD[RT]+=x; }        return; } pushdown (RT, R-L +1); intm = (L + r) >>1; if(L >m) Update (L, R, x, CH, rson); Else if(R <=m) Update (L, R, x, CH, lson); Else{update (L, R, x, CH, lson);    Update (L, R, x, CH, rson); } pushup (RT);}intMain () {intN, M, A, B, C, ch; scanf ("%d%d", &n, &m); Build (1, n +1,1);  while(m--) {scanf ("%d %d%d%d", &ch, &a, &b, &c); Update (a+1, B +1, C, CH,1, n +1,1); printf ("%d\n", sum[1]); }    return 0;}

Hihocoder 1080 segment Tree (interval update)

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.