답안 #412754

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
412754 2021-05-27T12:59:52 Z amoo_safar Long Mansion (JOI17_long_mansion) C++17
25 / 100
849 ms 42052 KB
// vaziat meshki-ghermeze !
#include <bits/stdc++.h>

#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'

using namespace std;

typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;

const ll Mod = 1000000007LL;
const int N = 2e5 + 10;
const ll Inf = 2242545357980376863LL;
const int Log = 20;

int n;
int L[Log][N], R[Log][N];
int C[N];

vector<int> B[N];

int vis[N];

typedef pair<int, int> pii;
map<pii, pii> mp;

pii Solve(pii A){
	// cerr << "^ " << A.F << ' ' << A.S << '\n';
	if(mp.count(A))
		return mp[A];
	int l = A.F, r = A.S;
	
	for(int lg = Log - 1; lg >= 0; lg --){
		if(l <= L[lg][r + 1])
			r += (1 << lg);
	}
	for(int lg = Log - 1; lg >= 0; lg --){
		if(l <= (1 << lg)) continue;
		if(R[lg][l - (1 << lg)] <= r)
			l -= (1 << lg);
	}
	if(r == n)
		return mp[A] = {l, r};
	int mt = L[0][r + 1];
	if(mt < l)
		return mp[A] = {l, r};
	l = mt;
	r ++;
	// cerr << "# " << l << ", " << r << '\n';
	return mp[A] = Solve({l, r});
}

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++){
		int sz;
		cin >> sz;
		int v;
		for(int j = 0; j < sz; j++){
			cin >> v;
			B[i].pb(v);
		}
	}

	// cerr << "! ";
	for(int i = 1; i <= n; i++){
		L[0][i] = vis[C[i - 1]];
		for(auto x : B[i])
			vis[x] = i;
		// cerr << L[0][i] << ' ';
	}
	L[0][n + 1] = 0;
	// cerr << '\n';
	
	fill(vis, vis + N, n + 1);
	// cerr << "# ";
	for(int i = n; i >= 1; i--){
		R[0][i] = vis[C[i]];
		for(auto x : B[i])
			vis[x] = i;
		// cerr << R[0][i] << ' ';
	}
	R[0][0] = n + 1;
	
	// cerr << '\n';
	for(int l = 1; l < Log; l++){
		for(int i = 1; i <= n + 1; i++){
			L[l][i] = L[l - 1][i];
			if(i + (1 << (l - 1)) <= n + 1)
				L[l][i] = min(L[l][i], L[l - 1][i + (1 << (l - 1))]);
		}
	}

	for(int l = 1; l < Log; l++){
		for(int i = 0; i <= n; i++){
			R[l][i] = R[l - 1][i];
			if(i + (1 << (l - 1)) <= n)
				R[l][i] = max(R[l][i], R[l - 1][i + (1 << (l - 1))]);
		}
	}

	// auto [l,r] = Solve({3, 3});
	// cerr << "!!! [" << l << ", " << r << "]\n";
	// exit(0);
	int q;
	cin >> q;
	int x, y;

	for(int i = 0; i < q; i++){
		cin >> x >> y;
		auto [l, r] = Solve({x, x});
		// debug(l);
		// debug(r);
		cout << (l <= y && y <= r ? "YES" : "NO") << '\n';
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 6604 KB Output is correct
2 Correct 9 ms 6860 KB Output is correct
3 Correct 10 ms 7244 KB Output is correct
4 Correct 9 ms 6536 KB Output is correct
5 Correct 8 ms 6476 KB Output is correct
6 Correct 8 ms 6476 KB Output is correct
7 Correct 8 ms 6584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 6604 KB Output is correct
2 Correct 9 ms 6860 KB Output is correct
3 Correct 10 ms 7244 KB Output is correct
4 Correct 9 ms 6536 KB Output is correct
5 Correct 8 ms 6476 KB Output is correct
6 Correct 8 ms 6476 KB Output is correct
7 Correct 8 ms 6584 KB Output is correct
8 Correct 229 ms 12424 KB Output is correct
9 Correct 237 ms 12408 KB Output is correct
10 Correct 232 ms 12916 KB Output is correct
11 Correct 248 ms 13784 KB Output is correct
12 Correct 193 ms 11784 KB Output is correct
13 Correct 225 ms 12644 KB Output is correct
14 Correct 189 ms 12620 KB Output is correct
15 Correct 190 ms 12592 KB Output is correct
16 Correct 204 ms 12368 KB Output is correct
17 Correct 206 ms 12568 KB Output is correct
18 Correct 192 ms 12584 KB Output is correct
19 Correct 199 ms 12720 KB Output is correct
20 Correct 194 ms 12556 KB Output is correct
21 Correct 202 ms 12472 KB Output is correct
22 Correct 189 ms 12484 KB Output is correct
23 Correct 243 ms 12428 KB Output is correct
24 Correct 223 ms 12424 KB Output is correct
25 Correct 271 ms 12448 KB Output is correct
26 Correct 233 ms 12488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 814 ms 41656 KB Output is correct
2 Correct 800 ms 41828 KB Output is correct
3 Correct 823 ms 42052 KB Output is correct
4 Correct 779 ms 41772 KB Output is correct
5 Correct 849 ms 41872 KB Output is correct
6 Correct 519 ms 39184 KB Output is correct
7 Correct 501 ms 39168 KB Output is correct
8 Correct 476 ms 39124 KB Output is correct
9 Correct 469 ms 38976 KB Output is correct
10 Correct 503 ms 38956 KB Output is correct
11 Correct 538 ms 38900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 6604 KB Output is correct
2 Correct 9 ms 6860 KB Output is correct
3 Correct 10 ms 7244 KB Output is correct
4 Correct 9 ms 6536 KB Output is correct
5 Correct 8 ms 6476 KB Output is correct
6 Correct 8 ms 6476 KB Output is correct
7 Correct 8 ms 6584 KB Output is correct
8 Correct 229 ms 12424 KB Output is correct
9 Correct 237 ms 12408 KB Output is correct
10 Correct 232 ms 12916 KB Output is correct
11 Correct 248 ms 13784 KB Output is correct
12 Correct 193 ms 11784 KB Output is correct
13 Correct 225 ms 12644 KB Output is correct
14 Correct 189 ms 12620 KB Output is correct
15 Correct 190 ms 12592 KB Output is correct
16 Correct 204 ms 12368 KB Output is correct
17 Correct 206 ms 12568 KB Output is correct
18 Correct 192 ms 12584 KB Output is correct
19 Correct 199 ms 12720 KB Output is correct
20 Correct 194 ms 12556 KB Output is correct
21 Correct 202 ms 12472 KB Output is correct
22 Correct 189 ms 12484 KB Output is correct
23 Correct 243 ms 12428 KB Output is correct
24 Correct 223 ms 12424 KB Output is correct
25 Correct 271 ms 12448 KB Output is correct
26 Correct 233 ms 12488 KB Output is correct
27 Correct 814 ms 41656 KB Output is correct
28 Correct 800 ms 41828 KB Output is correct
29 Correct 823 ms 42052 KB Output is correct
30 Correct 779 ms 41772 KB Output is correct
31 Correct 849 ms 41872 KB Output is correct
32 Correct 519 ms 39184 KB Output is correct
33 Correct 501 ms 39168 KB Output is correct
34 Correct 476 ms 39124 KB Output is correct
35 Correct 469 ms 38976 KB Output is correct
36 Correct 503 ms 38956 KB Output is correct
37 Correct 538 ms 38900 KB Output is correct
38 Runtime error 103 ms 29628 KB Execution killed with signal 11
39 Halted 0 ms 0 KB -