#include <bits/stdc++.h>
using i64 = long long;
constexpr i64 mod1 = 1000000007;
constexpr i64 mod2 = 998244353;
using T = std::pair<i64, i64>;
T addm1(T a, T b) {
return {(a.first + b.first) % mod1, (a.second + b.second) % mod1};
}
T em() {
return {0, 0};
}
T mapm1(i64 a, T b) {
if (a == -1) return b;
b.first = a * b.second;
b.first %= mod1;
return b;
}
i64 compm1(i64 a, i64 b) {
if (a == -1) return b;
return a;
}
i64 id() {
return -1;
}
struct LazySegTreem1 {
int n, size, logn;
std::vector<T> node;
std::vector<i64> lazy;
void update(int i) {
node[i] = addm1(node[2 * i], node[2 * i + 1]);
}
void ang(int i, i64 f) {
node[i] = mapm1(f, node[i]);
if (i < size) {
lazy[i] = compm1(f, lazy[i]);
}
}
void push(int i) {
ang(2 * i, lazy[i]);
ang(2 * i + 1, lazy[i]);
lazy[i] = id();
}
LazySegTreem1() {}
LazySegTreem1(const std::vector<T> &vec) {
n = (int)vec.size();
logn = 0;
while ((1 << logn) < n) ++logn;
size = 1 << logn;
node.assign(2 * size, em());
lazy.assign(size, id());
std::copy(vec.begin(), vec.end(), node.begin() + size);
for (int i = size - 1; i >= 1; --i) {
update(i);
}
}
void apply(int l, int r, i64 f) {
l += size, r += size;
for (int d = logn; d >= 1; --d) {
if (((l >> d) << d) != l) push(l >> d);
if (((r >> d) << d) != r) push((r - 1) >> d);
}
int l2 = l, r2 = r;
while (l < r) {
if (l & 1) ang(l++, f);
if (r & 1) ang(--r, f);
l /= 2, r /= 2;
}
l = l2, r = r2;
for (int d = 1; d <= logn; ++d) {
if (((l >> d) << d) != l) update(l >> d);
if (((r >> d) << d) != r) update((r - 1) >> d);
}
}
T fold(int l, int r) {
l += size, r += size;
for (int d = logn; d >= 1; --d) {
if (((l >> d) << d) != l) push(l >> d);
if (((r >> d) << d) != r) push((r - 1) >> d);
}
T pL = em(), pR = em();
while (l < r) {
if (l & 1) pL = addm1(pL, node[l++]);
if (r & 1) pR = addm1(node[--r], pR);
l /= 2;
r /= 2;
}
return addm1(pL, pR);
}
};
std::string cross(const std::string &a, const std::string &b) {
std::string c;
c.reserve(a.size());
for (int i = 0; i < (int)a.size(); ++i) {
if (a[i] == b[i]) {
c.push_back(a[i]);
} else {
char x = 'J' xor 'O' xor 'I';
x ^= a[i];
x ^= b[i];
c.push_back(x);
}
}
return c;
}
constexpr i64 iHs = 7;
std::uniform_int_distribution<int> dist(0, 1000000000);
std::mt19937 mt(100);
std::array<i64, 3> h4 = {{999996919, 999976379, 999939407}};
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int N;
std::cin >> N;
std::vector<i64> powsm1(N);
powsm1[0] = 1;
for (int i = 1; i < N; ++i) {
powsm1[i] = (powsm1[i - 1] * iHs) % mod1;
}
std::string S1, S2, S3;
std::cin >> S1 >> S2 >> S3;
int Q;
std::cin >> Q;
std::string T0;
std::cin >> T0;
std::vector<int> L(Q), R(Q);
std::vector<char> C(Q);
for (int i = 0; i < Q; ++i) {
std::cin >> L[i] >> R[i] >> C[i];
--L[i];
}
std::set<std::string> flowers;
std::queue<std::string> ns;
ns.push(S1);
ns.push(S2);
ns.push(S3);
flowers.insert(S1);
flowers.insert(S2);
flowers.insert(S3);
while (not ns.empty()) {
const auto t = ns.front();
ns.pop();
auto nflowers = flowers;
for (const auto &q : flowers) {
const auto r = cross(t, q);
if (nflowers.find(r) == nflowers.end()) {
nflowers.insert(r);
ns.push(r);
}
}
flowers = nflowers;
}
const int M = (int)flowers.size();
assert(M <= 20);
std::vector<std::string> fs;
for (const auto &e : flowers) {
fs.push_back(e);
}
std::vector<LazySegTreem1> segm1s(M);
for (int x = 0; x < M; ++x) {
std::vector<T> initVecm1(N);
for (int i = 0; i < N; ++i) {
const auto v = (fs[x][i] == 'J' ? h4[0] : (fs[x][i] == 'O' ? h4[1] : h4[2]));
initVecm1[i] = {(v * powsm1[i]) % mod1, powsm1[i]};
}
segm1s[x] = LazySegTreem1(initVecm1);
}
std::vector<T> initVecm1(N);
for (int i = 0; i < N; ++i) {
const auto v = (T0[i] == 'J' ? h4[0] : (T0[i] == 'O' ? h4[1] : h4[2]));
initVecm1[i] = {(v * powsm1[i]) % mod1, powsm1[i]};
}
LazySegTreem1 iUsegm1(initVecm1);
std::cout << (flowers.find(T0) != flowers.end() ? "Yes" : "No") << std::endl;
for (int x = 0; x < Q; ++x) {
iUsegm1.apply(L[x], R[x], (C[x] == 'J' ? h4[0] : (C[x] == 'O' ? h4[1] : h4[2])));
bool answer = false;
for (int i = 0; i < M; ++i) {
bool b = iUsegm1.fold(0, N).first == segm1s[i].fold(0, N).first;
if (b) answer = true;
}
std::cout << (answer ? "Yes" : "No") << std::endl;
}
/*
std::vector<int> sameLimitL(M, N), sameLimitR(M, 0);
std::vector<std::vector<std::pair<int, int>>> pressInfo(M);
for (int x = 0; x < M; ++x) {
const auto &f = fs[x];
for (int i = 0; i < N; ++i) {
if (f[i] != T0[i]) {
sameLimitL[x] = i;
break;
}
}
for (int i = N - 1; i >= 0; --i) {
if (f[i] != T0[i]) {
sameLimitR[i] = i + 1;
break;
}
}
int lastI = 0;
for (int i = 1; i < N; ++i) {
if (fs[lastI] != fs[i]) {
pressInfo[x].push_back({lastI, i - lastI});
lastI = i;
}
}
pressInfo[x].push_back({lastI, N - lastI});
}
std::cout << (flowers.find(T0) != flowers.end() ? "Yes" : "No") << std::endl;
for (int i = 0; i < Q; ++i) {
bool answer = false;
for (int x = 0; x < M; ++x) {
// judge
if (sameLimitL[x] < L[i]) {
continue;
}
if (sameLimitR[x] > R[i]) {
continue;
}
auto itr = std::lower_bound(pressInfo[x].begin(), pressInfo[x].end(), std::make_pair(L[i], N + 1));
--itr;
if (itr->first + itr->second < R[i]) {
continue;
}
if (fs[x][L[i]] != C[i]) {
continue;
}
answer = true;
break;
}
std::cout << (answer ? "Yes" : "No") << std::endl;
}
*/
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
309 ms |
2744 KB |
Output is correct |
2 |
Correct |
356 ms |
2844 KB |
Output is correct |
3 |
Correct |
353 ms |
2888 KB |
Output is correct |
4 |
Correct |
330 ms |
2764 KB |
Output is correct |
5 |
Correct |
355 ms |
2876 KB |
Output is correct |
6 |
Correct |
312 ms |
2724 KB |
Output is correct |
7 |
Correct |
337 ms |
2972 KB |
Output is correct |
8 |
Correct |
347 ms |
2892 KB |
Output is correct |
9 |
Correct |
349 ms |
3016 KB |
Output is correct |
10 |
Correct |
341 ms |
2828 KB |
Output is correct |
11 |
Correct |
346 ms |
2960 KB |
Output is correct |
12 |
Correct |
339 ms |
2792 KB |
Output is correct |
13 |
Correct |
348 ms |
2844 KB |
Output is correct |
14 |
Correct |
341 ms |
2820 KB |
Output is correct |
15 |
Correct |
365 ms |
2784 KB |
Output is correct |
16 |
Correct |
454 ms |
2996 KB |
Output is correct |
17 |
Correct |
346 ms |
2840 KB |
Output is correct |
18 |
Correct |
345 ms |
2900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
309 ms |
2744 KB |
Output is correct |
2 |
Correct |
356 ms |
2844 KB |
Output is correct |
3 |
Correct |
353 ms |
2888 KB |
Output is correct |
4 |
Correct |
330 ms |
2764 KB |
Output is correct |
5 |
Correct |
355 ms |
2876 KB |
Output is correct |
6 |
Correct |
312 ms |
2724 KB |
Output is correct |
7 |
Correct |
337 ms |
2972 KB |
Output is correct |
8 |
Correct |
347 ms |
2892 KB |
Output is correct |
9 |
Correct |
349 ms |
3016 KB |
Output is correct |
10 |
Correct |
341 ms |
2828 KB |
Output is correct |
11 |
Correct |
346 ms |
2960 KB |
Output is correct |
12 |
Correct |
339 ms |
2792 KB |
Output is correct |
13 |
Correct |
348 ms |
2844 KB |
Output is correct |
14 |
Correct |
341 ms |
2820 KB |
Output is correct |
15 |
Correct |
365 ms |
2784 KB |
Output is correct |
16 |
Correct |
454 ms |
2996 KB |
Output is correct |
17 |
Correct |
346 ms |
2840 KB |
Output is correct |
18 |
Correct |
345 ms |
2900 KB |
Output is correct |
19 |
Correct |
566 ms |
29428 KB |
Output is correct |
20 |
Correct |
544 ms |
29388 KB |
Output is correct |
21 |
Correct |
528 ms |
28852 KB |
Output is correct |
22 |
Correct |
735 ms |
28228 KB |
Output is correct |
23 |
Correct |
421 ms |
4360 KB |
Output is correct |
24 |
Correct |
450 ms |
4364 KB |
Output is correct |
25 |
Correct |
483 ms |
29348 KB |
Output is correct |
26 |
Correct |
566 ms |
29316 KB |
Output is correct |
27 |
Correct |
542 ms |
29336 KB |
Output is correct |
28 |
Correct |
537 ms |
29212 KB |
Output is correct |
29 |
Correct |
542 ms |
29012 KB |
Output is correct |
30 |
Correct |
480 ms |
4260 KB |
Output is correct |
31 |
Correct |
546 ms |
29368 KB |
Output is correct |
32 |
Correct |
557 ms |
28672 KB |
Output is correct |
33 |
Correct |
444 ms |
4232 KB |
Output is correct |
34 |
Correct |
507 ms |
29268 KB |
Output is correct |
35 |
Correct |
457 ms |
27576 KB |
Output is correct |
36 |
Correct |
540 ms |
4196 KB |
Output is correct |
37 |
Correct |
434 ms |
4260 KB |
Output is correct |
38 |
Correct |
551 ms |
29308 KB |
Output is correct |
39 |
Correct |
485 ms |
29360 KB |
Output is correct |
40 |
Correct |
470 ms |
16992 KB |
Output is correct |
41 |
Correct |
564 ms |
29528 KB |
Output is correct |
42 |
Correct |
424 ms |
29512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
309 ms |
2744 KB |
Output is correct |
2 |
Correct |
356 ms |
2844 KB |
Output is correct |
3 |
Correct |
353 ms |
2888 KB |
Output is correct |
4 |
Correct |
330 ms |
2764 KB |
Output is correct |
5 |
Correct |
355 ms |
2876 KB |
Output is correct |
6 |
Correct |
312 ms |
2724 KB |
Output is correct |
7 |
Correct |
337 ms |
2972 KB |
Output is correct |
8 |
Correct |
347 ms |
2892 KB |
Output is correct |
9 |
Correct |
349 ms |
3016 KB |
Output is correct |
10 |
Correct |
341 ms |
2828 KB |
Output is correct |
11 |
Correct |
346 ms |
2960 KB |
Output is correct |
12 |
Correct |
339 ms |
2792 KB |
Output is correct |
13 |
Correct |
348 ms |
2844 KB |
Output is correct |
14 |
Correct |
341 ms |
2820 KB |
Output is correct |
15 |
Correct |
365 ms |
2784 KB |
Output is correct |
16 |
Correct |
454 ms |
2996 KB |
Output is correct |
17 |
Correct |
346 ms |
2840 KB |
Output is correct |
18 |
Correct |
345 ms |
2900 KB |
Output is correct |
19 |
Correct |
495 ms |
2836 KB |
Output is correct |
20 |
Correct |
486 ms |
2892 KB |
Output is correct |
21 |
Correct |
373 ms |
2900 KB |
Output is correct |
22 |
Correct |
363 ms |
2828 KB |
Output is correct |
23 |
Correct |
390 ms |
2892 KB |
Output is correct |
24 |
Correct |
414 ms |
2808 KB |
Output is correct |
25 |
Correct |
392 ms |
2896 KB |
Output is correct |
26 |
Correct |
432 ms |
2824 KB |
Output is correct |
27 |
Correct |
399 ms |
2956 KB |
Output is correct |
28 |
Correct |
382 ms |
2664 KB |
Output is correct |
29 |
Correct |
438 ms |
2788 KB |
Output is correct |
30 |
Correct |
466 ms |
2660 KB |
Output is correct |
31 |
Correct |
562 ms |
2904 KB |
Output is correct |
32 |
Correct |
584 ms |
2924 KB |
Output is correct |
33 |
Correct |
552 ms |
2896 KB |
Output is correct |
34 |
Correct |
477 ms |
2764 KB |
Output is correct |
35 |
Correct |
577 ms |
2904 KB |
Output is correct |
36 |
Correct |
573 ms |
2848 KB |
Output is correct |
37 |
Correct |
525 ms |
2924 KB |
Output is correct |
38 |
Correct |
681 ms |
3160 KB |
Output is correct |
39 |
Correct |
594 ms |
2856 KB |
Output is correct |
40 |
Correct |
576 ms |
3036 KB |
Output is correct |
41 |
Correct |
540 ms |
2904 KB |
Output is correct |
42 |
Correct |
761 ms |
2896 KB |
Output is correct |
43 |
Correct |
567 ms |
2920 KB |
Output is correct |
44 |
Correct |
516 ms |
2900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
309 ms |
2744 KB |
Output is correct |
2 |
Correct |
356 ms |
2844 KB |
Output is correct |
3 |
Correct |
353 ms |
2888 KB |
Output is correct |
4 |
Correct |
330 ms |
2764 KB |
Output is correct |
5 |
Correct |
355 ms |
2876 KB |
Output is correct |
6 |
Correct |
312 ms |
2724 KB |
Output is correct |
7 |
Correct |
337 ms |
2972 KB |
Output is correct |
8 |
Correct |
347 ms |
2892 KB |
Output is correct |
9 |
Correct |
349 ms |
3016 KB |
Output is correct |
10 |
Correct |
341 ms |
2828 KB |
Output is correct |
11 |
Correct |
346 ms |
2960 KB |
Output is correct |
12 |
Correct |
339 ms |
2792 KB |
Output is correct |
13 |
Correct |
348 ms |
2844 KB |
Output is correct |
14 |
Correct |
341 ms |
2820 KB |
Output is correct |
15 |
Correct |
365 ms |
2784 KB |
Output is correct |
16 |
Correct |
454 ms |
2996 KB |
Output is correct |
17 |
Correct |
346 ms |
2840 KB |
Output is correct |
18 |
Correct |
345 ms |
2900 KB |
Output is correct |
19 |
Correct |
566 ms |
29428 KB |
Output is correct |
20 |
Correct |
544 ms |
29388 KB |
Output is correct |
21 |
Correct |
528 ms |
28852 KB |
Output is correct |
22 |
Correct |
735 ms |
28228 KB |
Output is correct |
23 |
Correct |
421 ms |
4360 KB |
Output is correct |
24 |
Correct |
450 ms |
4364 KB |
Output is correct |
25 |
Correct |
483 ms |
29348 KB |
Output is correct |
26 |
Correct |
566 ms |
29316 KB |
Output is correct |
27 |
Correct |
542 ms |
29336 KB |
Output is correct |
28 |
Correct |
537 ms |
29212 KB |
Output is correct |
29 |
Correct |
542 ms |
29012 KB |
Output is correct |
30 |
Correct |
480 ms |
4260 KB |
Output is correct |
31 |
Correct |
546 ms |
29368 KB |
Output is correct |
32 |
Correct |
557 ms |
28672 KB |
Output is correct |
33 |
Correct |
444 ms |
4232 KB |
Output is correct |
34 |
Correct |
507 ms |
29268 KB |
Output is correct |
35 |
Correct |
457 ms |
27576 KB |
Output is correct |
36 |
Correct |
540 ms |
4196 KB |
Output is correct |
37 |
Correct |
434 ms |
4260 KB |
Output is correct |
38 |
Correct |
551 ms |
29308 KB |
Output is correct |
39 |
Correct |
485 ms |
29360 KB |
Output is correct |
40 |
Correct |
470 ms |
16992 KB |
Output is correct |
41 |
Correct |
564 ms |
29528 KB |
Output is correct |
42 |
Correct |
424 ms |
29512 KB |
Output is correct |
43 |
Correct |
495 ms |
2836 KB |
Output is correct |
44 |
Correct |
486 ms |
2892 KB |
Output is correct |
45 |
Correct |
373 ms |
2900 KB |
Output is correct |
46 |
Correct |
363 ms |
2828 KB |
Output is correct |
47 |
Correct |
390 ms |
2892 KB |
Output is correct |
48 |
Correct |
414 ms |
2808 KB |
Output is correct |
49 |
Correct |
392 ms |
2896 KB |
Output is correct |
50 |
Correct |
432 ms |
2824 KB |
Output is correct |
51 |
Correct |
399 ms |
2956 KB |
Output is correct |
52 |
Correct |
382 ms |
2664 KB |
Output is correct |
53 |
Correct |
438 ms |
2788 KB |
Output is correct |
54 |
Correct |
466 ms |
2660 KB |
Output is correct |
55 |
Correct |
562 ms |
2904 KB |
Output is correct |
56 |
Correct |
584 ms |
2924 KB |
Output is correct |
57 |
Correct |
552 ms |
2896 KB |
Output is correct |
58 |
Correct |
477 ms |
2764 KB |
Output is correct |
59 |
Correct |
577 ms |
2904 KB |
Output is correct |
60 |
Correct |
573 ms |
2848 KB |
Output is correct |
61 |
Correct |
525 ms |
2924 KB |
Output is correct |
62 |
Correct |
681 ms |
3160 KB |
Output is correct |
63 |
Correct |
594 ms |
2856 KB |
Output is correct |
64 |
Correct |
576 ms |
3036 KB |
Output is correct |
65 |
Correct |
540 ms |
2904 KB |
Output is correct |
66 |
Correct |
761 ms |
2896 KB |
Output is correct |
67 |
Correct |
567 ms |
2920 KB |
Output is correct |
68 |
Correct |
516 ms |
2900 KB |
Output is correct |
69 |
Correct |
1228 ms |
112912 KB |
Output is correct |
70 |
Correct |
1160 ms |
114984 KB |
Output is correct |
71 |
Correct |
546 ms |
5692 KB |
Output is correct |
72 |
Correct |
524 ms |
5716 KB |
Output is correct |
73 |
Correct |
682 ms |
5672 KB |
Output is correct |
74 |
Correct |
640 ms |
49248 KB |
Output is correct |
75 |
Correct |
557 ms |
5576 KB |
Output is correct |
76 |
Correct |
656 ms |
50548 KB |
Output is correct |
77 |
Correct |
644 ms |
49344 KB |
Output is correct |
78 |
Correct |
516 ms |
5524 KB |
Output is correct |
79 |
Correct |
607 ms |
5676 KB |
Output is correct |
80 |
Correct |
1276 ms |
111840 KB |
Output is correct |
81 |
Correct |
967 ms |
9580 KB |
Output is correct |
82 |
Correct |
1193 ms |
114716 KB |
Output is correct |
83 |
Correct |
1245 ms |
114684 KB |
Output is correct |
84 |
Correct |
872 ms |
10040 KB |
Output is correct |
85 |
Correct |
847 ms |
10120 KB |
Output is correct |
86 |
Correct |
1260 ms |
112960 KB |
Output is correct |
87 |
Correct |
1166 ms |
115504 KB |
Output is correct |
88 |
Correct |
918 ms |
10108 KB |
Output is correct |
89 |
Correct |
1210 ms |
113804 KB |
Output is correct |
90 |
Correct |
1088 ms |
115428 KB |
Output is correct |
91 |
Correct |
823 ms |
10392 KB |
Output is correct |
92 |
Correct |
1135 ms |
113080 KB |
Output is correct |
93 |
Correct |
882 ms |
10484 KB |
Output is correct |
94 |
Correct |
827 ms |
10404 KB |
Output is correct |
95 |
Correct |
863 ms |
10404 KB |
Output is correct |
96 |
Correct |
512 ms |
30104 KB |
Output is correct |
97 |
Correct |
1170 ms |
115680 KB |
Output is correct |
98 |
Correct |
768 ms |
60904 KB |
Output is correct |
99 |
Correct |
1273 ms |
115572 KB |
Output is correct |
100 |
Correct |
1134 ms |
115980 KB |
Output is correct |