Submission #429787

# Submission time Handle Problem Language Result Execution time Memory
429787 2021-06-16T09:33:37 Z Kevin_Zhang_TW Long Mansion (JOI17_long_mansion) C++17
100 / 100
443 ms 53568 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l)==r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
// My bug list :
// integer overflow
// 0based, 1based forgotten
// index out of bound
// n, m, i, j typo
// some cases are missing

const int MAX_N = 500010;
int n, req[MAX_N], ln[MAX_N], rn[MAX_N], lgo[MAX_N], rgo[MAX_N];
vector<int> key[MAX_N];

void init_lrn() {
	vector<int> at(n + 10);
	for (int i = 1;i < n;++i) {
		for (int j : key[i]) at[j] = i;
		ln[i] = at[req[i]];
	}
	fill(AI(at), n + 1);
	for (int i = n-1;i >= 1;--i) {
		for (int j : key[i+1]) at[j] = i+1;
		rn[i] = at[req[i]];
	}
	rn[0] = n+1;
	ln[n+1] = 0;
}

struct sgt {
	struct node {
		int mn, mx;
		node (int a, int b) : mn(a), mx(b) {}
		node (int v = 0) : mn(v), mx(v) {}
		node (node a, node b) :
			mn(min(a.mn, b.mn)), mx(max(a.mx, b.mx)) {}
	};
	int n;
	vector<node> val;
	sgt(int n) : n(n) {
		val.resize(n<<1);
	}
	void modify(int i, int l, int r) {
		i += n;
		val[i] = node(l, r);
		while (i>>=1) 
			val[i] = node(val[i<<1], val[i<<1|1]);
	}
	node qry(int l, int r) {
		l += n, r += n;
		node ret(n + 10, -10);
		for (;l < r;l>>=1, r>>=1) {
			if (l&1) ret = node(ret, val[l++]);
			if (r&1) ret = node(ret, val[--r]);
		}
		return ret;
	}
}tree(0);
void upd(int &l, int &r) {
	auto [mn, mx] = tree.qry(l, r+1);
	chmin(l, mn);
	chmax(r, mx);
}

void init() {
	init_lrn();

	tree = sgt(n + 1);

	for (int i = 1;i <= n;++i) {
		lgo[i] = rgo[i] = i;
	}
	for (int i = 1;i <= n;++i) {
		int &l = lgo[i];
		while (rn[l-1] <= i) {
			--l;
			chmin(l, lgo[l]);
		}
	}
	for (int i = n;i >= 1;--i) {
		int &r = rgo[i];
		while (ln[r] >= i) {
			++r;
			chmax(r, rgo[r]);
		}
	}
	for (int i = 1;i <= n;++i) 
		tree.modify(i, lgo[i], rgo[i]);

	for (int i = 1;i <= n;++i) {
		int &l = lgo[i], &r = rgo[i];
		assert(l >= 1 && r <= n);
		while (rn[l-1] <= r || ln[r] >= l) {
			if (rn[l-1] <= r) --l;
			if (ln[r] >= l) ++r;
			upd(l, r);
		}
		tree.modify(i, l, r);
	}
}
int32_t main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	cin >> n;
	for (int i = 1;i < n;++i) cin >> req[i];
	for (int i = 1, sz;i <= n;++i) {
		cin >> sz;
		key[i].resize(sz);
		for (int &j : key[i]) cin >> j;
	}

	init();

	int q;
	cin >> q;
