답안 #538033

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
538033 2022-03-16T03:34:59 Z fhvirus Long Mansion (JOI17_long_mansion) C++17
100 / 100
366 ms 76664 KB
// rama_pang orz
// solution and codestyle adapted from his/her submission
#include<bits/stdc++.h>
using namespace std;

struct DisjointSet {
	int n;
	vector<int> f;
	DisjointSet () = default;
	DisjointSet (const int& _n): n(_n), f(n) { iota(begin(f), end(f), 0); }
	int F(int u) { return u == f[u] ? u : f[u] = F(f[u]); }
	bool M(int u, int v) {
		u = F(u); v = F(v);
		if (u != v) f[v] = u;
		return u != v;
	}
};

const int kN = 500005;
int N, Q, C[kN], B[kN];
vector<int> A[kN];

vector<int> key_pos[kN];
bool can_unlock(int l, int r, int c) {
	return *lower_bound(begin(key_pos[c]), end(key_pos[c]), l) <= r;
}

pair<int, int> memo[kN];
bool in_stack[kN];
DisjointSet dsu(kN);

array<int, 3> solve(int u) {
	if (in_stack[u]) return {u, u, u};
	if (dsu.F(u) != u) return solve(dsu.F(u));
	if (memo[u].first != 0) return {0, memo[u].first, memo[u].second};

	in_stack[u] = true;
	int lb = u, rb = u;
	while (true) {
		if (lb > 1 and can_unlock(lb, rb, C[lb - 1])) {
			auto nxt = solve(lb - 1);
			lb = min(lb, nxt[1]);
			rb = max(rb, nxt[2]);
			if (nxt[0] != 0 and dsu.M(nxt[0], u)) {
				in_stack[u] = false;
				return {nxt[0], lb, rb};
			}
		} else if (rb < N and can_unlock(lb, rb, C[rb])) {
			auto nxt = solve(rb + 1);
			lb = min(lb, nxt[1]);
			rb = max(rb, nxt[2]);
			if (nxt[0] != 0 and dsu.M(nxt[0], u)) {
				in_stack[u] = false;
				return {nxt[0], lb, rb};
			}
		} else break;
	}

	in_stack[u] = false;
	memo[u] = {lb, rb};
	return {0, lb, rb};
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	cin >> N;
	for (int i = 1; i < N; ++i)
		cin >> C[i];
	for (int i = 1; i <= N; ++i) {
		cin >> B[i];
		A[i].resize(B[i]);
		for (int& a: A[i]) {
			cin >> a;
			key_pos[a].emplace_back(i);
		}
	}
	for (int i = 1; i <= N; ++i)
		key_pos[i].emplace_back(N + 1);

	cin >> Q;
	for (int X, Y, i = 0; i < Q; ++i) {
		cin >> X >> Y;
		const auto ans = solve(X);
		cout << (ans[1] <= Y and Y <= ans[2] ? "YES\n" : "NO\n");
	}
	
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 25940 KB Output is correct
2 Correct 15 ms 26068 KB Output is correct
3 Correct 16 ms 26288 KB Output is correct
4 Correct 15 ms 25940 KB Output is correct
5 Correct 15 ms 25912 KB Output is correct
6 Correct 16 ms 25988 KB Output is correct
7 Correct 16 ms 25860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 25940 KB Output is correct
2 Correct 15 ms 26068 KB Output is correct
3 Correct 16 ms 26288 KB Output is correct
4 Correct 15 ms 25940 KB Output is correct
5 Correct 15 ms 25912 KB Output is correct
6 Correct 16 ms 25988 KB Output is correct
7 Correct 16 ms 25860 KB Output is correct
8 Correct 102 ms 27464 KB Output is correct
9 Correct 100 ms 27488 KB Output is correct
10 Correct 111 ms 27816 KB Output is correct
11 Correct 104 ms 28236 KB Output is correct
12 Correct 90 ms 27776 KB Output is correct
13 Correct 112 ms 27688 KB Output is correct
14 Correct 100 ms 27692 KB Output is correct
15 Correct 123 ms 27816 KB Output is correct
16 Correct 101 ms 28108 KB Output is correct
17 Correct 95 ms 27724 KB Output is correct
18 Correct 103 ms 27684 KB Output is correct
19 Correct 96 ms 27740 KB Output is correct
20 Correct 100 ms 27976 KB Output is correct
21 Correct 101 ms 28120 KB Output is correct
22 Correct 98 ms 27932 KB Output is correct
23 Correct 102 ms 27568 KB Output is correct
24 Correct 101 ms 27468 KB Output is correct
25 Correct 97 ms 27472 KB Output is correct
26 Correct 112 ms 27560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 177 ms 37744 KB Output is correct
2 Correct 189 ms 39340 KB Output is correct
3 Correct 187 ms 37736 KB Output is correct
4 Correct 192 ms 37704 KB Output is correct
5 Correct 189 ms 37800 KB Output is correct
6 Correct 158 ms 38104 KB Output is correct
7 Correct 177 ms 44476 KB Output is correct
8 Correct 150 ms 49384 KB Output is correct
9 Correct 159 ms 45356 KB Output is correct
10 Correct 152 ms 41752 KB Output is correct
11 Correct 158 ms 37984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 25940 KB Output is correct
2 Correct 15 ms 26068 KB Output is correct
3 Correct 16 ms 26288 KB Output is correct
4 Correct 15 ms 25940 KB Output is correct
5 Correct 15 ms 25912 KB Output is correct
6 Correct 16 ms 25988 KB Output is correct
7 Correct 16 ms 25860 KB Output is correct
8 Correct 102 ms 27464 KB Output is correct
9 Correct 100 ms 27488 KB Output is correct
10 Correct 111 ms 27816 KB Output is correct
11 Correct 104 ms 28236 KB Output is correct
12 Correct 90 ms 27776 KB Output is correct
13 Correct 112 ms 27688 KB Output is correct
14 Correct 100 ms 27692 KB Output is correct
15 Correct 123 ms 27816 KB Output is correct
16 Correct 101 ms 28108 KB Output is correct
17 Correct 95 ms 27724 KB Output is correct
18 Correct 103 ms 27684 KB Output is correct
19 Correct 96 ms 27740 KB Output is correct
20 Correct 100 ms 27976 KB Output is correct
21 Correct 101 ms 28120 KB Output is correct
22 Correct 98 ms 27932 KB Output is correct
23 Correct 102 ms 27568 KB Output is correct
24 Correct 101 ms 27468 KB Output is correct
25 Correct 97 ms 27472 KB Output is correct
26 Correct 112 ms 27560 KB Output is correct
27 Correct 177 ms 37744 KB Output is correct
28 Correct 189 ms 39340 KB Output is correct
29 Correct 187 ms 37736 KB Output is correct
30 Correct 192 ms 37704 KB Output is correct
31 Correct 189 ms 37800 KB Output is correct
32 Correct 158 ms 38104 KB Output is correct
33 Correct 177 ms 44476 KB Output is correct
34 Correct 150 ms 49384 KB Output is correct
35 Correct 159 ms 45356 KB Output is correct
36 Correct 152 ms 41752 KB Output is correct
37 Correct 158 ms 37984 KB Output is correct
38 Correct 366 ms 61776 KB Output is correct
39 Correct 364 ms 69584 KB Output is correct
40 Correct 327 ms 53836 KB Output is correct
41 Correct 344 ms 76664 KB Output is correct
42 Correct 194 ms 35788 KB Output is correct
43 Correct 161 ms 36428 KB Output is correct
44 Correct 332 ms 44596 KB Output is correct
45 Correct 266 ms 44560 KB Output is correct
46 Correct 282 ms 44480 KB Output is correct
47 Correct 174 ms 43084 KB Output is correct
48 Correct 163 ms 37708 KB Output is correct
49 Correct 242 ms 47188 KB Output is correct
50 Correct 247 ms 47332 KB Output is correct
51 Correct 330 ms 54900 KB Output is correct
52 Correct 198 ms 45360 KB Output is correct
53 Correct 331 ms 59464 KB Output is correct
54 Correct 296 ms 61760 KB Output is correct
55 Correct 260 ms 52036 KB Output is correct