-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuva11402.cpp
More file actions
175 lines (175 loc) · 3.85 KB
/
Copy pathuva11402.cpp
File metadata and controls
175 lines (175 loc) · 3.85 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include<iostream>
using namespace std;
const int MAXN = 1048576;
const int mod = 998244353;
string s;
long long lz[MAXN<<2];
long long cnt[MAXN<<2];
void pushdown(int x, int l, int r){
if(!lz[x])return ;
int mid = (l + r) >> 1;
if (lz[x] == 1) {
lz[x<<1] = 1;
lz[x<<1|1] = 1;
cnt[x<<1] = 0;
cnt[x<<1|1] = 0;
}
else if (lz[x] == 2) {
lz[x<<1] = 2;
lz[x<<1|1] = 2;
cnt[x<<1] = mid - l + 1;
cnt[x<<1|1] = r - mid;
}
else {
if (!lz[x<<1]) {
lz[x<<1] = 3;
}
else if (lz[x<<1] == 1) {
lz[x<<1] = 2;
}
else if (lz[x<<1] == 2) {
lz[x<<1] = 1;
}
else {
lz[x<<1] = 0;
}
cnt[x<<1] = mid - l + 1 - cnt[x<<1];
if (!lz[x<<1|1]) {
lz[x<<1|1] = 3;
}
else if (lz[x<<1|1] == 1) {
lz[x<<1|1] = 2;
}
else if (lz[x<<1|1] == 2) {
lz[x<<1|1] = 1;
}
else {
lz[x<<1|1] = 0;
}
cnt[x<<1|1] = r - mid - cnt[x<<1|1];
}
lz[x] = 0;
}
void pushup(int x){
cnt[x] = cnt[x<<1] + cnt[x<<1|1];
}
void build(int x,int l,int r){
lz[x] = cnt[x] = 0;
if(l==r){
if (s[l] == '1') {
cnt[x] = 1;
}
else {
cnt[x] = 0;
}
return;
}
int mid = (l+r) >> 1;
build(x<<1,l,mid);
build(x<<1|1,mid+1,r);
pushup(x);
}
void update(int x,int l,int r,int ql,int qr, int op){
if(ql <= l&&qr >= r){
if (op <= 2) {
if (op == 1) {
cnt[x] = 0;
lz[x] = 1;
}
else if (op == 2) {
cnt[x] = r - l + 1;
lz[x] = 2;
}
}
else {
if (lz[x] == 1) {
lz[x] = 2;
}
else if (lz[x] == 2) {
lz[x] = 1;
}
else if (lz[x] == 3) {
lz[x] = 0;
}
else {
lz[x] = 3;
}
cnt[x] = r - l + 1 - cnt[x];
}
return;
}
pushdown(x, l, r);
int mid = (l+r) >> 1;
if(ql <= mid){
update(x<<1, l, mid, ql, qr, op);
}
if(qr > mid){
update(x<<1|1, mid + 1, r, ql, qr, op);
}
pushup(x);
}
int query(int x,int l,int r,int ql,int qr){
if(ql <= l&&qr >= r){
//cout << l <<' '<<r<<' '<<cnt[x] << ' ' << lz[x] << endl;
return cnt[x];
}
pushdown(x, l, r);
int mid = (l+r) >> 1;
int ret = 0;
if(ql <= mid){
ret += query(x<<1, l, mid, ql, qr);
}
if(qr > mid){
ret += query(x<<1|1, mid + 1, r, ql, qr);
}
return ret;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("out.txt","w",stdout);
int t;
cin >> t;
int c = 0;
while (t--) {
int qc = 0;
c++;
cout << "Case "<<c<<":\n";
s = "";
int k;
cin >> k;
for (int i = 0; i < k; i++) {
int cnt;
string ss;
cin >> cnt >> ss;
for (int j = 0; j < cnt; j++) {
s += ss;
}
}
//s = " " + s;
//cout << s << endl;
build(1,0,s.size() - 1);
int q;
cin >> q;
while (q--) {
char c;
cin >> c;
int l, r;
cin >> l >> r;
if (c == 'F') {
update(1,0,s.size()-1,l,r,2);
}
else if (c == 'E') {
update(1,0,s.size()-1,l,r,1);
}
else if (c == 'I') {
update(1,0,s.size()-1,l,r,3);
}
else {
qc++;
cout << 'Q'<<qc<<": ";
cout << query(1,0,s.size()-1,l,r) << '\n';
}
}
}
}