#define yn(i) ((i) ? "YES" : "NO")
	while (q--) {
		int x, y;
		cin >> x >> y;
		if (x < y) {
			cout << yn(rgo[x] >= y) << '\n';
		}
		else {
			cout << yn(lgo[x] <= y) << '\n';
		}
	}
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12236 KB Output is correct
2 Correct 13 ms 12236 KB Output is correct
3 Correct 12 ms 12416 KB Output is correct
4 Correct 11 ms 12200 KB Output is correct
5 Correct 12 ms 12160 KB Output is correct
6 Correct 11 ms 12228 KB Output is correct
7 Correct 11 ms 12236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12236 KB Output is correct
2 Correct 13 ms 12236 KB Output is correct
3 Correct 12 ms 12416 KB Output is correct
4 Correct 11 ms 12200 KB Output is correct
5 Correct 12 ms 12160 KB Output is correct
6 Correct 11 ms 12228 KB Output is correct
7 Correct 11 ms 12236 KB Output is correct
8 Correct 136 ms 13656 KB Output is correct
9 Correct 141 ms 13636 KB Output is correct
10 Correct 138 ms 13892 KB Output is correct
11 Correct 136 ms 14188 KB Output is correct
12 Correct 151 ms 13784 KB Output is correct
13 Correct 136 ms 14016 KB Output is correct
14 Correct 136 ms 13908 KB Output is correct
15 Correct 136 ms 14020 KB Output is correct
16 Correct 130 ms 14268 KB Output is correct
17 Correct 137 ms 13892 KB Output is correct
18 Correct 133 ms 13928 KB Output is correct
19 Correct 134 ms 13976 KB Output is correct
20 Correct 138 ms 14192 KB Output is correct
21 Correct 133 ms 14116 KB Output is correct
22 Correct 143 ms 13872 KB Output is correct
23 Correct 138 ms 13768 KB Output is correct
24 Correct 134 ms 13900 KB Output is correct
25 Correct 161 ms 13696 KB Output is correct
26 Correct 133 ms 13740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 239 ms 20452 KB Output is correct
2 Correct 274 ms 20528 KB Output is correct
3 Correct 225 ms 20500 KB Output is correct
4 Correct 268 ms 20400 KB Output is correct
5 Correct 232 ms 20420 KB Output is correct
6 Correct 203 ms 20528 KB Output is correct
7 Correct 215 ms 20792 KB Output is correct
8 Correct 208 ms 20652 KB Output is correct
9 Correct 209 ms 20688 KB Output is correct
10 Correct 196 ms 20696 KB Output is correct
11 Correct 218 ms 20708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12236 KB Output is correct
2 Correct 13 ms 12236 KB Output is correct
3 Correct 12 ms 12416 KB Output is correct
4 Correct 11 ms 12200 KB Output is correct
5 Correct 12 ms 12160 KB Output is correct
6 Correct 11 ms 12228 KB Output is correct
7 Correct 11 ms 12236 KB Output is correct
8 Correct 136 ms 13656 KB Output is correct
9 Correct 141 ms 13636 KB Output is correct
10 Correct 138 ms 13892 KB Output is correct
11 Correct 136 ms 14188 KB Output is correct
12 Correct 151 ms 13784 KB Output is correct
13 Correct 136 ms 14016 KB Output is correct
14 Correct 136 ms 13908 KB Output is correct
15 Correct 136 ms 14020 KB Output is correct
16 Correct 130 ms 14268 KB Output is correct
17 Correct 137 ms 13892 KB Output is correct
18 Correct 133 ms 13928 KB Output is correct
19 Correct 134 ms 13976 KB Output is correct
20 Correct 138 ms 14192 KB Output is correct
21 Correct 133 ms 14116 KB Output is correct
22 Correct 143 ms 13872 KB Output is correct
23 Correct 138 ms 13768 KB Output is correct
24 Correct 134 ms 13900 KB Output is correct
25 Correct 161 ms 13696 KB Output is correct
26 Correct 133 ms 13740 KB Output is correct
27 Correct 239 ms 20452 KB Output is correct
28 Correct 274 ms 20528 KB Output is correct
29 Correct 225 ms 20500 KB Output is correct
30 Correct 268 ms 20400 KB Output is correct
31 Correct 232 ms 20420 KB Output is correct
32 Correct 203 ms 20528 KB Output is correct
33 Correct 215 ms 20792 KB Output is correct
34 Correct 208 ms 20652 KB Output is correct
35 Correct 209 ms 20688 KB Output is correct
36 Correct 196 ms 20696 KB Output is correct
37 Correct 218 ms 20708 KB Output is correct
38 Correct 395 ms 40100 KB Output is correct
39 Correct 436 ms 46712 KB Output is correct
40 Correct 397 ms 33436 KB Output is correct
41 Correct 443 ms 53568 KB Output is correct
42 Correct 208 ms 27932 KB Output is correct
43 Correct 224 ms 27920 KB Output is correct
44 Correct 317 ms 37192 KB Output is correct
45 Correct 282 ms 37272 KB Output is correct
46 Correct 287 ms 37416 KB Output is correct
47 Correct 219 ms 27864 KB Output is correct
48 Correct 226 ms 27772 KB Output is correct
49 Correct 315 ms 37112 KB Output is correct
50 Correct 304 ms 37208 KB Output is correct
51 Correct 283 ms 37560 KB Output is correct
52 Correct 287 ms 36172 KB Output is correct
53 Correct 332 ms 44452 KB Output is correct
54 Correct 390 ms 52752 KB Output is correct
55 Correct 339 ms 44640 KB Output is correct