-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCHEF_FLIPCOIN.cpp
94 lines (69 loc) · 1.3 KB
/
CHEF_FLIPCOIN.cpp
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
87
88
89
90
91
92
93
#include<stdio.h>
struct node
{
int heads;
int flips;
};
typedef struct node node;
node tree[500010];
void update(int index, int tl, int tr, int l, int r)
{
if(tl==l && tr==r)
{
tree[index].flips++;
tree[index].heads=tr-tl+1-tree[index].heads;
return;
}
int mid=(tl+tr)>>1;
int left=index<<1;
int right=left+1;
if(r<=mid)
{
update(left,tl,mid,l,r);
}
else if(l>mid)
{
update(right,mid+1,tr,l,r);
}
else
{
update(left,tl,mid,l,mid);
update(right,mid+1,tr,mid+1,r);
}
tree[index].heads=tree[left].heads+tree[right].heads;
if(tree[index].flips%2==1)
tree[index].heads=tr-tl+1-tree[index].heads;
}
int query(int index, int tl, int tr, int l, int r, int flips)
{
if(tl==l && tr==r)
{
if(flips%2==1)
return tr-tl+1-tree[index].heads;
else return tree[index].heads;
}
flips+=tree[index].flips;
int mid=(tl+tr)>>1;
int left=index<<1;
int right=left+1;
if(r<=mid)
return query(left,tl,mid,l,r,flips);
else if(l>mid)
return query(right,mid+1,tr,l,r,flips);
else return query(left,tl,mid,l,mid,flips)+query(right,mid+1,tr,mid+1,r,flips);
}
int main()
{
int n,q,l,r,c;
scanf("%d%d",&n,&q);
while(q--)
{
scanf("%d%d%d",&c,&l,&r);
if(c==0)
{
update(1,0,n-1,l,r);
}
else printf("%d\n",query(1,0,n-1,l,r,0));
}
return 0;
}