-
Notifications
You must be signed in to change notification settings - Fork 34
/
1080-Binary-Simulation.cpp
114 lines (83 loc) · 1.31 KB
/
1080-Binary-Simulation.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int create_tree(int input[],int tree[], int n)
{
int x;
for (int i = 1; i <= n; i++) {
x = i;
while(x <= n) {
tree[x] += input[i];
x += x & -x;
}
}
}
int update(int tree[], int x, int val, int n)
{
if(x == 0) {
return 0;
}
while(x <= n) {
tree[x] += val;
x += x & -x;
}
}
int query(int tree[], int x)
{
int sum;
sum = 0;
while(x > 0) {
sum += tree[x];
x -= x & -x;
}
return sum;
}
int alter(int tree[], int i, int j, int n)
{
update(tree, i, 1, n);
update(tree, j+1, -1, n);
}
int main()
{
int N = 100009;
int a[N];
int tree[N];
int n;
int x;
int t;
int y;
char temp;
int q;
char choice;
char epic[100007];
int ans;
ios_base::sync_with_stdio(false);
cin >> t;
for (int cs = 1; cs <= t; cs++) {
cin >> epic;
n = strlen(epic);
for (int i = 0; i < n; i++) {
a[i+1] = epic[i] - '0';
}
// create_tree(a, tree, n);
memset(tree, 0, sizeof(tree));
cin >> q;
printf("Case %d:\n", cs);
for (int i = 0; i < q; i++) {
cin >> choice;
switch(choice) {
case 'I':
cin >> x;
cin >> y;
alter(tree, x, y, n);
break;
case 'Q':
cin >> x;
ans = (a[x] ^ (query(tree, x) % 2));
printf("%d\n", ans);
break;
}
}
}
}