#include <bits/stdc++.h>
using namespace std;
const int MX = 2e5 + 10;
const int MT = 5;
const int MOD[2] = {998244353, 420691273};
inline void chad(int& a, int b, int t){
a = (a + b) % MOD[t];
}
inline int add(int a, int b, int t){
return (a + b) % MOD[t];
}
inline int mul(int a, int b, int t){
return 1ll * a * b % MOD[t];
}
inline int sub(int a, int b, int t){
return (a + MOD[t] - b) % MOD[t];
}
int fp[MX][2], pref[MX][2];
void init(){
pref[0][0] = pref[0][1] = fp[0][0] = fp[0][1] = 1;
for(int i = 1; i < MX; i++){
fp[i][0] = mul(fp[i - 1][0], MT, 0);
fp[i][1] = mul(fp[i - 1][1], MT, 1);
pref[i][0] = add(pref[i - 1][0], fp[i][0], 0);
pref[i][1] = add(pref[i - 1][1], fp[i][1], 1);
}
}
int sum(int l, int r, int t){
return (l == 0) ? pref[r][t] : sub(pref[r][t], pref[l - 1][t], t);
}
int N, Q, arr[MX], hs[MX][9]; string s[3], T;
map<int, int> mp;
struct segtree{
int st[MX * 4], lazy[MX * 4];
void propagate(int idx, int l, int r){
if(lazy[idx] != -1){
st[idx] = mul(lazy[idx], sum(l, r, 0), 0);
if(l != r){
lazy[idx << 1] = lazy[idx << 1|1] = lazy[idx];
}
lazy[idx] = -1;
}
}
void build(int idx, int l, int r){
lazy[idx] = -1;
if(l == r){
st[idx] = mul(arr[l], fp[l][0], 0);
}else{
int mid = l + r >> 1;
build(idx << 1, l, mid);
build(idx << 1 | 1, mid + 1, r);
st[idx] = add(st[idx << 1], st[idx << 1 | 1], 0);
}
}
void upd(int idx, int l, int r, int from, int to, int val){
propagate(idx, l, r);
if(r < from || to < l) return;
if(from <= l && r <= to){
lazy[idx] = val;
propagate(idx, l, r);
}else{
int mid = (l + r) >> 1;
upd(idx << 1, l, mid, from, to, val);
upd(idx << 1 | 1, mid + 1, r, from, to, val);
st[idx] = add(st[idx << 1], st[idx << 1 | 1], 0);
}
}
} seg;
// Observation #1 :
// If you match J = 0, O = 1, I = 2
// Crossing the strings is equal to 2(A[i] + B[i]) mod 3
// Where A and B are the two strings
// Assume we have strings A, B, and C we can create strings
// 2A + 2B, 2A + 2C, 2B + 2C
// A + B + 2C, A + C + 2B, B + C + 2A
int main(){
ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
cin >> N >> s[0] >> s[1] >> s[2]; init();
for(int j = 0; j < 3; j++){
for(int i = 0; i < N; i++){
if(s[j][i] == 'J') hs[i][j] = 0;
else if(s[j][i] == 'O') hs[i][j] = 1;
else if(s[j][i] == 'I') hs[i][j] = 2;
}
}
for(int i = 0; i < N; i++){
hs[i][3] = ((hs[i][0] + hs[i][1]) << 1) % 3;
}
for(int i = 0; i < N; i++){
hs[i][4] = ((hs[i][0] + hs[i][2]) << 1) % 3;
}
for(int i = 0; i < N; i++){
hs[i][5] = ((hs[i][1] + hs[i][2]) << 1) % 3;
}
for(int i = 0; i < N; i++){
hs[i][6] = ((hs[i][2] + hs[i][3]) << 1) % 3;
}
for(int i = 0; i < N; i++){
hs[i][7] = ((hs[i][1] + hs[i][4]) << 1) % 3;
}
for(int i = 0; i < N; i++){
hs[i][8] = ((hs[i][0] + hs[i][5]) << 1) % 3;
}
for(int j = 0; j < 9; j++){
int curr = 0;
for(int i = 0; i < N; i++){
chad(curr, mul(hs[i][j], fp[i][0], 0), 0);
}
mp[curr] = 1;
}
cin >> Q >> T;
for(int i = 0; i < N; i++){
if(T[i] == 'J') arr[i] = 0;
else if(T[i] == 'O') arr[i] = 1;
else if(T[i] == 'I') arr[i] = 2;
}
seg.build(1, 0, N - 1);
cout << (mp[seg.st[1]] ? "Yes" : "No") << '\n';
for(int i = 0; i < Q; i++){
int l, r; char c; cin >> l >> r >> c; l--; r--;
if(c == 'J') seg.upd(1, 0, N - 1, l, r, 0);
else if(c == 'O') seg.upd(1, 0, N - 1, l, r, 1);
else seg.upd(1, 0, N - 1, l, r, 2);
cout << (mp[seg.st[1]] ? "Yes" : "No") << '\n';
}
}
Compilation message
Main.cpp: In member function 'void segtree::build(int, int, int)':
Main.cpp:64:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
64 | int mid = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
196 ms |
10924 KB |
Output is correct |
2 |
Correct |
185 ms |
11664 KB |
Output is correct |
3 |
Correct |
181 ms |
6696 KB |
Output is correct |
4 |
Correct |
175 ms |
11652 KB |
Output is correct |
5 |
Correct |
177 ms |
11716 KB |
Output is correct |
6 |
Correct |
168 ms |
11032 KB |
Output is correct |
7 |
Correct |
184 ms |
11240 KB |
Output is correct |
8 |
Correct |
199 ms |
11888 KB |
Output is correct |
9 |
Correct |
187 ms |
11588 KB |
Output is correct |
10 |
Correct |
194 ms |
12744 KB |
Output is correct |
11 |
Correct |
202 ms |
12120 KB |
Output is correct |
12 |
Correct |
234 ms |
12716 KB |
Output is correct |
13 |
Correct |
193 ms |
12244 KB |
Output is correct |
14 |
Correct |
183 ms |
12740 KB |
Output is correct |
15 |
Correct |
184 ms |
12120 KB |
Output is correct |
16 |
Correct |
195 ms |
12648 KB |
Output is correct |
17 |
Correct |
204 ms |
12100 KB |
Output is correct |
18 |
Correct |
124 ms |
5464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
196 ms |
10924 KB |
Output is correct |
2 |
Correct |
185 ms |
11664 KB |
Output is correct |
3 |
Correct |
181 ms |
6696 KB |
Output is correct |
4 |
Correct |
175 ms |
11652 KB |
Output is correct |
5 |
Correct |
177 ms |
11716 KB |
Output is correct |
6 |
Correct |
168 ms |
11032 KB |
Output is correct |
7 |
Correct |
184 ms |
11240 KB |
Output is correct |
8 |
Correct |
199 ms |
11888 KB |
Output is correct |
9 |
Correct |
187 ms |
11588 KB |
Output is correct |
10 |
Correct |
194 ms |
12744 KB |
Output is correct |
11 |
Correct |
202 ms |
12120 KB |
Output is correct |
12 |
Correct |
234 ms |
12716 KB |
Output is correct |
13 |
Correct |
193 ms |
12244 KB |
Output is correct |
14 |
Correct |
183 ms |
12740 KB |
Output is correct |
15 |
Correct |
184 ms |
12120 KB |
Output is correct |
16 |
Correct |
195 ms |
12648 KB |
Output is correct |
17 |
Correct |
204 ms |
12100 KB |
Output is correct |
18 |
Correct |
124 ms |
5464 KB |
Output is correct |
19 |
Correct |
542 ms |
24096 KB |
Output is correct |
20 |
Correct |
429 ms |
24592 KB |
Output is correct |
21 |
Correct |
416 ms |
24756 KB |
Output is correct |
22 |
Correct |
371 ms |
22512 KB |
Output is correct |
23 |
Correct |
286 ms |
14052 KB |
Output is correct |
24 |
Correct |
287 ms |
13124 KB |
Output is correct |
25 |
Correct |
422 ms |
25720 KB |
Output is correct |
26 |
Correct |
391 ms |
24404 KB |
Output is correct |
27 |
Correct |
450 ms |
26176 KB |
Output is correct |
28 |
Correct |
450 ms |
24340 KB |
Output is correct |
29 |
Correct |
450 ms |
25684 KB |
Output is correct |
30 |
Correct |
312 ms |
14060 KB |
Output is correct |
31 |
Correct |
481 ms |
26052 KB |
Output is correct |
32 |
Correct |
375 ms |
23448 KB |
Output is correct |
33 |
Correct |
315 ms |
13080 KB |
Output is correct |
34 |
Correct |
439 ms |
24332 KB |
Output is correct |
35 |
Correct |
329 ms |
22444 KB |
Output is correct |
36 |
Correct |
244 ms |
13124 KB |
Output is correct |
37 |
Correct |
246 ms |
13124 KB |
Output is correct |
38 |
Correct |
403 ms |
22620 KB |
Output is correct |
39 |
Correct |
231 ms |
21492 KB |
Output is correct |
40 |
Correct |
342 ms |
19624 KB |
Output is correct |
41 |
Correct |
359 ms |
17100 KB |
Output is correct |
42 |
Correct |
77 ms |
17048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
196 ms |
10924 KB |
Output is correct |
2 |
Correct |
185 ms |
11664 KB |
Output is correct |
3 |
Correct |
181 ms |
6696 KB |
Output is correct |
4 |
Correct |
175 ms |
11652 KB |
Output is correct |
5 |
Correct |
177 ms |
11716 KB |
Output is correct |
6 |
Correct |
168 ms |
11032 KB |
Output is correct |
7 |
Correct |
184 ms |
11240 KB |
Output is correct |
8 |
Correct |
199 ms |
11888 KB |
Output is correct |
9 |
Correct |
187 ms |
11588 KB |
Output is correct |
10 |
Correct |
194 ms |
12744 KB |
Output is correct |
11 |
Correct |
202 ms |
12120 KB |
Output is correct |
12 |
Correct |
234 ms |
12716 KB |
Output is correct |
13 |
Correct |
193 ms |
12244 KB |
Output is correct |
14 |
Correct |
183 ms |
12740 KB |
Output is correct |
15 |
Correct |
184 ms |
12120 KB |
Output is correct |
16 |
Correct |
195 ms |
12648 KB |
Output is correct |
17 |
Correct |
204 ms |
12100 KB |
Output is correct |
18 |
Correct |
124 ms |
5464 KB |
Output is correct |
19 |
Correct |
192 ms |
11216 KB |
Output is correct |
20 |
Correct |
155 ms |
6728 KB |
Output is correct |
21 |
Correct |
189 ms |
12236 KB |
Output is correct |
22 |
Correct |
158 ms |
10784 KB |
Output is correct |
23 |
Correct |
255 ms |
12292 KB |
Output is correct |
24 |
Correct |
183 ms |
11472 KB |
Output is correct |
25 |
Correct |
212 ms |
12220 KB |
Output is correct |
26 |
Correct |
188 ms |
11300 KB |
Output is correct |
27 |
Correct |
197 ms |
12228 KB |
Output is correct |
28 |
Correct |
181 ms |
11376 KB |
Output is correct |
29 |
Correct |
188 ms |
11724 KB |
Output is correct |
30 |
Correct |
176 ms |
10972 KB |
Output is correct |
31 |
Correct |
180 ms |
12424 KB |
Output is correct |
32 |
Correct |
199 ms |
12328 KB |
Output is correct |
33 |
Correct |
196 ms |
11892 KB |
Output is correct |
34 |
Correct |
170 ms |
11208 KB |
Output is correct |
35 |
Correct |
185 ms |
12668 KB |
Output is correct |
36 |
Correct |
187 ms |
12064 KB |
Output is correct |
37 |
Correct |
243 ms |
12672 KB |
Output is correct |
38 |
Correct |
180 ms |
12052 KB |
Output is correct |
39 |
Correct |
212 ms |
12672 KB |
Output is correct |
40 |
Correct |
191 ms |
12060 KB |
Output is correct |
41 |
Correct |
181 ms |
12740 KB |
Output is correct |
42 |
Correct |
220 ms |
12048 KB |
Output is correct |
43 |
Correct |
184 ms |
11652 KB |
Output is correct |
44 |
Correct |
177 ms |
11912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
196 ms |
10924 KB |
Output is correct |
2 |
Correct |
185 ms |
11664 KB |
Output is correct |
3 |
Correct |
181 ms |
6696 KB |
Output is correct |
4 |
Correct |
175 ms |
11652 KB |
Output is correct |
5 |
Correct |
177 ms |
11716 KB |
Output is correct |
6 |
Correct |
168 ms |
11032 KB |
Output is correct |
7 |
Correct |
184 ms |
11240 KB |
Output is correct |
8 |
Correct |
199 ms |
11888 KB |
Output is correct |
9 |
Correct |
187 ms |
11588 KB |
Output is correct |
10 |
Correct |
194 ms |
12744 KB |
Output is correct |
11 |
Correct |
202 ms |
12120 KB |
Output is correct |
12 |
Correct |
234 ms |
12716 KB |
Output is correct |
13 |
Correct |
193 ms |
12244 KB |
Output is correct |
14 |
Correct |
183 ms |
12740 KB |
Output is correct |
15 |
Correct |
184 ms |
12120 KB |
Output is correct |
16 |
Correct |
195 ms |
12648 KB |
Output is correct |
17 |
Correct |
204 ms |
12100 KB |
Output is correct |
18 |
Correct |
124 ms |
5464 KB |
Output is correct |
19 |
Correct |
542 ms |
24096 KB |
Output is correct |
20 |
Correct |
429 ms |
24592 KB |
Output is correct |
21 |
Correct |
416 ms |
24756 KB |
Output is correct |
22 |
Correct |
371 ms |
22512 KB |
Output is correct |
23 |
Correct |
286 ms |
14052 KB |
Output is correct |
24 |
Correct |
287 ms |
13124 KB |
Output is correct |
25 |
Correct |
422 ms |
25720 KB |
Output is correct |
26 |
Correct |
391 ms |
24404 KB |
Output is correct |
27 |
Correct |
450 ms |
26176 KB |
Output is correct |
28 |
Correct |
450 ms |
24340 KB |
Output is correct |
29 |
Correct |
450 ms |
25684 KB |
Output is correct |
30 |
Correct |
312 ms |
14060 KB |
Output is correct |
31 |
Correct |
481 ms |
26052 KB |
Output is correct |
32 |
Correct |
375 ms |
23448 KB |
Output is correct |
33 |
Correct |
315 ms |
13080 KB |
Output is correct |
34 |
Correct |
439 ms |
24332 KB |
Output is correct |
35 |
Correct |
329 ms |
22444 KB |
Output is correct |
36 |
Correct |
244 ms |
13124 KB |
Output is correct |
37 |
Correct |
246 ms |
13124 KB |
Output is correct |
38 |
Correct |
403 ms |
22620 KB |
Output is correct |
39 |
Correct |
231 ms |
21492 KB |
Output is correct |
40 |
Correct |
342 ms |
19624 KB |
Output is correct |
41 |
Correct |
359 ms |
17100 KB |
Output is correct |
42 |
Correct |
77 ms |
17048 KB |
Output is correct |
43 |
Correct |
192 ms |
11216 KB |
Output is correct |
44 |
Correct |
155 ms |
6728 KB |
Output is correct |
45 |
Correct |
189 ms |
12236 KB |
Output is correct |
46 |
Correct |
158 ms |
10784 KB |
Output is correct |
47 |
Correct |
255 ms |
12292 KB |
Output is correct |
48 |
Correct |
183 ms |
11472 KB |
Output is correct |
49 |
Correct |
212 ms |
12220 KB |
Output is correct |
50 |
Correct |
188 ms |
11300 KB |
Output is correct |
51 |
Correct |
197 ms |
12228 KB |
Output is correct |
52 |
Correct |
181 ms |
11376 KB |
Output is correct |
53 |
Correct |
188 ms |
11724 KB |
Output is correct |
54 |
Correct |
176 ms |
10972 KB |
Output is correct |
55 |
Correct |
180 ms |
12424 KB |
Output is correct |
56 |
Correct |
199 ms |
12328 KB |
Output is correct |
57 |
Correct |
196 ms |
11892 KB |
Output is correct |
58 |
Correct |
170 ms |
11208 KB |
Output is correct |
59 |
Correct |
185 ms |
12668 KB |
Output is correct |
60 |
Correct |
187 ms |
12064 KB |
Output is correct |
61 |
Correct |
243 ms |
12672 KB |
Output is correct |
62 |
Correct |
180 ms |
12052 KB |
Output is correct |
63 |
Correct |
212 ms |
12672 KB |
Output is correct |
64 |
Correct |
191 ms |
12060 KB |
Output is correct |
65 |
Correct |
181 ms |
12740 KB |
Output is correct |
66 |
Correct |
220 ms |
12048 KB |
Output is correct |
67 |
Correct |
184 ms |
11652 KB |
Output is correct |
68 |
Correct |
177 ms |
11912 KB |
Output is correct |
69 |
Correct |
453 ms |
21924 KB |
Output is correct |
70 |
Correct |
448 ms |
24616 KB |
Output is correct |
71 |
Correct |
285 ms |
13124 KB |
Output is correct |
72 |
Correct |
268 ms |
13248 KB |
Output is correct |
73 |
Correct |
253 ms |
13164 KB |
Output is correct |
74 |
Correct |
351 ms |
23460 KB |
Output is correct |
75 |
Correct |
285 ms |
14284 KB |
Output is correct |
76 |
Correct |
405 ms |
25760 KB |
Output is correct |
77 |
Correct |
327 ms |
22412 KB |
Output is correct |
78 |
Correct |
250 ms |
13196 KB |
Output is correct |
79 |
Correct |
244 ms |
13200 KB |
Output is correct |
80 |
Correct |
325 ms |
22024 KB |
Output is correct |
81 |
Correct |
315 ms |
14024 KB |
Output is correct |
82 |
Correct |
373 ms |
25816 KB |
Output is correct |
83 |
Correct |
395 ms |
23468 KB |
Output is correct |
84 |
Correct |
251 ms |
13148 KB |
Output is correct |
85 |
Correct |
259 ms |
13176 KB |
Output is correct |
86 |
Correct |
354 ms |
21656 KB |
Output is correct |
87 |
Correct |
395 ms |
24312 KB |
Output is correct |
88 |
Correct |
247 ms |
12712 KB |
Output is correct |
89 |
Correct |
380 ms |
22908 KB |
Output is correct |
90 |
Correct |
411 ms |
24604 KB |
Output is correct |
91 |
Correct |
270 ms |
12748 KB |
Output is correct |
92 |
Correct |
342 ms |
22272 KB |
Output is correct |
93 |
Correct |
270 ms |
13192 KB |
Output is correct |
94 |
Correct |
251 ms |
13204 KB |
Output is correct |
95 |
Correct |
233 ms |
13120 KB |
Output is correct |
96 |
Correct |
403 ms |
22552 KB |
Output is correct |
97 |
Correct |
239 ms |
22604 KB |
Output is correct |
98 |
Correct |
310 ms |
19608 KB |
Output is correct |
99 |
Correct |
382 ms |
17040 KB |
Output is correct |
100 |
Correct |
90 ms |
17084 KB |
Output is correct |