답안 #564365

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
564365 2022-05-19T04:09:02 Z amunduzbaev Long Mansion (JOI17_long_mansion) C++17
100 / 100
815 ms 66464 KB
#include "bits/stdc++.h"
using namespace std;
 
#define ar array

const int N = 5e5 + 5;

struct ST{
	ar<int, 2> tree[N << 2];
	void sett(int i, int v, int lx = 0, int rx = N, int x = 1){
		if(lx == rx) { tree[x] = {v, v}; return; }
		int m = (lx + rx) >> 1;
		if(i <= m) sett(i, v, lx, m, x<<1);
		else sett(i, v, m+1, rx, x<<1|1);
		tree[x][0] = min(tree[x<<1][0], tree[x<<1|1][0]);
		tree[x][1] = max(tree[x<<1][1], tree[x<<1|1][1]);
	}
	
	//~ int Max(int l, int r, int lx = 0, int rx = N, int x = 1){
		//~ if(lx > r || rx < l) return -N;
		//~ if(lx >= l && rx <= r) return tree[x][1];
		//~ int m = (lx + rx) >> 1;
		//~ return max(Max(l, r, lx, m, x<<1), Max(l, r, m+1, rx, x<<1|1));
	//~ }
	
	int Min(int l, int r, int lx = 0, int rx = N, int x = 1){
		if(lx > r || rx < l) return N;
		if(lx >= l && rx <= r) return tree[x][0];
		int m = (lx + rx) >> 1;
		return min(Min(l, r, lx, m, x<<1), Min(l, r, m+1, rx, x<<1|1));
	}
	
	int get(int l, int r, int v, int lx = 0, int rx = N, int x = 1){
		if(lx > r || rx < l) return -1;
		if(lx >= l && rx <= r){
			if(v > tree[x][1]) return -1;
			if(lx == rx) return lx;
			int m = (lx + rx) >> 1;
			if(tree[x<<1][1] >= v) return get(l, r, v, lx, m, x<<1);
			else return get(l, r, v, m+1, rx, x<<1|1);
		} int m = (lx + rx) >> 1;
		int R = get(l, r, v, lx, m, x<<1);
		if(~R) return R;
		return get(l, r, v, m+1, rx, x<<1|1);
	}
}tree;

vector<int> a[N];
int b[N], c[N], x[N], y[N];
int q, n, is[N], lp[N], rp[N];
int last[N], val[N];
int tmp[N];

void solve(){
	memset(last, 0, sizeof last);
	for(int i=1;i<n;i++){
		for(auto x : a[i]){
			last[x] = i;
		}
		
		lp[i] = last[c[i]];
	}
	
	//~ for(int i=1;i<n;i++) cout<<c[i]<<" ";
	//~ cout<<"\n";
	//~ for(int i=1;i<=n;i++){
		//~ for(auto x : a[i]) cout<<x<<" ";
		//~ cout<<"\n";
	//~ }
	
	memset(last, 0, sizeof last);
	for(int i=n-1;i>0;i--){
		for(auto x : a[i+1]){
			last[x] = i + 1;
		}
		rp[i] = last[c[i]];
		if(rp[i]) tree.sett(i, rp[i]);
		else tree.sett(i, N);
	}
	//~ for(int i=1;i<n;i++) cout<<lp[i]<<" ";
	//~ cout<<"\n";
	
	for(int i=1;i<n;i++){
		if(lp[i]){
			if(lp[i] < i){
				val[i] = tree.get(lp[i], i - 1, i + 1);
				if(val[i] < 0) val[i] = N;
			} else val[i] = N;
		} else {
			val[i] = -1;
		}
	}
	for(int i=1;i<n;i++){
		tree.sett(i, val[i]);
	}
	
	for(int i=0;i<q;i++){
		if(x[i] > y[i]){
			continue;
		}
		
		y[i]--;
		is[i] |= (tree.Min(x[i], y[i]) >= x[i]);
		y[i]++;
		//~ int mx = -1;
		//~ for(int j=x[i]-1;j>0;j--){
			//~ if(!rp[j]) mx = N;
			//~ mx = max(mx, rp[j]);
			//~ tmp[j] = mx;
		//~ }
		//~ bool ok = 1;
		//~ for(int j=x[i];j<y[i];j++){
			//~ if(!lp[j]) ok = 0;
			//~ if(lp[j] < x[i] && j < tmp[lp[j]]){
				//~ ok = 0;
			//~ }
		//~ }
		
		//~ is[i] |= ok;
	}
}

signed main(){
	ios::sync_with_stdio(0); cin.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 j=0;j<b[i];j++){
			cin>>a[i][j];
		}
	}
	
	cin>>q;
	for(int i=0;i<q;i++){
		cin>>x[i]>>y[i];
	}
	
	solve();
	for(int i=1;i<n-i;i++){
		swap(c[i], c[n-i]);
	}
	
	for(int i=1;i<n-i+1;i++){
		swap(b[i], b[n-i+1]);
		swap(a[i], a[n-i+1]);
	}
	
	for(int i=0;i<q;i++){
		x[i] = n - x[i] + 1;
		y[i] = n - y[i] + 1;
	}
	
	solve();
	for(int i=0;i<q;i++){
		if(is[i]) cout<<"YES\n";
		else cout<<"NO\n";
	}
}

# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 14292 KB Output is correct
2 Correct 16 ms 14440 KB Output is correct
3 Correct 15 ms 14472 KB Output is correct
4 Correct 14 ms 14316 KB Output is correct
5 Correct 14 ms 14388 KB Output is correct
6 Correct 15 ms 14388 KB Output is correct
7 Correct 12 ms 14420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 14292 KB Output is correct
2 Correct 16 ms 14440 KB Output is correct
3 Correct 15 ms 14472 KB Output is correct
4 Correct 14 ms 14316 KB Output is correct
5 Correct 14 ms 14388 KB Output is correct
6 Correct 15 ms 14388 KB Output is correct
7 Correct 12 ms 14420 KB Output is correct
8 Correct 191 ms 21736 KB Output is correct
9 Correct 193 ms 21792 KB Output is correct
10 Correct 214 ms 22016 KB Output is correct
11 Correct 214 ms 22220 KB Output is correct
12 Correct 216 ms 21908 KB Output is correct
13 Correct 169 ms 21984 KB Output is correct
14 Correct 163 ms 21928 KB Output is correct
15 Correct 181 ms 22048 KB Output is correct
16 Correct 173 ms 22176 KB Output is correct
17 Correct 195 ms 21964 KB Output is correct
18 Correct 170 ms 22028 KB Output is correct
19 Correct 182 ms 21984 KB Output is correct
20 Correct 176 ms 22188 KB Output is correct
21 Correct 189 ms 22240 KB Output is correct
22 Correct 167 ms 22008 KB Output is correct
23 Correct 194 ms 21816 KB Output is correct
24 Correct 203 ms 21788 KB Output is correct
25 Correct 176 ms 21804 KB Output is correct
26 Correct 160 ms 21800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 364 ms 28356 KB Output is correct
2 Correct 344 ms 28920 KB Output is correct
3 Correct 352 ms 29008 KB Output is correct
4 Correct 343 ms 28936 KB Output is correct
5 Correct 382 ms 28912 KB Output is correct
6 Correct 273 ms 29016 KB Output is correct
7 Correct 295 ms 34752 KB Output is correct
8 Correct 307 ms 34680 KB Output is correct
9 Correct 294 ms 34700 KB Output is correct
10 Correct 304 ms 34792 KB Output is correct
11 Correct 289 ms 34652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 14292 KB Output is correct
2 Correct 16 ms 14440 KB Output is correct
3 Correct 15 ms 14472 KB Output is correct
4 Correct 14 ms 14316 KB Output is correct
5 Correct 14 ms 14388 KB Output is correct
6 Correct 15 ms 14388 KB Output is correct
7 Correct 12 ms 14420 KB Output is correct
8 Correct 191 ms 21736 KB Output is correct
9 Correct 193 ms 21792 KB Output is correct
10 Correct 214 ms 22016 KB Output is correct
11 Correct 214 ms 22220 KB Output is correct
12 Correct 216 ms 21908 KB Output is correct
13 Correct 169 ms 21984 KB Output is correct
14 Correct 163 ms 21928 KB Output is correct
15 Correct 181 ms 22048 KB Output is correct
16 Correct 173 ms 22176 KB Output is correct
17 Correct 195 ms 21964 KB Output is correct
18 Correct 170 ms 22028 KB Output is correct
19 Correct 182 ms 21984 KB Output is correct
20 Correct 176 ms 22188 KB Output is correct
21 Correct 189 ms 22240 KB Output is correct
22 Correct 167 ms 22008 KB Output is correct
23 Correct 194 ms 21816 KB Output is correct
24 Correct 203 ms 21788 KB Output is correct
25 Correct 176 ms 21804 KB Output is correct
26 Correct 160 ms 21800 KB Output is correct
27 Correct 364 ms 28356 KB Output is correct
28 Correct 344 ms 28920 KB Output is correct
29 Correct 352 ms 29008 KB Output is correct
30 Correct 343 ms 28936 KB Output is correct
31 Correct 382 ms 28912 KB Output is correct
32 Correct 273 ms 29016 KB Output is correct
33 Correct 295 ms 34752 KB Output is correct
34 Correct 307 ms 34680 KB Output is correct
35 Correct 294 ms 34700 KB Output is correct
36 Correct 304 ms 34792 KB Output is correct
37 Correct 289 ms 34652 KB Output is correct
38 Correct 734 ms 59960 KB Output is correct
39 Correct 815 ms 66464 KB Output is correct
40 Correct 640 ms 52136 KB Output is correct
41 Correct 729 ms 64920 KB Output is correct
42 Correct 248 ms 35848 KB Output is correct
43 Correct 283 ms 35844 KB Output is correct
44 Correct 362 ms 45172 KB Output is correct
45 Correct 378 ms 45300 KB Output is correct
46 Correct 413 ms 45528 KB Output is correct
47 Correct 308 ms 35916 KB Output is correct
48 Correct 291 ms 35708 KB Output is correct
49 Correct 403 ms 45168 KB Output is correct
50 Correct 464 ms 45236 KB Output is correct
51 Correct 451 ms 45552 KB Output is correct
52 Correct 387 ms 44188 KB Output is correct
53 Correct 459 ms 52636 KB Output is correct
54 Correct 575 ms 60956 KB Output is correct
55 Correct 474 ms 52736 KB Output is correct