#include "bits/stdc++.h"
using namespace std;
#define int long long
const int MAXN = 2e5 + 10;
int MOD;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int bm(int b, int p) {
if(p==0) return 1 % MOD;
int r = bm(b, p >> 1);
if(p&1) return (((r*r) % MOD) * b) % MOD;
return (r*r) % MOD;
}
int inv(int b) {
return bm(b, MOD-2);
}
int fastlog(int x) {
return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void printcase(int i) { cout << "Case #" << i << ": "; }
string gene(string a, string b) {
string c;
for(int i=0; i<a.size(); i++) {
if(a[i] == b[i]) c += a[i];
else {
string t = "JOI";
for(int j=0; j<3; j++) {
if(a[i] != t[j] && b[i] != t[j]) c += t[j];
}
}
}
return c;
}
int random_prime(int l, int r) {
while(1) {
int x = rnd(l, r);
int c = 0;
for(int i=1; i*i<=x; i++) {
if(x%i == 0) {
c+=2;
if(i*i == x) c--;
}
}
if(c==2) return x;
}
return 1226;
}
int pre[MAXN];
int bruh;
int geometric_sequence_sum(int a, int x, int n) {
// S = ax^0+ax^1+...+ax^(n-1)
// xS = ax^1+ax^2+...+ax^n
// (x-1)S = ax^n-a
// S = a(x^n-1)/(x-1)
int s = a;
s *= pre[n] - 1;
s %= MOD;
s *= bruh;
s %= MOD;
return s;
}
int ans, n;
void add_segment(int l, int r, int v) {
ans += geometric_sequence_sum(pre[n-r-1], 3, r-l+1) * v;
ans %= MOD;
}
void sub_segment(int l, int r, int v) {
ans -= geometric_sequence_sum(pre[n-r-1], 3, r-l+1) * v;
ans += (MOD << 1);
ans %= MOD;
}
void solve(int tc) {
MOD = random_prime(5e8, 1.25 * 1e9);
cin >> n;
pre[0] = 1;
bruh = inv(2);
for(int i=1; i<=n; i++) pre[i] = (pre[i-1] * 3) % MOD;
string s[9];
cin >> s[0] >> s[1] >> s[2];
s[3] = gene(s[0], s[1]);
s[4] = gene(s[0], s[2]);
s[5] = gene(s[1], s[2]);
s[6] = gene(s[2], s[3]);
s[7] = gene(s[1], s[4]);
s[8] = gene(s[0], s[5]);
int h[9];
for(int i=0; i<9; i++) {
h[i] = 0;
for(int j=0; j<n; j++) {
h[i] = (h[i] * 3 + (s[i][j] == 'J' ? 0 : (s[i][j] == 'O' ? 1 : 2))) % MOD;
}
}
int q;
cin >> q;
string def;
cin >> def;
for(char c: def) ans = (ans * 3 + (c == 'J' ? 0 : (c == 'O' ? 1 : 2))) % MOD;
bool yes = 0;
for(int i=0; i<9; i++) yes |= (ans == h[i]);
cout << (yes ? "Yes\n" : "No\n");
set<pair<pair<int,int>,int> > ranges;
for(int i=0; i<def.size(); i++) {
int j = i;
while(j+1 < def.size() && def[j+1] == def[i]) j++;
ranges.insert({{i, j}, (def[i] == 'J' ? 0 : (def[i] == 'O' ? 1 : 2))});
i = j;
}
for(int i=0; i<q; i++) {
int l, r;
cin >> l >> r;
--l, --r;
char c;
cin >> c;
auto it = ranges.lower_bound({{l, 0}, 0});
if(it != ranges.begin()) it--;
vector<pair<pair<int,int>,int> > to_add, to_sub;
to_add.push_back({{l, r}, (c == 'J' ? 0 : (c == 'O' ? 1 : 2))});
while(it != ranges.end() && (*it).first.first <= r) {
if((*it).first.second >= l) { // relevant
to_sub.push_back(*it);
if((*it).first.first < l) {
to_add.push_back({{(*it).first.first, l-1}, (*it).second});
}
if((*it).first.second > r) {
to_add.push_back({{r+1, (*it).first.second}, (*it).second});
}
}
it++;
}
for(auto x: to_sub) {
ranges.erase(x);
sub_segment(x.first.first, x.first.second, x.second);
}
for(auto x: to_add) {
ranges.insert(x);
add_segment(x.first.first, x.first.second, x.second);
}
bool yes = 0;
for(int i=0; i<9; i++) {
if(h[i] == ans) yes = 1;
}
cout << (yes ? "Yes\n" : "No\n");
}
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
Compilation message
Main.cpp: In function 'std::string gene(std::string, std::string)':
Main.cpp:25:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for(int i=0; i<a.size(); i++) {
| ~^~~~~~~~~
Main.cpp: In function 'void solve(long long int)':
Main.cpp:105:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
105 | for(int i=0; i<def.size(); i++) {
| ~^~~~~~~~~~~
Main.cpp:107:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | while(j+1 < def.size() && def[j+1] == def[i]) j++;
| ~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
179 ms |
800 KB |
Output is correct |
2 |
Correct |
162 ms |
880 KB |
Output is correct |
3 |
Correct |
128 ms |
848 KB |
Output is correct |
4 |
Correct |
175 ms |
896 KB |
Output is correct |
5 |
Correct |
181 ms |
844 KB |
Output is correct |
6 |
Correct |
171 ms |
892 KB |
Output is correct |
7 |
Correct |
165 ms |
856 KB |
Output is correct |
8 |
Correct |
180 ms |
864 KB |
Output is correct |
9 |
Correct |
181 ms |
884 KB |
Output is correct |
10 |
Correct |
164 ms |
832 KB |
Output is correct |
11 |
Correct |
175 ms |
860 KB |
Output is correct |
12 |
Correct |
158 ms |
896 KB |
Output is correct |
13 |
Correct |
169 ms |
800 KB |
Output is correct |
14 |
Correct |
169 ms |
860 KB |
Output is correct |
15 |
Correct |
177 ms |
904 KB |
Output is correct |
16 |
Correct |
166 ms |
924 KB |
Output is correct |
17 |
Correct |
174 ms |
836 KB |
Output is correct |
18 |
Correct |
118 ms |
928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
179 ms |
800 KB |
Output is correct |
2 |
Correct |
162 ms |
880 KB |
Output is correct |
3 |
Correct |
128 ms |
848 KB |
Output is correct |
4 |
Correct |
175 ms |
896 KB |
Output is correct |
5 |
Correct |
181 ms |
844 KB |
Output is correct |
6 |
Correct |
171 ms |
892 KB |
Output is correct |
7 |
Correct |
165 ms |
856 KB |
Output is correct |
8 |
Correct |
180 ms |
864 KB |
Output is correct |
9 |
Correct |
181 ms |
884 KB |
Output is correct |
10 |
Correct |
164 ms |
832 KB |
Output is correct |
11 |
Correct |
175 ms |
860 KB |
Output is correct |
12 |
Correct |
158 ms |
896 KB |
Output is correct |
13 |
Correct |
169 ms |
800 KB |
Output is correct |
14 |
Correct |
169 ms |
860 KB |
Output is correct |
15 |
Correct |
177 ms |
904 KB |
Output is correct |
16 |
Correct |
166 ms |
924 KB |
Output is correct |
17 |
Correct |
174 ms |
836 KB |
Output is correct |
18 |
Correct |
118 ms |
928 KB |
Output is correct |
19 |
Correct |
318 ms |
12984 KB |
Output is correct |
20 |
Correct |
363 ms |
15404 KB |
Output is correct |
21 |
Correct |
448 ms |
17792 KB |
Output is correct |
22 |
Correct |
343 ms |
16780 KB |
Output is correct |
23 |
Correct |
274 ms |
1908 KB |
Output is correct |
24 |
Correct |
315 ms |
1844 KB |
Output is correct |
25 |
Correct |
447 ms |
21952 KB |
Output is correct |
26 |
Correct |
391 ms |
18480 KB |
Output is correct |
27 |
Correct |
283 ms |
5096 KB |
Output is correct |
28 |
Correct |
288 ms |
5204 KB |
Output is correct |
29 |
Correct |
308 ms |
5096 KB |
Output is correct |
30 |
Correct |
264 ms |
1412 KB |
Output is correct |
31 |
Correct |
278 ms |
5160 KB |
Output is correct |
32 |
Correct |
315 ms |
5436 KB |
Output is correct |
33 |
Correct |
242 ms |
1752 KB |
Output is correct |
34 |
Correct |
351 ms |
18468 KB |
Output is correct |
35 |
Correct |
364 ms |
15248 KB |
Output is correct |
36 |
Correct |
251 ms |
1988 KB |
Output is correct |
37 |
Correct |
249 ms |
1876 KB |
Output is correct |
38 |
Correct |
236 ms |
15316 KB |
Output is correct |
39 |
Correct |
271 ms |
12776 KB |
Output is correct |
40 |
Correct |
361 ms |
13996 KB |
Output is correct |
41 |
Correct |
300 ms |
18416 KB |
Output is correct |
42 |
Correct |
165 ms |
18480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
179 ms |
800 KB |
Output is correct |
2 |
Correct |
162 ms |
880 KB |
Output is correct |
3 |
Correct |
128 ms |
848 KB |
Output is correct |
4 |
Correct |
175 ms |
896 KB |
Output is correct |
5 |
Correct |
181 ms |
844 KB |
Output is correct |
6 |
Correct |
171 ms |
892 KB |
Output is correct |
7 |
Correct |
165 ms |
856 KB |
Output is correct |
8 |
Correct |
180 ms |
864 KB |
Output is correct |
9 |
Correct |
181 ms |
884 KB |
Output is correct |
10 |
Correct |
164 ms |
832 KB |
Output is correct |
11 |
Correct |
175 ms |
860 KB |
Output is correct |
12 |
Correct |
158 ms |
896 KB |
Output is correct |
13 |
Correct |
169 ms |
800 KB |
Output is correct |
14 |
Correct |
169 ms |
860 KB |
Output is correct |
15 |
Correct |
177 ms |
904 KB |
Output is correct |
16 |
Correct |
166 ms |
924 KB |
Output is correct |
17 |
Correct |
174 ms |
836 KB |
Output is correct |
18 |
Correct |
118 ms |
928 KB |
Output is correct |
19 |
Correct |
154 ms |
912 KB |
Output is correct |
20 |
Correct |
141 ms |
860 KB |
Output is correct |
21 |
Correct |
159 ms |
844 KB |
Output is correct |
22 |
Correct |
139 ms |
848 KB |
Output is correct |
23 |
Correct |
160 ms |
832 KB |
Output is correct |
24 |
Correct |
158 ms |
796 KB |
Output is correct |
25 |
Correct |
163 ms |
920 KB |
Output is correct |
26 |
Correct |
143 ms |
900 KB |
Output is correct |
27 |
Correct |
153 ms |
808 KB |
Output is correct |
28 |
Correct |
146 ms |
836 KB |
Output is correct |
29 |
Correct |
160 ms |
860 KB |
Output is correct |
30 |
Correct |
147 ms |
836 KB |
Output is correct |
31 |
Correct |
169 ms |
916 KB |
Output is correct |
32 |
Correct |
149 ms |
912 KB |
Output is correct |
33 |
Correct |
161 ms |
832 KB |
Output is correct |
34 |
Correct |
144 ms |
872 KB |
Output is correct |
35 |
Correct |
161 ms |
896 KB |
Output is correct |
36 |
Correct |
170 ms |
892 KB |
Output is correct |
37 |
Correct |
155 ms |
852 KB |
Output is correct |
38 |
Correct |
155 ms |
872 KB |
Output is correct |
39 |
Correct |
163 ms |
908 KB |
Output is correct |
40 |
Correct |
159 ms |
940 KB |
Output is correct |
41 |
Correct |
161 ms |
952 KB |
Output is correct |
42 |
Correct |
166 ms |
872 KB |
Output is correct |
43 |
Correct |
153 ms |
856 KB |
Output is correct |
44 |
Correct |
158 ms |
848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
179 ms |
800 KB |
Output is correct |
2 |
Correct |
162 ms |
880 KB |
Output is correct |
3 |
Correct |
128 ms |
848 KB |
Output is correct |
4 |
Correct |
175 ms |
896 KB |
Output is correct |
5 |
Correct |
181 ms |
844 KB |
Output is correct |
6 |
Correct |
171 ms |
892 KB |
Output is correct |
7 |
Correct |
165 ms |
856 KB |
Output is correct |
8 |
Correct |
180 ms |
864 KB |
Output is correct |
9 |
Correct |
181 ms |
884 KB |
Output is correct |
10 |
Correct |
164 ms |
832 KB |
Output is correct |
11 |
Correct |
175 ms |
860 KB |
Output is correct |
12 |
Correct |
158 ms |
896 KB |
Output is correct |
13 |
Correct |
169 ms |
800 KB |
Output is correct |
14 |
Correct |
169 ms |
860 KB |
Output is correct |
15 |
Correct |
177 ms |
904 KB |
Output is correct |
16 |
Correct |
166 ms |
924 KB |
Output is correct |
17 |
Correct |
174 ms |
836 KB |
Output is correct |
18 |
Correct |
118 ms |
928 KB |
Output is correct |
19 |
Correct |
318 ms |
12984 KB |
Output is correct |
20 |
Correct |
363 ms |
15404 KB |
Output is correct |
21 |
Correct |
448 ms |
17792 KB |
Output is correct |
22 |
Correct |
343 ms |
16780 KB |
Output is correct |
23 |
Correct |
274 ms |
1908 KB |
Output is correct |
24 |
Correct |
315 ms |
1844 KB |
Output is correct |
25 |
Correct |
447 ms |
21952 KB |
Output is correct |
26 |
Correct |
391 ms |
18480 KB |
Output is correct |
27 |
Correct |
283 ms |
5096 KB |
Output is correct |
28 |
Correct |
288 ms |
5204 KB |
Output is correct |
29 |
Correct |
308 ms |
5096 KB |
Output is correct |
30 |
Correct |
264 ms |
1412 KB |
Output is correct |
31 |
Correct |
278 ms |
5160 KB |
Output is correct |
32 |
Correct |
315 ms |
5436 KB |
Output is correct |
33 |
Correct |
242 ms |
1752 KB |
Output is correct |
34 |
Correct |
351 ms |
18468 KB |
Output is correct |
35 |
Correct |
364 ms |
15248 KB |
Output is correct |
36 |
Correct |
251 ms |
1988 KB |
Output is correct |
37 |
Correct |
249 ms |
1876 KB |
Output is correct |
38 |
Correct |
236 ms |
15316 KB |
Output is correct |
39 |
Correct |
271 ms |
12776 KB |
Output is correct |
40 |
Correct |
361 ms |
13996 KB |
Output is correct |
41 |
Correct |
300 ms |
18416 KB |
Output is correct |
42 |
Correct |
165 ms |
18480 KB |
Output is correct |
43 |
Correct |
154 ms |
912 KB |
Output is correct |
44 |
Correct |
141 ms |
860 KB |
Output is correct |
45 |
Correct |
159 ms |
844 KB |
Output is correct |
46 |
Correct |
139 ms |
848 KB |
Output is correct |
47 |
Correct |
160 ms |
832 KB |
Output is correct |
48 |
Correct |
158 ms |
796 KB |
Output is correct |
49 |
Correct |
163 ms |
920 KB |
Output is correct |
50 |
Correct |
143 ms |
900 KB |
Output is correct |
51 |
Correct |
153 ms |
808 KB |
Output is correct |
52 |
Correct |
146 ms |
836 KB |
Output is correct |
53 |
Correct |
160 ms |
860 KB |
Output is correct |
54 |
Correct |
147 ms |
836 KB |
Output is correct |
55 |
Correct |
169 ms |
916 KB |
Output is correct |
56 |
Correct |
149 ms |
912 KB |
Output is correct |
57 |
Correct |
161 ms |
832 KB |
Output is correct |
58 |
Correct |
144 ms |
872 KB |
Output is correct |
59 |
Correct |
161 ms |
896 KB |
Output is correct |
60 |
Correct |
170 ms |
892 KB |
Output is correct |
61 |
Correct |
155 ms |
852 KB |
Output is correct |
62 |
Correct |
155 ms |
872 KB |
Output is correct |
63 |
Correct |
163 ms |
908 KB |
Output is correct |
64 |
Correct |
159 ms |
940 KB |
Output is correct |
65 |
Correct |
161 ms |
952 KB |
Output is correct |
66 |
Correct |
166 ms |
872 KB |
Output is correct |
67 |
Correct |
153 ms |
856 KB |
Output is correct |
68 |
Correct |
158 ms |
848 KB |
Output is correct |
69 |
Correct |
261 ms |
11940 KB |
Output is correct |
70 |
Correct |
275 ms |
15376 KB |
Output is correct |
71 |
Correct |
229 ms |
1904 KB |
Output is correct |
72 |
Correct |
228 ms |
1920 KB |
Output is correct |
73 |
Correct |
233 ms |
1828 KB |
Output is correct |
74 |
Correct |
329 ms |
16044 KB |
Output is correct |
75 |
Correct |
233 ms |
1864 KB |
Output is correct |
76 |
Correct |
408 ms |
21788 KB |
Output is correct |
77 |
Correct |
323 ms |
13580 KB |
Output is correct |
78 |
Correct |
220 ms |
1816 KB |
Output is correct |
79 |
Correct |
224 ms |
1936 KB |
Output is correct |
80 |
Correct |
341 ms |
14288 KB |
Output is correct |
81 |
Correct |
219 ms |
1824 KB |
Output is correct |
82 |
Correct |
431 ms |
21684 KB |
Output is correct |
83 |
Correct |
349 ms |
17384 KB |
Output is correct |
84 |
Correct |
237 ms |
1872 KB |
Output is correct |
85 |
Correct |
230 ms |
1840 KB |
Output is correct |
86 |
Correct |
294 ms |
12548 KB |
Output is correct |
87 |
Correct |
287 ms |
5196 KB |
Output is correct |
88 |
Correct |
212 ms |
1664 KB |
Output is correct |
89 |
Correct |
265 ms |
5628 KB |
Output is correct |
90 |
Correct |
301 ms |
6936 KB |
Output is correct |
91 |
Correct |
236 ms |
1532 KB |
Output is correct |
92 |
Correct |
378 ms |
15224 KB |
Output is correct |
93 |
Correct |
222 ms |
1820 KB |
Output is correct |
94 |
Correct |
220 ms |
1892 KB |
Output is correct |
95 |
Correct |
230 ms |
1884 KB |
Output is correct |
96 |
Correct |
148 ms |
4756 KB |
Output is correct |
97 |
Correct |
259 ms |
12704 KB |
Output is correct |
98 |
Correct |
369 ms |
13884 KB |
Output is correct |
99 |
Correct |
284 ms |
18444 KB |
Output is correct |
100 |
Correct |
143 ms |
18460 KB |
Output is correct |