답안 #448301

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
448301 2021-07-29T13:58:32 Z dutch Long Mansion (JOI17_long_mansion) C++17
100 / 100
761 ms 60896 KB
#include <bits/stdc++.h>
using namespace std;
#define f(z) z = n + 1 - z
 
const int LIM = 5e5+1, INF = 1e9, S = 1<<19;
 
struct SegmentTree{
	vector<int> a; int l, r;
	SegmentTree(int N, int* v) : a(S+S, INF) {
		for(int i=N-0; --i>=0; ) a[i+S-1] = v[i];
		for(int i=S-1; --i>=0; ){
			a[i] = min(a[2*i+1], a[2*i+2]);
		}
	}
	int query(int x, int lx, int rx){
		if(a[x] >= r || rx <= l) return 0;
		if(rx - lx < 2) return lx;
		int mx = (lx + rx) / 2;
		return query(2*x+1, lx, mx) ? : query(2*x+2, mx, rx);
	}
	int operator()(int L, int V){ //first v[i] s.t. i>=l && v[i]<v
		l = L, r = V; return query(0, 0, S);
	}
	int rangeMin(int x, int lx, int rx){
		if(r<=lx || rx<=l) return INF;
		if(l<=lx && rx<=r) return a[x];
		int mx = (lx + rx) / 2;
		return min(rangeMin(2*x+1, lx, mx), rangeMin(2*x+2, mx, rx));
	}
	int rangeMin(int L, int R){
		l = L, r = R + 1; return rangeMin(0, 0, S);
	}
};
 
int n, q, c[LIM], last[LIM], l[LIM], r[LIM], rL[LIM], rR[LIM];
array<int, 2> b[LIM];
bool ans[LIM];
vector<int> a[LIM];
 
void flip(){
	for(int i=1; i<n; ++i){
		f(l[i]), f(r[i]);
	}
	swap(l, r);
	reverse(l+1, l+n);
	reverse(r+1, r+n);
	for(int i=1; i<n; ++i){
		f(rL[i]), f(rR[i]);
	}
	swap(rL, rR);
	reverse(rL+1, rL+n);
	reverse(rR+1, rR+n);
	for(int i=0; i<q; ++i){
		for(int &j : b[i]) f(j);
	}
}

void calc(int* v){
	for(int i=1; i<n; ++i) r[i] =-r[i];
	SegmentTree st(n+1, r);
	for(int i=1; i<n; ++i) r[i] =-r[i];
	for(int i=1; i<n; ++i){
		v[i] = min(n, st(l[i], -i));
		if(!l[i]) v[i] = 0;
	}
}
 
void solve(){
	SegmentTree st(n, rR);
	
	for(int i=0; i<q; ++i){
		auto [u, v] = b[i];
		if(u > v) continue;
		ans[i] = st.rangeMin(u, v-1) >= u;
	}
}
 
