--- range segment tree modification

First interval range of value-added and changed values ​​very different.

Range of value-added

Ideas:

Introducing a lazy flag for each node, such as in the [1,8] interval, updated [3,5], simply updating the node 5 ([3,4]), the node 12 ([5]), labeled and updates, as well as their parent update. When a query to a labeled zone, just decentralization if it fully contained query section mark, the updated value of this node, and then quit (because lazy Well, no direct access is not updated). If the update interval of a process, If there is already a marked decentralization of the mark, until marked on the sub-intervals and the parent section does not (in fact, this operation is not possible, because it will clear all query nodes query interval, two mark does not conflict, just to speed up the query speed). For example, the query [2,4]. (Tag can understand the role of the child of this node needs to be updated)

 

 

Code: 

const int N=1e5;
int a[N],sum[N],lazy[N*4];
void init(){
    memset(a,1,sizeof(a));
    memset(sum,0,sizeof(sum));
    memset(lazy,0,sizeof(lazy));
}
void push_up(int rt){
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void bulid(int rt,int l,int r){
    if(l==r){
        sum[rt]=a[l];
        return ;
    }
    int mid=(l+r)>>1;
    bulid(rt<<1,l,mid);
    bulid(rt<<1|1,mid+1,r);
    push_up(rt);
}
void push_down(int rt,int l,int r){
    if(lazy[rt]){
        int m=(l+r)>>1;
        lazy[rt<<1]+=lazy[rt];
        the lazy [RT << . 1 | . 1 ] + = the lazy [RT]; 
        SUM [RT << . 1 ] + = the lazy [RT] * (m-L + . 1 ); 
        SUM [RT << . 1 | . 1 ] + = the lazy [ RT] * (R- m); 
        the lazy [RT] = 0 ; 
    } 
} 
void Update ( int RT, int L, int R & lt, int LL, int RR, int V) {
     IF (LL <= L && R & lt <= RR) {// recursive boundary: query interval, a current section comprising 
        the lazy [RT] + =v; // tag (secondary code, the lazy has a value, indicating the node is updated, the child node is not updated)
        SUM [RT] + = V * (L-R & lt + . 1 ); // Update the current node
         return ; // because lazy, so this can be updated 
    } 
    push_down (RT, L, R & lt); // if lazy [rt] there, an update subranges, will spread to the child node, the current node clears the flag tag. This may not 
    int m = (L + R & lt) >> . 1 ;
     IF (LL <= m) Update (RT << . 1 , L, m, LL, RR, V);
     IF (RR> m) Update ( << RT . 1 | . 1 , m + . 1 , R & lt, LL, RR, V); 
    push_up (RT); // update back the parent node 
} 
Long  Long Query ( int RT, int L, int R & lt, int LL, int RR) {
     IF(LL <= L && R & lt <= RR) return SUM [RT]; 
    push_down (RT, L, R & lt); // check the lazy, to see whether the node update, or update the nodes, empty the marker 
    int m = (L + R & lt) >> . 1 ;
     Long  Long RES = 0 ;
     IF (LL <= m) RES + = Query (RT << . 1 , L, m, LL, RR);
     IF (RR> m) RES + = Query (RT << . 1 | . 1 , + m . 1 , R & lt, LL, RR);
     return RES; 
}

 Too late, change the value to write it tomorrow

Guess you like

Origin www.cnblogs.com/jjl0229/p/11318561.html