//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;
void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << "'" << x << "'";}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#define int long long
typedef pair<int, int> T;
const int oo = 1e18, oo2 = 1e9+7, K = 30;
const int mod = 998244353;
void add(int &a, int b){
a += b;
if (a >= mod) a-=mod;
}
int add2(int a, int b){
a += b;
if (a >= mod) a-=mod;
return a;
}
void sub(int &a, int b){
a -= b;
if (a < 0) a += mod;
}
int mul(int a, int b){
return (a*b)%mod;
}
T f(T a, T b){
return T{add2(a.first, b.first), add2(a.second, b.second)};
}
mt19937 rng(chrono::steady_clock().now().time_since_epoch().count());
int p(int a, int b){
return a + rng()%(b-a+1);
}
set<vector<int>>now;
vector<int>A, B, C;
int change(char c){
if (c == 'J') return 0;
else if (c == 'O') return 1;
return 2;
}
int sub2(int a, int b){
sub(a, b);
return a;
}
T fsub(T a, T b){
return {sub2(a.first, b.first), sub2(a.second, b.second)};
}
void gen(string &s, vector<int>&a){
for (auto c: s){
a.emplace_back(change(c));
}
}
vector<int> merge(vector<int>&a, vector<int>&b){
vector<int>ret;
for (int i = 0; i<(int)a.size(); i++){
ret.emplace_back((2*a[i] + 2*b[i])%3);
}
return ret;
}
struct Q{
int l, r, c;
T h;
Q(){}
Q(int _l, int _r, T _h, int _c): l(_l), r(_r), h(_h), c(_c) {}
bool operator <(const Q&he) const {
return l < he.l;
}
};
void rec(vector<int>s){
if (now.find(s) != now.end()) return;
now.insert(s);
for (auto t: {A, B, C}){
rec(merge(s, t));
}
}
void solve(){
int n; cin >> n;
string a, b, c; cin >> a >> b >> c;
gen(a, A);
gen(b, B);
gen(c, C);
for (auto s: {A, B, C}) rec(s);
assert((int)now.size() <= 9);
vector<T>Hash;
int b1 = p(1, mod-1), b2 = p(1, mod-1);
for (auto vt: now){
T H = {0, 0}, p = {1, 1};
for (auto ch: vt){
H = f(H, {mul(ch, p.first), mul(ch, p.second)});
p = {mul(p.first, b1), mul(p.second, b2)};
}
Hash.emplace_back(H);
}
int q; cin >> q >> a;
A.clear(); gen(a, A);
set<Q>blocks;
int begin = 0;
T sum = {0, 0}, all = {0, 0}, p = {1, 1};
for (int i = 0; i<n; i++){
if (i && A[i] != A[i-1]){
blocks.insert({begin, i-1, sum, A[i-1]});
begin = i;
all = f(all, sum);
sum = {0, 0};
}
sum = f(sum, {mul(A[i], p.first), mul(A[i], p.second)});
p = {mul(p.first, b1), mul(p.second, b2)};
}
blocks.insert({begin, n-1, sum, A.back()});
all = f(all, sum);
// for (auto [l, r, h, c]: blocks){
// debug(l, r, h, c);
// }
auto answer = [&](){
for (auto x: Hash){
if (x == all){
cout << "Yes\n";
return;
}
}
cout << "No\n";
};
debug(all);
answer();
vector<int>power1(n+1), power2(n+1);
vector<int>pref1(n+1), pref2(n+1);
power1[0] = power2[0] = 1;
pref1[0] = pref2[0] = 1;
for (int i = 1; i<=n; i++){
power1[i] = mul(power1[i-1], b1);
power2[i] = mul(power2[i-1], b2);
pref1[i] = add2(pref1[i-1], power1[i]);
pref2[i] = add2(pref2[i-1], power2[i]);
}
auto get_sum = [&](int l, int r, int v) {
if (r < l) return T{0ll, 0ll};
return T{mul(power1[l], mul(v, pref1[r-l])), mul(power2[l], mul(v, pref2[r-l]))};
};
vector<int>fix;
while (q--){
int l, r; char d; cin >> l >> r >> d;
--l;--r;
debug(l, r, d);
int what = change(d);
auto wrr = prev(blocks.upper_bound({l, -oo, {-oo, -oo}, -oo}));
fix.clear();
pair<Q, Q> tmp;
tmp.first.l = -1;
tmp.second.l = -1;
for (auto it = wrr; it != blocks.end() && it->l <= r; it++){
auto [ll, rr, val, hh] = *it;
fix.emplace_back(ll);
// debug(ll, rr, val, hh);
if (ll >= l && rr <= r){
all = fsub(all, hh);
} else {
//at most two such intervals
bool sub = 0;
if (ll < l){
tmp.first = {ll, l-1, get_sum(ll, l-1, val), val};
all = fsub(all, get_sum(l, min(rr, r), val));
sub = 1;
}
if (rr > r){
tmp.second = Q{r+1, rr, get_sum(r+1, rr, val), val};
if (!sub){
all = fsub(all, get_sum(max(ll, l), r, val));
}
}
}
}
debug(all);
for (auto x: fix){
blocks.erase(blocks.lower_bound({x, -oo, {-oo, -oo}, -oo}));
}
blocks.insert({l, r, get_sum(l, r, what), what});
all = f(all, get_sum(l, r, what));
if (tmp.first.l != -1) blocks.insert(tmp.first);
if (tmp.second.l != -1) blocks.insert(tmp.second);
// for (auto [l, r, val, hh]: blocks){
// debug(l, r, val, hh);
// }
debug(all);
answer();
}
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
Compilation message
Main.cpp: In constructor 'Q::Q(long long int, long long int, T, long long int)':
Main.cpp:95:7: warning: 'Q::h' will be initialized after [-Wreorder]
95 | T h;
| ^
Main.cpp:94:15: warning: 'long long int Q::c' [-Wreorder]
94 | int l, r, c;
| ^
Main.cpp:97:5: warning: when initialized here [-Wreorder]
97 | Q(int _l, int _r, T _h, int _c): l(_l), r(_r), h(_h), c(_c) {}
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
117 ms |
772 KB |
Output is correct |
2 |
Correct |
122 ms |
928 KB |
Output is correct |
3 |
Correct |
99 ms |
904 KB |
Output is correct |
4 |
Correct |
110 ms |
848 KB |
Output is correct |
5 |
Correct |
111 ms |
1052 KB |
Output is correct |
6 |
Correct |
113 ms |
1100 KB |
Output is correct |
7 |
Correct |
126 ms |
1064 KB |
Output is correct |
8 |
Correct |
117 ms |
1088 KB |
Output is correct |
9 |
Correct |
125 ms |
1256 KB |
Output is correct |
10 |
Correct |
120 ms |
1160 KB |
Output is correct |
11 |
Correct |
124 ms |
1120 KB |
Output is correct |
12 |
Correct |
136 ms |
1204 KB |
Output is correct |
13 |
Correct |
119 ms |
1184 KB |
Output is correct |
14 |
Correct |
115 ms |
1092 KB |
Output is correct |
15 |
Correct |
118 ms |
1104 KB |
Output is correct |
16 |
Correct |
121 ms |
1164 KB |
Output is correct |
17 |
Correct |
120 ms |
1224 KB |
Output is correct |
18 |
Correct |
86 ms |
1120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
117 ms |
772 KB |
Output is correct |
2 |
Correct |
122 ms |
928 KB |
Output is correct |
3 |
Correct |
99 ms |
904 KB |
Output is correct |
4 |
Correct |
110 ms |
848 KB |
Output is correct |
5 |
Correct |
111 ms |
1052 KB |
Output is correct |
6 |
Correct |
113 ms |
1100 KB |
Output is correct |
7 |
Correct |
126 ms |
1064 KB |
Output is correct |
8 |
Correct |
117 ms |
1088 KB |
Output is correct |
9 |
Correct |
125 ms |
1256 KB |
Output is correct |
10 |
Correct |
120 ms |
1160 KB |
Output is correct |
11 |
Correct |
124 ms |
1120 KB |
Output is correct |
12 |
Correct |
136 ms |
1204 KB |
Output is correct |
13 |
Correct |
119 ms |
1184 KB |
Output is correct |
14 |
Correct |
115 ms |
1092 KB |
Output is correct |
15 |
Correct |
118 ms |
1104 KB |
Output is correct |
16 |
Correct |
121 ms |
1164 KB |
Output is correct |
17 |
Correct |
120 ms |
1224 KB |
Output is correct |
18 |
Correct |
86 ms |
1120 KB |
Output is correct |
19 |
Correct |
256 ms |
25340 KB |
Output is correct |
20 |
Correct |
226 ms |
26668 KB |
Output is correct |
21 |
Correct |
372 ms |
25340 KB |
Output is correct |
22 |
Correct |
322 ms |
22956 KB |
Output is correct |
23 |
Correct |
216 ms |
2584 KB |
Output is correct |
24 |
Correct |
186 ms |
2524 KB |
Output is correct |
25 |
Correct |
366 ms |
27884 KB |
Output is correct |
26 |
Correct |
348 ms |
28008 KB |
Output is correct |
27 |
Correct |
214 ms |
25152 KB |
Output is correct |
28 |
Correct |
240 ms |
25168 KB |
Output is correct |
29 |
Correct |
234 ms |
24504 KB |
Output is correct |
30 |
Correct |
162 ms |
2076 KB |
Output is correct |
31 |
Correct |
213 ms |
25160 KB |
Output is correct |
32 |
Correct |
222 ms |
23160 KB |
Output is correct |
33 |
Correct |
170 ms |
2396 KB |
Output is correct |
34 |
Correct |
273 ms |
27768 KB |
Output is correct |
35 |
Correct |
344 ms |
20552 KB |
Output is correct |
36 |
Correct |
176 ms |
2508 KB |
Output is correct |
37 |
Correct |
178 ms |
2504 KB |
Output is correct |
38 |
Correct |
176 ms |
26800 KB |
Output is correct |
39 |
Correct |
256 ms |
25132 KB |
Output is correct |
40 |
Correct |
321 ms |
18340 KB |
Output is correct |
41 |
Correct |
237 ms |
27940 KB |
Output is correct |
42 |
Correct |
125 ms |
28016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
117 ms |
772 KB |
Output is correct |
2 |
Correct |
122 ms |
928 KB |
Output is correct |
3 |
Correct |
99 ms |
904 KB |
Output is correct |
4 |
Correct |
110 ms |
848 KB |
Output is correct |
5 |
Correct |
111 ms |
1052 KB |
Output is correct |
6 |
Correct |
113 ms |
1100 KB |
Output is correct |
7 |
Correct |
126 ms |
1064 KB |
Output is correct |
8 |
Correct |
117 ms |
1088 KB |
Output is correct |
9 |
Correct |
125 ms |
1256 KB |
Output is correct |
10 |
Correct |
120 ms |
1160 KB |
Output is correct |
11 |
Correct |
124 ms |
1120 KB |
Output is correct |
12 |
Correct |
136 ms |
1204 KB |
Output is correct |
13 |
Correct |
119 ms |
1184 KB |
Output is correct |
14 |
Correct |
115 ms |
1092 KB |
Output is correct |
15 |
Correct |
118 ms |
1104 KB |
Output is correct |
16 |
Correct |
121 ms |
1164 KB |
Output is correct |
17 |
Correct |
120 ms |
1224 KB |
Output is correct |
18 |
Correct |
86 ms |
1120 KB |
Output is correct |
19 |
Correct |
118 ms |
1128 KB |
Output is correct |
20 |
Correct |
91 ms |
1204 KB |
Output is correct |
21 |
Correct |
113 ms |
1108 KB |
Output is correct |
22 |
Correct |
105 ms |
1048 KB |
Output is correct |
23 |
Correct |
111 ms |
1168 KB |
Output is correct |
24 |
Correct |
110 ms |
1096 KB |
Output is correct |
25 |
Correct |
110 ms |
1080 KB |
Output is correct |
26 |
Correct |
106 ms |
1100 KB |
Output is correct |
27 |
Correct |
109 ms |
1156 KB |
Output is correct |
28 |
Correct |
102 ms |
1128 KB |
Output is correct |
29 |
Correct |
121 ms |
1220 KB |
Output is correct |
30 |
Correct |
104 ms |
1184 KB |
Output is correct |
31 |
Correct |
117 ms |
1152 KB |
Output is correct |
32 |
Correct |
109 ms |
1176 KB |
Output is correct |
33 |
Correct |
117 ms |
1112 KB |
Output is correct |
34 |
Correct |
108 ms |
1072 KB |
Output is correct |
35 |
Correct |
110 ms |
1100 KB |
Output is correct |
36 |
Correct |
116 ms |
1100 KB |
Output is correct |
37 |
Correct |
113 ms |
1216 KB |
Output is correct |
38 |
Correct |
119 ms |
1120 KB |
Output is correct |
39 |
Correct |
112 ms |
1100 KB |
Output is correct |
40 |
Correct |
116 ms |
1108 KB |
Output is correct |
41 |
Correct |
117 ms |
1212 KB |
Output is correct |
42 |
Correct |
117 ms |
1200 KB |
Output is correct |
43 |
Correct |
114 ms |
1108 KB |
Output is correct |
44 |
Correct |
118 ms |
1200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
117 ms |
772 KB |
Output is correct |
2 |
Correct |
122 ms |
928 KB |
Output is correct |
3 |
Correct |
99 ms |
904 KB |
Output is correct |
4 |
Correct |
110 ms |
848 KB |
Output is correct |
5 |
Correct |
111 ms |
1052 KB |
Output is correct |
6 |
Correct |
113 ms |
1100 KB |
Output is correct |
7 |
Correct |
126 ms |
1064 KB |
Output is correct |
8 |
Correct |
117 ms |
1088 KB |
Output is correct |
9 |
Correct |
125 ms |
1256 KB |
Output is correct |
10 |
Correct |
120 ms |
1160 KB |
Output is correct |
11 |
Correct |
124 ms |
1120 KB |
Output is correct |
12 |
Correct |
136 ms |
1204 KB |
Output is correct |
13 |
Correct |
119 ms |
1184 KB |
Output is correct |
14 |
Correct |
115 ms |
1092 KB |
Output is correct |
15 |
Correct |
118 ms |
1104 KB |
Output is correct |
16 |
Correct |
121 ms |
1164 KB |
Output is correct |
17 |
Correct |
120 ms |
1224 KB |
Output is correct |
18 |
Correct |
86 ms |
1120 KB |
Output is correct |
19 |
Correct |
256 ms |
25340 KB |
Output is correct |
20 |
Correct |
226 ms |
26668 KB |
Output is correct |
21 |
Correct |
372 ms |
25340 KB |
Output is correct |
22 |
Correct |
322 ms |
22956 KB |
Output is correct |
23 |
Correct |
216 ms |
2584 KB |
Output is correct |
24 |
Correct |
186 ms |
2524 KB |
Output is correct |
25 |
Correct |
366 ms |
27884 KB |
Output is correct |
26 |
Correct |
348 ms |
28008 KB |
Output is correct |
27 |
Correct |
214 ms |
25152 KB |
Output is correct |
28 |
Correct |
240 ms |
25168 KB |
Output is correct |
29 |
Correct |
234 ms |
24504 KB |
Output is correct |
30 |
Correct |
162 ms |
2076 KB |
Output is correct |
31 |
Correct |
213 ms |
25160 KB |
Output is correct |
32 |
Correct |
222 ms |
23160 KB |
Output is correct |
33 |
Correct |
170 ms |
2396 KB |
Output is correct |
34 |
Correct |
273 ms |
27768 KB |
Output is correct |
35 |
Correct |
344 ms |
20552 KB |
Output is correct |
36 |
Correct |
176 ms |
2508 KB |
Output is correct |
37 |
Correct |
178 ms |
2504 KB |
Output is correct |
38 |
Correct |
176 ms |
26800 KB |
Output is correct |
39 |
Correct |
256 ms |
25132 KB |
Output is correct |
40 |
Correct |
321 ms |
18340 KB |
Output is correct |
41 |
Correct |
237 ms |
27940 KB |
Output is correct |
42 |
Correct |
125 ms |
28016 KB |
Output is correct |
43 |
Correct |
118 ms |
1128 KB |
Output is correct |
44 |
Correct |
91 ms |
1204 KB |
Output is correct |
45 |
Correct |
113 ms |
1108 KB |
Output is correct |
46 |
Correct |
105 ms |
1048 KB |
Output is correct |
47 |
Correct |
111 ms |
1168 KB |
Output is correct |
48 |
Correct |
110 ms |
1096 KB |
Output is correct |
49 |
Correct |
110 ms |
1080 KB |
Output is correct |
50 |
Correct |
106 ms |
1100 KB |
Output is correct |
51 |
Correct |
109 ms |
1156 KB |
Output is correct |
52 |
Correct |
102 ms |
1128 KB |
Output is correct |
53 |
Correct |
121 ms |
1220 KB |
Output is correct |
54 |
Correct |
104 ms |
1184 KB |
Output is correct |
55 |
Correct |
117 ms |
1152 KB |
Output is correct |
56 |
Correct |
109 ms |
1176 KB |
Output is correct |
57 |
Correct |
117 ms |
1112 KB |
Output is correct |
58 |
Correct |
108 ms |
1072 KB |
Output is correct |
59 |
Correct |
110 ms |
1100 KB |
Output is correct |
60 |
Correct |
116 ms |
1100 KB |
Output is correct |
61 |
Correct |
113 ms |
1216 KB |
Output is correct |
62 |
Correct |
119 ms |
1120 KB |
Output is correct |
63 |
Correct |
112 ms |
1100 KB |
Output is correct |
64 |
Correct |
116 ms |
1108 KB |
Output is correct |
65 |
Correct |
117 ms |
1212 KB |
Output is correct |
66 |
Correct |
117 ms |
1200 KB |
Output is correct |
67 |
Correct |
114 ms |
1108 KB |
Output is correct |
68 |
Correct |
118 ms |
1200 KB |
Output is correct |
69 |
Correct |
273 ms |
86164 KB |
Output is correct |
70 |
Correct |
263 ms |
100500 KB |
Output is correct |
71 |
Correct |
174 ms |
2924 KB |
Output is correct |
72 |
Correct |
178 ms |
3192 KB |
Output is correct |
73 |
Correct |
174 ms |
3068 KB |
Output is correct |
74 |
Correct |
338 ms |
37576 KB |
Output is correct |
75 |
Correct |
182 ms |
3144 KB |
Output is correct |
76 |
Correct |
414 ms |
44128 KB |
Output is correct |
77 |
Correct |
323 ms |
37616 KB |
Output is correct |
78 |
Correct |
170 ms |
3028 KB |
Output is correct |
79 |
Correct |
178 ms |
3108 KB |
Output is correct |
80 |
Correct |
318 ms |
72348 KB |
Output is correct |
81 |
Correct |
190 ms |
5976 KB |
Output is correct |
82 |
Correct |
424 ms |
100492 KB |
Output is correct |
83 |
Correct |
368 ms |
95064 KB |
Output is correct |
84 |
Correct |
174 ms |
6056 KB |
Output is correct |
85 |
Correct |
176 ms |
6152 KB |
Output is correct |
86 |
Correct |
263 ms |
76960 KB |
Output is correct |
87 |
Correct |
296 ms |
100396 KB |
Output is correct |
88 |
Correct |
165 ms |
6224 KB |
Output is correct |
89 |
Correct |
274 ms |
90020 KB |
Output is correct |
90 |
Correct |
299 ms |
100568 KB |
Output is correct |
91 |
Correct |
174 ms |
6060 KB |
Output is correct |
92 |
Correct |
371 ms |
75828 KB |
Output is correct |
93 |
Correct |
179 ms |
6120 KB |
Output is correct |
94 |
Correct |
179 ms |
6064 KB |
Output is correct |
95 |
Correct |
184 ms |
6084 KB |
Output is correct |
96 |
Correct |
131 ms |
24972 KB |
Output is correct |
97 |
Correct |
321 ms |
100532 KB |
Output is correct |
98 |
Correct |
367 ms |
66120 KB |
Output is correct |
99 |
Correct |
284 ms |
100520 KB |
Output is correct |
100 |
Correct |
191 ms |
100436 KB |
Output is correct |