답안 #419885

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
419885 2021-06-07T15:45:40 Z Berted Crossing (JOI21_crossing) C++14
100 / 100
609 ms 24676 KB
#include <iostream>
#include <set>
#include <vector>
#define ll long long

using namespace std;

const int Hs = 2, SZ = (1 << 19) + 5;

const char cmp[3] = {'J', 'O', 'I'};
const ll MD[Hs] = {1000000007, 420691273}, Hp[Hs] = {17, 7};
ll pre[Hs][2][200001];

inline int getVal(const char &c)
{
	if (c == 'J') return 1;
	else if (c == 'O') return 2;
	else if (c == 'I') return 3;
	else return -1;
}

struct Hash
{
	int sz = 0; ll A[Hs] = {};
	inline void build(const string &s, const int &l, const int &r)
	{
		sz = r - l + 1;
		for (int i = l; i <= r; i++)
		{
			for (int j = 0; j < Hs; j++) {A[j] = (A[j] * Hp[j] % MD[j] + getVal(s[i])) % MD[j];}
		}
	}
};

inline Hash merge(const Hash &l, const Hash &r)
{
	Hash ret;
	for (int i = 0; i < Hs; i++)
	{
		ret.A[i] = (r.A[i] + l.A[i] * pre[i][0][r.sz]) % MD[i];
		ret.sz = l.sz + r.sz;
	}
	return ret;
}

inline bool operator==(const Hash &l, const Hash &r)
{
	for (int i = 0; i < Hs; i++)
	{
		if (l.A[i] != r.A[i]) return 0;
	}
	return 1;
}

int N, Q;
string A, B, C, T;
set<string> S;
Hash seg[SZ]; int lz[SZ];

void prop(int idx, int L, int R)
{
	if (L <= R)
	{
		if (lz[idx])
		{
			for (int i = 0; i < Hs; i++) {seg[idx].A[i] = pre[i][1][R - L + 1] * lz[idx] % MD[i];}
			if (L < R)
			{
				lz[2 * idx] = lz[idx];
				lz[2 * idx + 1] = lz[idx];
			}
			lz[idx] = 0;
		}
	}
}

void build(int idx, int L, int R, const string &T)
{
	//cerr << "BLD: " << L << " " << R << "\n";
	if (L < R)
	{
		int M = L + R >> 1;
		build(2 * idx, L, M, T);
		build(2 * idx + 1, M + 1, R, T);
		seg[idx] = merge(seg[2 * idx], seg[2 * idx + 1]);
	}
	else if (L == R) {seg[idx].build(T, L, R);}
}

inline void upd(int idx, int L, int R, int l, int r, int t)
{
	l = max(L, l); r = min(R, r);
	prop(idx, L, R);
	if (l <= r)
	{
		if (L == l && R == r) {lz[idx] = t; prop(idx, L, R);}
		else
		{
			int M = L + R >> 1;
			upd(2 * idx, L, M, l, r, t);
			upd(2 * idx + 1, M + 1, R, l, r, t);
			seg[idx] = merge(seg[2 * idx], seg[2 * idx + 1]);
		}
	}
}

inline string cross(const string &A, const string &B)
{
	string ret;
	for (int i = 0; i < N; i++)
	{
		int a = getVal(A[i]) - 1, b = getVal(B[i]) - 1;
		if (a == b) ret.push_back(cmp[a]);
		else ret.push_back(cmp[3 - a - b]);
	}
	return ret;
}

vector<Hash> dat;

