Steven_MengのBlog

传送门

发现翻转奇数次的数变成$1$,翻转偶数次的数变成$0$,于是只需知道每个数翻转了几次,就知道它最终的值,使用单点查询,区间修改的普通线段树即可。

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
75
76
77
78
79
80
81
82
83
84
85
86
#include <bits/stdc++.h>
#define MAXN 100005
using namespace std;
inline int read(){
int x=0,f=1;
char ch=getchar();
while (ch<'0'||ch>'9'){
if (ch=='-') f=-1;
ch=getchar();
}
while (ch>='0'&&ch<='9'){
x=(x<<3)+(x<<1)+(ch^'0');
ch=getchar();
}
return x*f;
}
namespace SegmentTree{
struct node{
int l,r;
int val;
int tag;
inline int len(){
return r-l+1;
}
}tree[MAXN<<2];
#define lc i<<1
#define rc i<<1|1
inline void pushup(int i){
tree[i].val=tree[lc].val+tree[rc].val;
}
inline void Rev(int i,int val){
tree[i].val+=tree[i].len()*val;
tree[i].tag+=val;
}
inline void pushdown(int i){
if (tree[i].tag){
Rev(lc,tree[i].tag),Rev(rc,tree[i].tag);
tree[i].tag=0;
}
}
void Build(int i,int l,int r){
tree[i].l=l,tree[i].r=r;
if (l==r){
return ;
}
int mid=(l+r)>>1;
Build(lc,l,mid);
Build(rc,mid+1,r);
}
void Update(int i,int L,int R){
if (L<=tree[i].l&&tree[i].r<=R){
Rev(i,1);
return ;
}
pushdown(i);
int mid=(tree[i].l+tree[i].r)>>1;
if (L<=mid) Update(lc,L,R);
if (mid<R) Update(rc,L,R);
pushup(i);
}
int Query(int i,int index){
if (tree[i].l==tree[i].r){
return tree[i].val;
}
pushdown(i);
int mid=(tree[i].l+tree[i].r)>>1;
if (index<=mid) return Query(lc,index);
else return Query(rc,index);
}
}
using namespace SegmentTree;
int main(){
int n=read(),m=read();
Build(1,1,n);
for (register int i=1;i<=m;++i){
int opr=read();
if (opr==1){
int l=read(),r=read();
Update(1,l,r);
}
else {
int p=read();
printf("%d\n",Query(1,p)&1);
}
}
}

 评论