signed main(){
	cin.tie(0)->sync_with_stdio(0);
	cin >> n;
	for(int i=1; i<n; ++i) cin >> c[i];
	for(int i=1, j; i<=n; ++i){
		cin >> j;
		a[i].resize(j);
		for(int &k : a[i]) cin >> k;
	}
 
	cin >> q;
	for(int i=0; i<q; ++i){
		cin >> b[i][0] >> b[i][1];
	}
 
	for(int i=1; i<n; ++i){
		for(int &j : a[i]) last[j] = i;
		l[i] = last[c[i]];
	}
	fill(last, last+n+1, n+1);
	for(int i=n; i>1; --i){
		for(int &j : a[i]) last[j] = i;
		r[i-1] = last[c[i-1]];
	}
	calc(rR);
	flip();
	calc(rR);
 
	solve();
	flip();
	solve();
	for(int i=0; i<q; ++i){
		cout << (ans[i] ? "YES" : "NO") << '\n';
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 24136 KB Output is correct
2 Correct 28 ms 24248 KB Output is correct
3 Correct 26 ms 24264 KB Output is correct
4 Correct 26 ms 24224 KB Output is correct
5 Correct 27 ms 24136 KB Output is correct
6 Correct 26 ms 24136 KB Output is correct
7 Correct 26 ms 24136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 24136 KB Output is correct
2 Correct 28 ms 24248 KB Output is correct
3 Correct 26 ms 24264 KB Output is correct
4 Correct 26 ms 24224 KB Output is correct
5 Correct 27 ms 24136 KB Output is correct
6 Correct 26 ms 24136 KB Output is correct
7 Correct 26 ms 24136 KB Output is correct
8 Correct 270 ms 34464 KB Output is correct
9 Correct 270 ms 34324 KB Output is correct
10 Correct 283 ms 34824 KB Output is correct
11 Correct 287 ms 34988 KB Output is correct
12 Correct 250 ms 33932 KB Output is correct
13 Correct 224 ms 34508 KB Output is correct
14 Correct 227 ms 34616 KB Output is correct
15 Correct 227 ms 34496 KB Output is correct
16 Correct 229 ms 34284 KB Output is correct
17 Correct 223 ms 34640 KB Output is correct
18 Correct 237 ms 34616 KB Output is correct
19 Correct 226 ms 34572 KB Output is correct
20 Correct 222 ms 34440 KB Output is correct
21 Correct 238 ms 34428 KB Output is correct
22 Correct 235 ms 34408 KB Output is correct
23 Correct 235 ms 34268 KB Output is correct
24 Correct 225 ms 34276 KB Output is correct
25 Correct 224 ms 34348 KB Output is correct
26 Correct 231 ms 34264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 433 ms 39504 KB Output is correct
2 Correct 431 ms 41152 KB Output is correct
3 Correct 406 ms 41096 KB Output is correct
4 Correct 441 ms 41264 KB Output is correct
5 Correct 430 ms 41264 KB Output is correct
6 Correct 343 ms 40496 KB Output is correct
7 Correct 330 ms 40244 KB Output is correct
8 Correct 353 ms 40292 KB Output is correct
9 Correct 349 ms 40236 KB Output is correct
10 Correct 331 ms 40196 KB Output is correct
11 Correct 337 ms 40364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 24136 KB Output is correct
2 Correct 28 ms 24248 KB Output is correct
3 Correct 26 ms 24264 KB Output is correct
4 Correct 26 ms 24224 KB Output is correct
5 Correct 27 ms 24136 KB Output is correct
6 Correct 26 ms 24136 KB Output is correct
7 Correct 26 ms 24136 KB Output is correct
8 Correct 270 ms 34464 KB Output is correct
9 Correct 270 ms 34324 KB Output is correct
10 Correct 283 ms 34824 KB Output is correct
11 Correct 287 ms 34988 KB Output is correct
12 Correct 250 ms 33932 KB Output is correct
13 Correct 224 ms 34508 KB Output is correct
14 Correct 227 ms 34616 KB Output is correct
15 Correct 227 ms 34496 KB Output is correct
16 Correct 229 ms 34284 KB Output is correct
17 Correct 223 ms 34640 KB Output is correct
18 Correct 237 ms 34616 KB Output is correct
19 Correct 226 ms 34572 KB Output is correct
20 Correct 222 ms 34440 KB Output is correct
21 Correct 238 ms 34428 KB Output is correct
22 Correct 235 ms 34408 KB Output is correct
23 Correct 235 ms 34268 KB Output is correct
24 Correct 225 ms 34276 KB Output is correct
25 Correct 224 ms 34348 KB Output is correct
26 Correct 231 ms 34264 KB Output is correct
27 Correct 433 ms 39504 KB Output is correct
28 Correct 431 ms 41152 KB Output is correct
29 Correct 406 ms 41096 KB Output is correct
30 Correct 441 ms 41264 KB Output is correct
31 Correct 430 ms 41264 KB Output is correct
32 Correct 343 ms 40496 KB Output is correct
33 Correct 330 ms 40244 KB Output is correct
34 Correct 353 ms 40292 KB Output is correct
35 Correct 349 ms 40236 KB Output is correct
36 Correct 331 ms 40196 KB Output is correct
37 Correct 337 ms 40364 KB Output is correct
38 Correct 720 ms 57244 KB Output is correct
39 Correct 761 ms 60896 KB Output is correct
40 Correct 643 ms 52056 KB Output is correct
41 Correct 661 ms 59316 KB Output is correct
42 Correct 332 ms 41516 KB Output is correct
43 Correct 343 ms 41412 KB Output is correct
44 Correct 418 ms 47916 KB Output is correct
45 Correct 433 ms 48208 KB Output is correct
46 Correct 441 ms 48264 KB Output is correct
47 Correct 361 ms 41524 KB Output is correct
48 Correct 339 ms 41232 KB Output is correct
49 Correct 427 ms 47916 KB Output is correct
50 Correct 470 ms 48040 KB Output is correct
51 Correct 444 ms 48420 KB Output is correct
52 Correct 400 ms 46992 KB Output is correct
53 Correct 458 ms 52568 KB Output is correct
54 Correct 554 ms 58000 KB Output is correct
55 Correct 477 ms 52620 KB Output is correct