-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLITE.cpp
More file actions
76 lines (65 loc) · 1.41 KB
/
LITE.cpp
File metadata and controls
76 lines (65 loc) · 1.41 KB
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
//LITE.cpp
#include <iostream>
#include <cstring>
using namespace std;
int tree[4*100005];
bool lazy[4*100005];
void update(int i, int start, int end, int low, int high){
if (lazy[i]){
lazy[i] = false;
tree[i] = end-start+1 - tree[i];
if (start!=end){
lazy[2*i] = !lazy[2*i];
lazy[2*i+1] = !lazy[2*i+1];
}
}
if (start>end || low>end || high<start)
return;
if (start>=low && end<=high){
tree[i] = end-start+1 - tree[i];
if (start!=end){
lazy[2*i] = !lazy[2*i];
lazy[2*i+1] = !lazy[2*i+1];
}
return;
}
int mid = start+(end-start)/2;
update(2*i, start, mid, low, high);
update(2*i+1, mid+1, end, low, high);
tree[i] = tree[2*i] + tree[2*i+1];
}
int query(int i, int start, int end, int low, int high){
if (low>end || high<start)
return 0;
if (lazy[i]){
tree[i] = end-start+1 - tree[i];
if (start!=end){
lazy[2*i] = !lazy[2*i];
lazy[2*i+1] = !lazy[2*i+1];
}
lazy[i] = false;
}
if (start>=low && end<=high)
return tree[i];
int mid = start+(end-start)/2;
int res1 = query(2*i, start, mid, low, high);
int res2 = query(2*i+1, mid+1, end, low, high);
return res1+res2;
}
int main() {
memset(tree, 0, sizeof tree);
memset(lazy, false, sizeof lazy);
int n, c;
scanf("%d%d", &n, &c);
while (c--){
int x, l, r;
scanf("%d%d%d", &x, &l, &r);
if (x==0){
update(1, 1, n, l, r);
}else{
int res = query(1, 1, n, l, r);
printf("%d\n", res);
}
}
return 0;
}