-
Notifications
You must be signed in to change notification settings - Fork 9
/
LITE - Light Switching.cpp
80 lines (80 loc) · 1.77 KB
/
LITE - Light Switching.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
#include<bits/stdc++.h>
using namespace std;
#define mx 100005
int tree[mx*4]={0}, lazy[mx*4]={0};
void update(int pos, int b, int e, int i, int j)
{
if(b>e) return;
int m = (b+e)/2;
int l = pos*2;
int r = l+1;
if(lazy[pos]!=0){
lazy[pos]%=2;
if(lazy[pos]==1){
tree[pos] = (e-b+1) - tree[pos];
if(b!=e){
lazy[l]++;
lazy[r]++;
lazy[l]%=2;
lazy[r]%=2;
}
lazy[pos] = 0;
}
}
if(b>j || e<i) return;
if(b>=i && e<=j){
tree[pos] = (e-b+1) - tree[pos];
if(b!=e){
lazy[l]++;
lazy[r]++;
lazy[l]%=2;
lazy[r]%=2;
}
return;
}
update(l, b, m, i, j);
update(r, m+1, e, i, j);
tree[pos] = tree[l] + tree[r];
}
int query(int pos, int b, int e, int i, int j)
{
if(b>e) return 0;
int m = (b+e)/2;
int l = pos*2;
int r = l+1;
if(lazy[pos]!=0){
lazy[pos]%=2;
if(lazy[pos]==1){
tree[pos] = (e-b+1) - tree[pos];
if(b!=e){
lazy[l]++;
lazy[r]++;
lazy[l]%=2;
lazy[r]%=2;
}
lazy[pos] = 0;
}
}
if(b>j || e<i) return 0;
if(b>=i && e<=j){
return tree[pos];
}
int q1 = query(l, b, m, i, j);
int q2 = query(r, m+1, e, i, j);
return q1 + q2;
}
int main()
{
int n,m,c,a,b;
scanf("%d%d", &n, &m);
while(m--){
scanf("%d%d%d", &c, &a, &b);
if(c==0){
update(1,1,n,a,b);
}
else{
printf("%d\n", query(1,1,n,a,b));
}
}
return 0;
}