codevs 1191 树轴染色 线段树区间定值,求和

题目链接:

http://codevs.cn/problem/1191/

题意:

题解:

sum表示这段区间白点的个数,初始全部为0。
lazy为1表示到达这个区间需要被更新为白点,到需要更新的时候再pushdown

代码:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 #define MS(a) memset(a,0,sizeof(a))
 5 #define MP make_pair
 6 #define PB push_back
 7 const int INF = 0x3f3f3f3f;
 8 const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
 9 inline ll read(){
10     ll x=0,f=1;char ch=getchar();
11     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
12     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
13     return x*f;
14 }
15 //////////////////////////////////////////////////////////////////////////
16 const int maxn = 4e5+10;
17 
18 struct node{
19     int l,r,sum,lazy;
20 }tree[maxn<<2];
21 
22 void pushup(int rt){
23     tree[rt].sum = tree[rt<<1].sum + tree[rt<<1|1].sum;
24 }
25 
26 void pushdown(int rt,int x){
27     if(tree[rt].lazy!=-1){
28         tree[rt<<1].lazy = tree[rt<<1|1].lazy = tree[rt].lazy;
29         tree[rt<<1].sum = (x-(x>>1));
30         tree[rt<<1|1].sum = (x>>1);
31         tree[rt].lazy = -1;
32     }
33 }
34 
35 void build(int rt,int l,int r){
36     tree[rt].l = l, tree[rt].r = r;
37     tree[rt].lazy = -1;
38     if(l == r)
39         tree[rt].sum = 0;
40     else{
41         int mid = (l+r)/2;
42         build(rt<<1,l,mid);
43         build(rt<<1|1,mid+1,r);
44         pushup(rt);
45     }
46 }
47 
48 void update(int rt,int l,int r){
49     int L = tree[rt].l, R = tree[rt].r;
50     if(l<=L && R<=r){
51         tree[rt].lazy = 1;
52         tree[rt].sum = R-L+1;
53         return ;
54     }else{
55         pushdown(rt,R-L+1);
56         int mid = (L+R)/2;
57         if(l <= mid) update(rt<<1,l,r);
58         if(r > mid) update(rt<<1|1,l,r);
59         pushup(rt);
60     }
61 }
62 
63 int main(){
64     int n=read(),m=read();
65     build(1,1,n);
66     for(int i=0; i<m; i++){
67         int a=read(),b=read();
68         update(1,a,b);
69         cout << n-tree[1].sum << endl;
70     }
71 
72     return 0;
73 }

 

posted @ 2017-03-02 15:11  _yxg123  阅读(159)  评论(0编辑  收藏  举报