线段树

COGS2527

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<queue>
#include<vector>
#include<algorithm>
#include<cmath>
#include<stack>
#include<map>
using namespace std;
#define R register
#define ll long long
#define fo(i,a,b) for(R int (i)=(a);(i)<=(b);++(i))
#define dbg(x) cout<<#x<<"="<<x<<'\n'
#define mmt(x,y) memset(x,y,sizeof(x))
int n,m;
struct node
{
int l,r,sum,lazy;
}a[200000<<4|1];
void build(int x,int l,int r)
{
a[x].l=l;a[x].r=r;
if(l==r)
{
a[x].sum=1;
return;
}
R int mid=(l+r)>>1;
build(x<<1,l,mid);build(x<<1|1,mid+1,r);
a[x].sum=a[x<<1].sum+a[x<<1|1].sum;
}
void mt(int x)
{
a[x<<1].lazy=a[x<<1|1].lazy=1;
a[x<<1].sum=a[x<<1|1].sum=0;
a[x].lazy=0;
}
int query(int x,int l,int r)
{
if(l<=a[x].l&&a[x].r<=r)return a[x].sum;
if(a[x].lazy)mt(x);
R int mid=(a[x].l+a[x].r)>>1,res=0;
if(l<=mid)res+=query(x<<1,l,r);
if(r>mid)res+=query(x<<1|1,l,r);
return res;
}
void change(int x,int l,int r)
{
if(l<=a[x].l&&a[x].r<=r)
{
a[x].lazy=1;a[x].sum=0;
return;
}
if(a[x].lazy)mt(x);
R int mid=(a[x].l+a[x].r)>>1;
if(l<=mid)change(x<<1,l,r);
if(r>mid)change(x<<1|1,l,r);
a[x].sum=a[x<<1].sum+a[x<<1|1].sum;
}
int main()
{
scanf("%d%d",&n,&m);
build(1,1,n);
while(m--)
{
R int l,r;
scanf("%d%d",&l,&r);
change(1,l,r);
printf("%d\n",query(1,1,n));
}
return 0;
}

显示 Gitment 评论