Submission #422148

# Submission time Handle Problem Language Result Execution time Memory
422148 2021-06-09T18:54:59 Z valerikk Crossing (JOI21_crossing) C++17
100 / 100
4665 ms 55008 KB
#include <bits/stdc++.h>

using namespace std;

struct Query {
	int l, r, c;
};

int get_id(char c) {
	if (c == 'J') return 0;
	if (c == 'O') return 1;
	if (c == 'I') return 2;
	assert(0);
	return -1;
}

vector<int> read(int n) {
	string s;
	cin >> s;
	vector<int> res(n);
	for (int i = 0; i < n; ++i) res[i] = get_id(s[i]);
	return res;
}

int n, q;
vector<vector<int>> strs;
vector<int> t;
vector<Query> queries;
vector<vector<int>> pref;
set<pair<pair<int, int>, int>> st;
int cnt_good;
vector<vector<int>> coefs = {{0, 0, 1},
                                   {0, 1, 0},
                                   {1, 0, 0},
                                   {0, 2, 2},
                                   {2, 0, 2},
                                   {0, 2, 2},
                                   {2, 2, 0},
                                   {2, 0, 2},
                                   {2, 2, 0},
                                   {1, 1, 2},
                                   {1, 1, 2},
                                   {1, 2, 1},
                                   {1, 2, 1},
                                   {2, 1, 1},
                                   {2, 1, 1},
                                   {2, 1, 1},
                                   {1, 1, 2},
                                   {1, 2, 1},
                                   {1, 1, 2},
                                   {1, 2, 1},
                                   {1, 2, 1},
                                   {1, 1, 2},
                                   {1, 1, 2},
                                   {2, 1, 1},
                                   {2, 1, 1},
                                   {2, 1, 1},
                                   {1, 1, 2},
                                   {1, 2, 1},
                                   {1, 1, 2},
                                   {1, 2, 1},
                                   {1, 1, 2},
                                   {1, 2, 1},
                                   {2, 1, 1},
                                   {1, 2, 1},
                                   {2, 1, 1},
                                   {1, 2, 1},
                                   {1, 1, 2},
                                   {1, 1, 2},
                                   {2, 1, 1},
                                   {2, 1, 1},
                                   {1, 1, 2},
                                   {1, 2, 1},
                                   {2, 1, 1},
                                   {1, 2, 1},
                                   {2, 1, 1}};

vector<int> str_add(vector<int> a, vector<int> b) {
	vector<int> res(n);
	for (int i = 0; i < n; ++i) res[i] = (6 + -a[i] - b[i]) % 3;
	return res;
}

bool check(int l, int r, int c) {
	return pref[r][c] - pref[l][c] == r - l;
}

void insert(int l, int r, int c) {
	st.insert({{l, r}, c});
	if (check(l, r, c)) ++cnt_good;
}

void erase(int l, int r, int c) {
	st.erase({{l, r}, c});
	if (check(l, r, c)) --cnt_good;
}

