-
Notifications
You must be signed in to change notification settings - Fork 2
/
1080 - Binary Simulation.cpp
90 lines (90 loc) · 2.07 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
#include<bits/stdc++.h>
using namespace std;
#define mx 100005
int ara[mx];
struct info
{
int value;
int prop;
}tree[4*mx];
void build(int at, int begin,int end)
{
if(begin == end){
tree[at].value = ara[begin];
tree[at].prop = 0;
return;
}
int left = at*2;
int right = at*2+1;
int mid = (begin+end)/2;
build(left,begin,mid);
build(right,mid+1,end);
tree[at].value = tree[left].value + tree[right].value;
tree[at].prop = 0;
}
int query(int at,int begin,int end,int i)
{
if(begin>=i && end<=i){
int x = tree[at].value;/// modify
if(tree[at].prop %2 ==1)
x = (x == 0) ? 1:0;
return x;
}
int left = at*2;
int right = at*2+1;
int mid = (begin+end)/2;
int p1;
if(i<=mid)
p1 = query(left,begin,mid,i);
else
p1 = query(right,mid+1,end,i);
if(tree[at].prop %2 ==1)
p1 = (p1 == 0) ? 1:0;
return p1;
}
void update(int at, int begin,int end,int i,int j)
{
if(j<begin || i>end) return;// index is out of range
if(begin>=i && end<=j){// total overlap
tree[at].prop++;
return;
}
int left = at*2;
int right = at*2+1;
int mid = (begin+end)/2;
update(left,begin,mid,i,j);
update(right,mid+1,end,i,j);
}
int main()
{
int tc,q,i,j;
scanf("%d",&tc);getchar();
char c;
for(int t=1; t<=tc; t++){
int n=0;
while(c = getchar()){
if(c == '0')
ara[++n] = 0;
else if(c == '1')
ara[++n] = 1;
else
break;
}
build(1,1,n);
printf("Case %d:\n",t);
scanf("%d",&q);getchar();
while(q--){
c = getchar();
if(c == 'I'){
scanf("%d%d",&i,&j);
update(1,1,n,i,j);
}
else{
scanf("%d",&i);
printf("%d\n",query(1,1,n,i));
}
getchar();
}
}
return 0;
}