线段树之成段更新

成段更新就是在一个区域内进行操作,主要是要用lazy标记。

HDU - 1698

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #define lson l,m,rt<<1
 5 #define rson m+1,r,rt<<1|1
 6 const int MAX = 1e5+10;
 7 int tree[MAX<<2], col[MAX<<2];
 8 void PushUp(int rt){
 9     tree[rt] = tree[rt<<1] + tree[rt<<1|1];
10 }
11 void PushDown(int rt, int m){
12     if(col[rt]){
13         col[rt<<1] = col[rt<<1|1] = col[rt];
14         tree[rt<<1] = (m-(m>>1))*col[rt];
15         tree[rt<<1|1] = (m>>1) * col[rt];
16         col[rt] = 0;
17     }
18 }
19 void build(int l, int r, int rt){
20     col[rt] = 0;
21     tree[rt] = 1;
22     if(l == r) return;
23     int m = (l+r) >> 1;
24     build(lson);
25     build(rson);
26     PushUp(rt);
27 }
28 void update(int L, int R, int c, int l, int r, int rt){
29     if(L <= l && r <= R){
30         col[rt] = c;
31         tree[rt] = c*(r-l+1);
32         return;
33     }
34     PushDown(rt,r-l+1);
35     int m = (l+r)>>1;
36     if(L <= m) update(L,R,c,lson);
37     if(R > m) update(L,R,c,rson);
38     PushUp(rt);
39 }
40 int main(){
41     int t,n,m;
42     scanf("%d",&t);
43     for(int i = 1; i <= t; i ++){
44         scanf("%d %d",&n,&m);
45         build(1,n,1);
46         while(m--){
47             int l,r,num;
48             scanf("%d %d %d",&l,&r,&num);
49             update(l,r,num,1,n,1);
50         }
51         printf("Case %d: The total value of the hook is %d.\n",i,tree[1]);
52     }
53     return 0;
54 }

 

 

POJ - 3468

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #define ll long long
 5 #define lson l,m,rt<<1
 6 #define rson m+1,r,rt<<1|1
 7 using namespace std;
 8 const int MAX = 100010;
 9 ll tree[MAX<<2], add[MAX<<2];
10 void PushUp(int rt){
11     tree[rt] = tree[rt<<1]+tree[rt<<1|1];
12 }
13 void PushDown(int rt, int m){
14     if(add[rt]){
15         add[rt<<1] += add[rt];
16         add[rt<<1|1] += add[rt];
17         tree[rt<<1] += add[rt]*(m-(m>>1));
18         tree[rt<<1|1] += add[rt]*(m>>1);
19         add[rt] = 0;
20     }
21 }
22 void build(int l, int r, int rt){
23     add[rt] = 0;
24     if(l == r){
25         scanf("%lld",&tree[rt]);
26         return;
27     }
28     int m = (l+r)>>1;
29     build(lson);
30     build(rson);
31     PushUp(rt);
32 }
33 void update(int LL, int RR, int num, int l, int r, int rt){
34     if(LL <= l && r <= RR){
35         add[rt] += num;
36         tree[rt] += (ll)num * (r-l+1);
37         return;
38     }
39     PushDown(rt,r-l+1);
40     int m = (l+r)>>1;
41     if(LL <= m)update(LL,RR,num,lson);
42     if(m < RR) update(LL,RR,num,rson);
43     PushUp(rt);
44 }
45 ll query(int LL, int RR, int l, int r, int rt){
46     if(LL <= l && r <= RR){
47         return tree[rt];
48     }
49     PushDown(rt,r-l+1);
50     int m = (l+r)>>1;
51     ll res = 0;
52     if(LL <= m) res += query(LL,RR,lson);
53     if(m < RR) res += query(LL,RR,rson);
54     return res;
55 }
56 int main(){
57     int n,q;
58     scanf("%d %d",&n,&q);
59     build(1,n,1);
60     while(q--){
61         char ch[10];
62         int l,r,num;
63         scanf("%s",ch);
64         if(ch[0] == 'Q'){
65             scanf("%d %d",&l,&r);
66             printf("%lld\n",query(l,r,1,n,1));
67         }else{
68             scanf("%d %d %d",&l,&r,&num);
69             update(l,r,num,1,n,1);
70         }
71     }
72     return 0;
73 }

 

posted @ 2017-07-14 10:51  starry_sky  阅读(179)  评论(0编辑  收藏  举报