int main()
{
	ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> N;
	cin >> A >> B >> C;

	for (int i = 0; i < Hs; i++)		// Hashing precomputation.
	{
		pre[i][0][0] = 1;
		for (int k = 1; k <= N; k++)
		{
			pre[i][0][k] = pre[i][0][k - 1] * Hp[i] % MD[i];
			pre[i][1][k] = (pre[i][1][k - 1] * Hp[i] % MD[i] + 1) % MD[i];
		}
	}

	S.insert(A); S.insert(B); S.insert(C);
	for (const auto &x : S)
		for (const auto &y : S) S.insert(cross(x, y));

	for (const auto &x : S) 
	{
		dat.push_back(Hash());
		dat.back().build(x, 0, N - 1);
		//cerr << x << "\n" << dat.back().A[0] << "\n" << dat.back().A[1] << "\n";
	}

	cin >> Q;
	cin >> T;

	build(1, 0, N - 1, T);

	for (int i = 0; i <= Q; i++)
	{
		bool found = 0;
		for (const auto &x : dat)
		{
			if (x == seg[1]) {found = 1; break;} 
		}
		if (found) cout << "Yes\n";
		else cout << "No\n";
		if (i < Q)
		{
			int l, r; char c; cin >> l >> r >> c;
			upd(1, 0, N - 1, l - 1, r - 1, getVal(c));
		}
	}
	return 0;
}

Compilation message

Main.cpp: In function 'void build(int, int, int, const string&)':
Main.cpp:82:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   82 |   int M = L + R >> 1;
      |           ~~^~~
Main.cpp: In function 'void upd(int, int, int, int, int, int)':
Main.cpp:99:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   99 |    int M = L + R >> 1;
      |            ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 124 ms 828 KB Output is correct
