답안 #538028

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
538028 2022-03-16T03:26:25 Z 8e7 Long Mansion (JOI17_long_mansion) C++17
100 / 100
410 ms 49036 KB
//Challenge: Accepted
#include <bits/stdc++.h>
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
	while (l != r) cout << *l << " ", l++;
	cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 500005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
vector<int> pos[maxn];
int c[maxn];

pii ans[maxn];

pii merge(pii x, pii y){return {min(x.ff, y.ff), max(x.ss, y.ss)};}
bool vis[maxn];
struct DSU{
	pii ran[maxn];
	int par[maxn];
	void init(int n) {
		for (int i = 1;i <= n;i++) par[i] = i, ran[i] = {i, i};
	}
	int find(int a) {
		return a == par[a] ? a : (par[a] = find(par[a]));
	}
	pii getran(int a) {
		return ran[find(a)];
	}
	void Union(int a, int b) {
		a = find(a), b = find(b);
		ran[b] = merge(ran[b], ran[a]);
		par[a] = b;
	}
} d;
bool key(int x, int l, int r) {
	int il = lower_bound(pos[x].begin(), pos[x].end(), l) - pos[x].begin();
	int ir = upper_bound(pos[x].begin(), pos[x].end(), r) - pos[x].begin();
	return ir > il;	
}
int tot;
bool solve(int n, int par) {
	while (true) {
		n = d.find(n);
		pii r = d.getran(n);	
		if (r.ff > 1 && key(c[r.ff-1], r.ff, r.ss)) {
			if (d.find(par) == d.find(r.ff-1)) return true;
			if (solve(r.ff-1, n)) {
				d.Union(r.ff-1, n);
			} else {
				d.ran[n] = merge(d.ran[n], d.ran[r.ff-1]);				
			}
		} else if (r.ss < tot && key(c[r.ss], r.ff, r.ss)) {
			if (d.find(par) == d.find(r.ss+1)) return true;
			if (solve(r.ss+1, n)) {
				d.Union(r.ss+1, n);
			} else {
				d.ran[n] = merge(d.ran[n], d.ran[r.ss+1]);				
			}
		} else {
			break;
		}
	}
	return false;
}
int main() {
	io
	int n;
	cin >> n;
	tot = n;
	for (int i = 1;i <= n - 1;i++) cin >> c[i];	
	for (int i = 1;i <= n;i++) {
		int bi;
		cin >> bi;
		for (int j = 0;j < bi;j++) {
			int x;cin>> x;
			pos[x].push_back(i);
		}
	}
	d.init(n);
	for (int i = 1;i <= n;i++) {
		if (vis[d.find(i)]) continue;
		solve(i, 0);
		vis[d.find(i)] = 1;
	}
	for (int i = 1;i <= n;i++) {
		ans[i] = d.getran(i);
	}
	int q;
	cin >> q;
	while (q--) {
		int x, y;
		cin >> x >> y;
		cout << (y <= ans[x].ss && y >= ans[x].ff ? "YES" : "NO") << "\n";
	}
}

# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 16084 KB Output is correct
2 Correct 12 ms 16152 KB Output is correct
3 Correct 11 ms 16168 KB Output is correct
4 Correct 10 ms 16084 KB Output is correct
5 Correct 14 ms 16032 KB Output is correct
6 Correct 11 ms 16068 KB Output is correct
7 Correct 10 ms 16084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 16084 KB Output is correct
2 Correct 12 ms 16152 KB Output is correct
3 Correct 11 ms 16168 KB Output is correct
4 Correct 10 ms 16084 KB Output is correct
5 Correct 14 ms 16032 KB Output is correct
6 Correct 11 ms 16068 KB Output is correct
7 Correct 10 ms 16084 KB Output is correct
8 Correct 119 ms 21976 KB Output is correct
9 Correct 110 ms 21836 KB Output is correct
10 Correct 129 ms 22244 KB Output is correct
11 Correct 116 ms 22532 KB Output is correct
12 Correct 98 ms 21572 KB Output is correct
13 Correct 122 ms 22048 KB Output is correct
14 Correct 114 ms 22092 KB Output is correct
15 Correct 138 ms 22076 KB Output is correct
16 Correct 132 ms 21856 KB Output is correct
17 Correct 121 ms 22116 KB Output is correct
18 Correct 122 ms 22148 KB Output is correct
19 Correct 112 ms 22160 KB Output is correct
20 Correct 105 ms 22040 KB Output is correct
21 Correct 114 ms 21996 KB Output is correct
22 Correct 128 ms 22012 KB Output is correct
23 Correct 125 ms 21920 KB Output is correct
24 Correct 121 ms 21860 KB Output is correct
25 Correct 110 ms 21908 KB Output is correct
26 Correct 126 ms 21948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 221 ms 23240 KB Output is correct
2 Correct 232 ms 28320 KB Output is correct
3 Correct 217 ms 28300 KB Output is correct
4 Correct 230 ms 28860 KB Output is correct
5 Correct 225 ms 28876 KB Output is correct
6 Correct 211 ms 27096 KB Output is correct
7 Correct 155 ms 26956 KB Output is correct
8 Correct 161 ms 26812 KB Output is correct
9 Correct 151 ms 26804 KB Output is correct
10 Correct 182 ms 26832 KB Output is correct
11 Correct 148 ms 26804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 16084 KB Output is correct
2 Correct 12 ms 16152 KB Output is correct
3 Correct 11 ms 16168 KB Output is correct
4 Correct 10 ms 16084 KB Output is correct
5 Correct 14 ms 16032 KB Output is correct
6 Correct 11 ms 16068 KB Output is correct
7 Correct 10 ms 16084 KB Output is correct
8 Correct 119 ms 21976 KB Output is correct
9 Correct 110 ms 21836 KB Output is correct
10 Correct 129 ms 22244 KB Output is correct
11 Correct 116 ms 22532 KB Output is correct
12 Correct 98 ms 21572 KB Output is correct
13 Correct 122 ms 22048 KB Output is correct
14 Correct 114 ms 22092 KB Output is correct
15 Correct 138 ms 22076 KB Output is correct
16 Correct 132 ms 21856 KB Output is correct
17 Correct 121 ms 22116 KB Output is correct
18 Correct 122 ms 22148 KB Output is correct
19 Correct 112 ms 22160 KB Output is correct
20 Correct 105 ms 22040 KB Output is correct
21 Correct 114 ms 21996 KB Output is correct
22 Correct 128 ms 22012 KB Output is correct
23 Correct 125 ms 21920 KB Output is correct
24 Correct 121 ms 21860 KB Output is correct
25 Correct 110 ms 21908 KB Output is correct
26 Correct 126 ms 21948 KB Output is correct
27 Correct 221 ms 23240 KB Output is correct
28 Correct 232 ms 28320 KB Output is correct
29 Correct 217 ms 28300 KB Output is correct
30 Correct 230 ms 28860 KB Output is correct
31 Correct 225 ms 28876 KB Output is correct
32 Correct 211 ms 27096 KB Output is correct
33 Correct 155 ms 26956 KB Output is correct
34 Correct 161 ms 26812 KB Output is correct
35 Correct 151 ms 26804 KB Output is correct
36 Correct 182 ms 26832 KB Output is correct
37 Correct 148 ms 26804 KB Output is correct
38 Correct 310 ms 38896 KB Output is correct
39 Correct 308 ms 40388 KB Output is correct
40 Correct 238 ms 36196 KB Output is correct
41 Correct 410 ms 37812 KB Output is correct
42 Correct 184 ms 28412 KB Output is correct
43 Correct 158 ms 28268 KB Output is correct
44 Correct 260 ms 34768 KB Output is correct
45 Correct 243 ms 34896 KB Output is correct
46 Correct 279 ms 35336 KB Output is correct
47 Correct 197 ms 28572 KB Output is correct
48 Correct 157 ms 28056 KB Output is correct
49 Correct 240 ms 34092 KB Output is correct
50 Correct 235 ms 34600 KB Output is correct
51 Correct 308 ms 35672 KB Output is correct
52 Correct 196 ms 35992 KB Output is correct
53 Correct 231 ms 42224 KB Output is correct
54 Correct 293 ms 49036 KB Output is correct
55 Correct 268 ms 42372 KB Output is correct