int main() {
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	for (int i = 0; i < 3; ++i) strs.push_back(read(n));

	{
		sort(coefs.begin(), coefs.end());
		coefs.erase(unique(coefs.begin(), coefs.end()), coefs.end());
		vector<vector<int>> nstrs;
		for (auto v : coefs) {
			vector<int> cur(n);
			for (int i = 0; i < n; ++i) cur[i] = (strs[0][i] * v[0] + strs[1][i] * v[1] + strs[2][i] * v[2]) % 3;
			nstrs.push_back(cur);
		}
		strs = nstrs;
		sort(strs.begin(), strs.end());
		strs.erase(unique(strs.begin(), strs.end()), strs.end());
	}

	cin >> q;
	t = read(n);
	queries.resize(q);
	for (int i = 0; i < q; ++i) {
		cin >> queries[i].l >> queries[i].r;
		--queries[i].l;
		char c;
		cin >> c;
		queries[i].c = get_id(c);
	}
	vector<int> ans(q + 1, 0);

	for (auto s : strs) {
		pref.assign(n + 1, vector<int>(3, 0));
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < 3; ++j) pref[i + 1][j] = pref[i][j] + (s[i] == j);
		}
		st = {};
		cnt_good = 0;
		for (int i = 0; i < n; ++i) insert(i, i + 1, t[i]);
		for (int i = 0; i <= q; ++i) {
			ans[i] |= cnt_good == (int)st.size();
			if (i != q) {
				int l = queries[i].l, r = queries[i].r, c = queries[i].c;
				vector<pair<pair<int, int>, int>> inserted, erased;
				auto it = st.lower_bound({{l, -1}, -1});
				if (it != st.begin()) --it;
				for (int iteration = 0; it != st.end(); ++iteration, ++it) {
					int curl = it->first.first, curr = it->first.second, curc = it->second;
					if (curl >= r || curr <= l) {
						if (iteration == 0) {
							continue;
						} else {
							break;
						}
					}
					int cl = max(curl, l), cr = min(curr, r);
					erased.push_back({{curl, curr}, curc});
					inserted.push_back({{curl, cl}, curc});
					inserted.push_back({{cl, cr}, c});
					inserted.push_back({{cr, curr}, curc});
				}
				for (auto seg : erased) erase(seg.first.first, seg.first.second, seg.second);
				{
					vector<pair<pair<int, int>, int>> compressed;
					for (auto seg : inserted) {
						int curl = seg.first.first, curr = seg.first.second, curc = seg.second;
						if (curl >= curr) continue;
						if (compressed.empty() || curl > compressed.back().first.second || curc != compressed.back().second) {
							compressed.push_back({{curl, curr}, curc});
						} else {
							compressed.back().first.second = curr;
						}
					}
					inserted = compressed;
				}
				for (auto seg : inserted) insert(seg.first.first, seg.first.second, seg.second);
			}
		}
	}

	for (int i = 0; i <= q; ++i) cout << (ans[i] ? "Yes\n" : "No\n");
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 193 ms 3748 KB Output is correct
2 Correct 197 ms 4036 KB Output is correct
3 Correct 162 ms 4036 KB Output is correct
4 Correct 229 ms 3884 KB Output is correct
5 Correct 209 ms 3876 KB Output is correct
6 Correct 208 ms 3828 KB Output is correct
7 Correct 206 ms 3908 KB Output is correct
8 Correct 229 ms 3992 KB Output is correct
9 Correct 215 ms 4048 KB Output is correct
10 Correct 238 ms 3956 KB Output is correct
11 Correct 220 ms 4084 KB Output is correct
12 Correct 214 ms 4036 KB Output is correct
13 Correct 248 ms 3980 KB Output is correct
14 Correct 222 ms 4072 KB Output is correct
15 Correct 240 ms 4036 KB Output is correct
16 Correct 215 ms 3956 KB Output is correct
17 Correct 230 ms 4036 KB Output is correct
18 Correct 152 ms 3984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 193 ms 3748 KB Output is correct
2 Correct 197 ms 4036 KB Output is correct
3 Correct 162 ms 4036 KB Output is correct
4 Correct 229 ms 3884 KB Output is correct
5 Correct 209 ms 3876 KB Output is correct
6 Correct 208 ms 3828 KB Output is correct
7 Correct 206 ms 3908 KB Output is correct
8 Correct 229 ms 3992 KB Output is correct
9 Correct 215 ms 4048 KB Output is correct
10 Correct 238 ms 3956 KB Output is correct
11 Correct 220 ms 4084 KB Output is correct
12 Correct 214 ms 4036 KB Output is correct
13 Correct 248 ms 3980 KB Output is correct
14 Correct 222 ms 4072 KB Output is correct
15 Correct 240 ms 4036 KB Output is correct
16 Correct 215 ms 3956 KB Output is correct
17 Correct 230 ms 4036 KB Output is correct
18 Correct 152 ms 3984 KB Output is correct
19 Correct 445 ms 33900 KB Output is correct
20 Correct 362 ms 40420 KB Output is correct
21 Correct 606 ms 45032 KB Output is correct
22 Correct 472 ms 35936 KB Output is correct
23 Correct 295 ms 5816 KB Output is correct
24 Correct 304 ms 5780 KB Output is correct
25 Correct 620 ms 46612 KB Output is correct
26 Correct 568 ms 46696 KB Output is correct
27 Correct 480 ms 46624 KB Output is correct
28 Correct 443 ms 46640 KB Output is correct
29 Correct 449 ms 45960 KB Output is correct
30 Correct 294 ms 5408 KB Output is correct
31 Correct 469 ms 46720 KB Output is correct
32 Correct 448 ms 44248 KB Output is correct
33 Correct 290 ms 5644 KB Output is correct
34 Correct 452 ms 46716 KB Output is correct
35 Correct 496 ms 32508 KB Output is correct
36 Correct 297 ms 5700 KB Output is correct
37 Correct 316 ms 5828 KB Output is correct
38 Correct 337 ms 46700 KB Output is correct
39 Correct 365 ms 29864 KB Output is correct
40 Correct 491 ms 29012 KB Output is correct
41 Correct 251 ms 46712 KB Output is correct
42 Correct 259 ms 46616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 193 ms 3748 KB Output is correct
2 Correct 197 ms 4036 KB Output is correct
3 Correct 162 ms 4036 KB Output is correct
4 Correct 229 ms 3884 KB Output is correct
5 Correct 209 ms 3876 KB Output is correct
6 Correct 208 ms 3828 KB Output is correct
7 Correct 206 ms 3908 KB Output is correct
8 Correct 229 ms 3992 KB Output is correct
9 Correct 215 ms 4048 KB Output is correct
10 Correct 238 ms 3956 KB Output is correct
11 Correct 220 ms 4084 KB Output is correct
12 Correct 214 ms 4036 KB Output is correct
13 Correct 248 ms 3980 KB Output is correct
14 Correct 222 ms 4072 KB Output is correct
15 Correct 240 ms 4036 KB Output is correct
16 Correct 215 ms 3956 KB Output is correct
17 Correct 230 ms 4036 KB Output is correct
18 Correct 152 ms 3984 KB Output is correct
19 Correct 1367 ms 3836 KB Output is correct
20 Correct 1129 ms 4100 KB Output is correct
21 Correct 554 ms 4036 KB Output is correct
22 Correct 482 ms 3764 KB Output is correct
23 Correct 552 ms 3992 KB Output is correct
24 Correct 528 ms 3884 KB Output is correct
25 Correct 609 ms 3976 KB Output is correct
26 Correct 506 ms 3868 KB Output is correct
27 Correct 564 ms 3972 KB Output is correct
28 Correct 506 ms 3712 KB Output is correct
29 Correct 552 ms 3980 KB Output is correct
30 Correct 498 ms 3756 KB Output is correct
31 Correct 1618 ms 3976 KB Output is correct
32 Correct 1575 ms 3972 KB Output is correct
33 Correct 1576 ms 3980 KB Output is correct
34 Correct 1439 ms 3816 KB Output is correct
35 Correct 1638 ms 3980 KB Output is correct
36 Correct 1546 ms 4088 KB Output is correct
37 Correct 1596 ms 3980 KB Output is correct
38 Correct 1604 ms 3980 KB Output is correct
39 Correct 1583 ms 3980 KB Output is correct
40 Correct 1579 ms 3984 KB Output is correct
41 Correct 1571 ms 3980 KB Output is correct
42 Correct 1609 ms 3976 KB Output is correct
43 Correct 1476 ms 4056 KB Output is correct
44 Correct 1591 ms 3980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 193 ms 3748 KB Output is correct
2 Correct 197 ms 4036 KB Output is correct
3 Correct 162 ms 4036 KB Output is correct
4 Correct 229 ms 3884 KB Output is correct
5 Correct 209 ms 3876 KB Output is correct
6 Correct 208 ms 3828 KB Output is correct
7 Correct 206 ms 3908 KB Output is correct
8 Correct 229 ms 3992 KB Output is correct
9 Correct 215 ms 4048 KB Output is correct
10 Correct 238 ms 3956 KB Output is correct
11 Correct 220 ms 4084 KB Output is correct
12 Correct 214 ms 4036 KB Output is correct
13 Correct 248 ms 3980 KB Output is correct
14 Correct 222 ms 4072 KB Output is correct
15 Correct 240 ms 4036 KB Output is correct
16 Correct 215 ms 3956 KB Output is correct
17 Correct 230 ms 4036 KB Output is correct
18 Correct 152 ms 3984 KB Output is correct
19 Correct 445 ms 33900 KB Output is correct
20 Correct 362 ms 40420 KB Output is correct
21 Correct 606 ms 45032 KB Output is correct
22 Correct 472 ms 35936 KB Output is correct
23 Correct 295 ms 5816 KB Output is correct
24 Correct 304 ms 5780 KB Output is correct
25 Correct 620 ms 46612 KB Output is correct
26 Correct 568 ms 46696 KB Output is correct
27 Correct 480 ms 46624 KB Output is correct
28 Correct 443 ms 46640 KB Output is correct
29 Correct 449 ms 45960 KB Output is correct
30 Correct 294 ms 5408 KB Output is correct
31 Correct 469 ms 46720 KB Output is correct
32 Correct 448 ms 44248 KB Output is correct
33 Correct 290 ms 5644 KB Output is correct
34 Correct 452 ms 46716 KB Output is correct
35 Correct 496 ms 32508 KB Output is correct
36 Correct 297 ms 5700 KB Output is correct
37 Correct 316 ms 5828 KB Output is correct
38 Correct 337 ms 46700 KB Output is correct
39 Correct 365 ms 29864 KB Output is correct
40 Correct 491 ms 29012 KB Output is correct
41 Correct 251 ms 46712 KB Output is correct
42 Correct 259 ms 46616 KB Output is correct
43 Correct 1367 ms 3836 KB Output is correct
44 Correct 1129 ms 4100 KB Output is correct
45 Correct 554 ms 4036 KB Output is correct
46 Correct 482 ms 3764 KB Output is correct
47 Correct 552 ms 3992 KB Output is correct
48 Correct 528 ms 3884 KB Output is correct
49 Correct 609 ms 3976 KB Output is correct
50 Correct 506 ms 3868 KB Output is correct
51 Correct 564 ms 3972 KB Output is correct
52 Correct 506 ms 3712 KB Output is correct
53 Correct 552 ms 3980 KB Output is correct
54 Correct 498 ms 3756 KB Output is correct
55 Correct 1618 ms 3976 KB Output is correct
56 Correct 1575 ms 3972 KB Output is correct
57 Correct 1576 ms 3980 KB Output is correct
58 Correct 1439 ms 3816 KB Output is correct
59 Correct 1638 ms 3980 KB Output is correct
60 Correct 1546 ms 4088 KB Output is correct
61 Correct 1596 ms 3980 KB Output is correct
62 Correct 1604 ms 3980 KB Output is correct
63 Correct 1583 ms 3980 KB Output is correct
64 Correct 1579 ms 3984 KB Output is correct
65 Correct 1571 ms 3980 KB Output is correct
66 Correct 1609 ms 3976 KB Output is correct
67 Correct 1476 ms 4056 KB Output is correct
68 Correct 1591 ms 3980 KB Output is correct
69 Correct 2698 ms 35180 KB Output is correct
70 Correct 2621 ms 47652 KB Output is correct
71 Correct 787 ms 5828 KB Output is correct
72 Correct 770 ms 5856 KB Output is correct
73 Correct 785 ms 5868 KB Output is correct
74 Correct 1443 ms 38048 KB Output is correct
75 Correct 821 ms 5796 KB Output is correct
76 Correct 1560 ms 50144 KB Output is correct
77 Correct 1254 ms 38260 KB Output is correct
78 Correct 795 ms 5824 KB Output is correct
79 Correct 827 ms 5816 KB Output is correct
80 Correct 3561 ms 37368 KB Output is correct
81 Correct 2373 ms 6076 KB Output is correct
82 Correct 4665 ms 54980 KB Output is correct
83 Correct 3690 ms 52404 KB Output is correct
84 Correct 2193 ms 6048 KB Output is correct
85 Correct 2245 ms 6164 KB Output is correct
86 Correct 2762 ms 39072 KB Output is correct
87 Correct 3319 ms 55008 KB Output is correct
88 Correct 2005 ms 5856 KB Output is correct
89 Correct 3369 ms 50108 KB Output is correct
90 Correct 3689 ms 54920 KB Output is correct
91 Correct 2043 ms 5732 KB Output is correct
92 Correct 3708 ms 38756 KB Output is correct
93 Correct 2225 ms 6056 KB Output is correct
94 Correct 2207 ms 6000 KB Output is correct
95 Correct 2333 ms 6136 KB Output is correct
96 Correct 326 ms 46692 KB Output is correct
97 Correct 2394 ms 36048 KB Output is correct
98 Correct 4019 ms 35288 KB Output is correct
99 Correct 1564 ms 54916 KB Output is correct
100 Correct 1538 ms 54876 KB Output is correct