2 Correct 116 ms 892 KB Output is correct
3 Correct 126 ms 836 KB Output is correct
4 Correct 99 ms 936 KB Output is correct
5 Correct 93 ms 896 KB Output is correct
6 Correct 99 ms 836 KB Output is correct
7 Correct 116 ms 856 KB Output is correct
8 Correct 99 ms 856 KB Output is correct
9 Correct 116 ms 912 KB Output is correct
10 Correct 106 ms 944 KB Output is correct
11 Correct 103 ms 832 KB Output is correct
12 Correct 101 ms 936 KB Output is correct
13 Correct 103 ms 896 KB Output is correct
14 Correct 98 ms 964 KB Output is correct
15 Correct 102 ms 868 KB Output is correct
16 Correct 102 ms 852 KB Output is correct
17 Correct 100 ms 940 KB Output is correct
18 Correct 125 ms 900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 124 ms 828 KB Output is correct
2 Correct 116 ms 892 KB Output is correct
3 Correct 126 ms 836 KB Output is correct
4 Correct 99 ms 936 KB Output is correct
5 Correct 93 ms 896 KB Output is correct
6 Correct 99 ms 836 KB Output is correct
7 Correct 116 ms 856 KB Output is correct
8 Correct 99 ms 856 KB Output is correct
9 Correct 116 ms 912 KB Output is correct
10 Correct 106 ms 944 KB Output is correct
11 Correct 103 ms 832 KB Output is correct
12 Correct 101 ms 936 KB Output is correct
13 Correct 103 ms 896 KB Output is correct
14 Correct 98 ms 964 KB Output is correct
15 Correct 102 ms 868 KB Output is correct
16 Correct 102 ms 852 KB Output is correct
17 Correct 100 ms 940 KB Output is correct
18 Correct 125 ms 900 KB Output is correct
19 Correct 463 ms 22712 KB Output is correct
20 Correct 381 ms 21140 KB Output is correct
21 Correct 313 ms 22340 KB Output is correct
22 Correct 277 ms 21588 KB Output is correct
23 Correct 164 ms 2244 KB Output is correct
24 Correct 168 ms 2132 KB Output is correct
25 Correct 374 ms 22700 KB Output is correct
26 Correct 311 ms 22704 KB Output is correct
27 Correct 332 ms 22820 KB Output is correct
28 Correct 337 ms 22736 KB Output is correct
29 Correct 314 ms 22472 KB Output is correct
30 Correct 179 ms 2244 KB Output is correct
31 Correct 324 ms 22680 KB Output is correct
32 Correct 459 ms 22148 KB Output is correct
33 Correct 167 ms 2232 KB Output is correct
34 Correct 381 ms 22668 KB Output is correct
35 Correct 286 ms 20820 KB Output is correct
36 Correct 191 ms 2140 KB Output is correct
37 Correct 192 ms 2168 KB Output is correct
38 Correct 335 ms 21044 KB Output is correct
39 Correct 202 ms 20600 KB Output is correct
40 Correct 353 ms 13076 KB Output is correct
41 Correct 545 ms 22828 KB Output is correct
42 Correct 80 ms 20884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 124 ms 828 KB Output is correct
2 Correct 116 ms 892 KB Output is correct
3 Correct 126 ms 836 KB Output is correct
4 Correct 99 ms 936 KB Output is correct
5 Correct 93 ms 896 KB Output is correct
6 Correct 99 ms 836 KB Output is correct
7 Correct 116 ms 856 KB Output is correct
8 Correct 99 ms 856 KB Output is correct
9 Correct 116 ms 912 KB Output is correct
10 Correct 106 ms 944 KB Output is correct
11 Correct 103 ms 832 KB Output is correct
12 Correct 101 ms 936 KB Output is correct
13 Correct 103 ms 896 KB Output is correct
14 Correct 98 ms 964 KB Output is correct
15 Correct 102 ms 868 KB Output is correct
16 Correct 102 ms 852 KB Output is correct
17 Correct 100 ms 940 KB Output is correct
18 Correct 125 ms 900 KB Output is correct
19 Correct 121 ms 828 KB Output is correct
20 Correct 138 ms 856 KB Output is correct
21 Correct 104 ms 932 KB Output is correct
22 Correct 94 ms 840 KB Output is correct
23 Correct 101 ms 852 KB Output is correct
24 Correct 108 ms 840 KB Output is correct
25 Correct 103 ms 920 KB Output is correct
26 Correct 91 ms 856 KB Output is correct
27 Correct 124 ms 840 KB Output is correct
28 Correct 90 ms 900 KB Output is correct
29 Correct 106 ms 836 KB Output is correct
30 Correct 88 ms 852 KB Output is correct
31 Correct 108 ms 908 KB Output is correct
32 Correct 102 ms 896 KB Output is correct
33 Correct 121 ms 840 KB Output is correct
34 Correct 92 ms 916 KB Output is correct
35 Correct 107 ms 876 KB Output is correct
36 Correct 103 ms 932 KB Output is correct
37 Correct 137 ms 840 KB Output is correct
38 Correct 111 ms 844 KB Output is correct
39 Correct 106 ms 872 KB Output is correct
40 Correct 103 ms 868 KB Output is correct
41 Correct 123 ms 984 KB Output is correct
42 Correct 103 ms 856 KB Output is correct
43 Correct 96 ms 948 KB Output is correct
44 Correct 106 ms 936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 124 ms 828 KB Output is correct
2 Correct 116 ms 892 KB Output is correct
3 Correct 126 ms 836 KB Output is correct
4 Correct 99 ms 936 KB Output is correct
5 Correct 93 ms 896 KB Output is correct
6 Correct 99 ms 836 KB Output is correct
7 Correct 116 ms 856 KB Output is correct
8 Correct 99 ms 856 KB Output is correct
9 Correct 116 ms 912 KB Output is correct
10 Correct 106 ms 944 KB Output is correct
11 Correct 103 ms 832 KB Output is correct
12 Correct 101 ms 936 KB Output is correct
13 Correct 103 ms 896 KB Output is correct
14 Correct 98 ms 964 KB Output is correct
15 Correct 102 ms 868 KB Output is correct
16 Correct 102 ms 852 KB Output is correct
17 Correct 100 ms 940 KB Output is correct
18 Correct 125 ms 900 KB Output is correct
19 Correct 463 ms 22712 KB Output is correct
20 Correct 381 ms 21140 KB Output is correct
21 Correct 313 ms 22340 KB Output is correct
22 Correct 277 ms 21588 KB Output is correct
23 Correct 164 ms 2244 KB Output is correct
24 Correct 168 ms 2132 KB Output is correct
25 Correct 374 ms 22700 KB Output is correct
26 Correct 311 ms 22704 KB Output is correct
27 Correct 332 ms 22820 KB Output is correct
28 Correct 337 ms 22736 KB Output is correct
29 Correct 314 ms 22472 KB Output is correct
30 Correct 179 ms 2244 KB Output is correct
31 Correct 324 ms 22680 KB Output is correct
32 Correct 459 ms 22148 KB Output is correct
33 Correct 167 ms 2232 KB Output is correct
34 Correct 381 ms 22668 KB Output is correct
35 Correct 286 ms 20820 KB Output is correct
36 Correct 191 ms 2140 KB Output is correct
37 Correct 192 ms 2168 KB Output is correct
38 Correct 335 ms 21044 KB Output is correct
39 Correct 202 ms 20600 KB Output is correct
40 Correct 353 ms 13076 KB Output is correct
41 Correct 545 ms 22828 KB Output is correct
42 Correct 80 ms 20884 KB Output is correct
43 Correct 121 ms 828 KB Output is correct
44 Correct 138 ms 856 KB Output is correct
45 Correct 104 ms 932 KB Output is correct
46 Correct 94 ms 840 KB Output is correct
47 Correct 101 ms 852 KB Output is correct
48 Correct 108 ms 840 KB Output is correct
49 Correct 103 ms 920 KB Output is correct
50 Correct 91 ms 856 KB Output is correct
51 Correct 124 ms 840 KB Output is correct
52 Correct 90 ms 900 KB Output is correct
53 Correct 106 ms 836 KB Output is correct
54 Correct 88 ms 852 KB Output is correct
55 Correct 108 ms 908 KB Output is correct
56 Correct 102 ms 896 KB Output is correct
57 Correct 121 ms 840 KB Output is correct
58 Correct 92 ms 916 KB Output is correct
59 Correct 107 ms 876 KB Output is correct
60 Correct 103 ms 932 KB Output is correct
61 Correct 137 ms 840 KB Output is correct
62 Correct 111 ms 844 KB Output is correct
63 Correct 106 ms 872 KB Output is correct
64 Correct 103 ms 868 KB Output is correct
65 Correct 123 ms 984 KB Output is correct
66 Correct 103 ms 856 KB Output is correct
67 Correct 96 ms 948 KB Output is correct
68 Correct 106 ms 936 KB Output is correct
69 Correct 609 ms 22964 KB Output is correct
70 Correct 486 ms 22900 KB Output is correct
71 Correct 172 ms 2224 KB Output is correct
72 Correct 187 ms 2184 KB Output is correct
73 Correct 169 ms 2196 KB Output is correct
74 Correct 348 ms 21828 KB Output is correct
75 Correct 164 ms 2188 KB Output is correct
76 Correct 347 ms 23040 KB Output is correct
77 Correct 330 ms 21844 KB Output is correct
78 Correct 174 ms 2188 KB Output is correct
79 Correct 204 ms 2244 KB Output is correct
80 Correct 409 ms 21920 KB Output is correct
81 Correct 184 ms 2292 KB Output is correct
82 Correct 572 ms 24464 KB Output is correct
83 Correct 572 ms 23928 KB Output is correct
84 Correct 178 ms 2244 KB Output is correct
85 Correct 190 ms 2320 KB Output is correct
86 Correct 392 ms 22232 KB Output is correct
87 Correct 485 ms 24448 KB Output is correct
88 Correct 223 ms 2260 KB Output is correct
89 Correct 413 ms 23412 KB Output is correct
90 Correct 501 ms 24372 KB Output is correct
91 Correct 191 ms 2336 KB Output is correct
92 Correct 492 ms 22124 KB Output is correct
93 Correct 170 ms 2284 KB Output is correct
94 Correct 176 ms 2284 KB Output is correct
95 Correct 180 ms 2268 KB Output is correct
96 Correct 336 ms 21056 KB Output is correct
97 Correct 269 ms 22460 KB Output is correct
98 Correct 364 ms 14280 KB Output is correct
99 Correct 579 ms 24676 KB Output is correct
100 Correct 210 ms 22624 KB Output is correct