Submission #564357

# Submission time Handle Problem Language Result Execution time Memory
564357 2022-05-19T03:45:12 Z amunduzbaev Long Mansion (JOI17_long_mansion) C++17
0 / 100
392 ms 35736 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];

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]];
		tree.sett(i, rp[i]);
	}
	//~ 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++){
		//~ cout<<lp[i]<<" "<<rp[i]<<"\n";
		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]++;
	}
}

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";
	}
}

# Verdict Execution time Memory Grader output
1 Correct 14 ms 14400 KB Output is correct
2 Correct 12 ms 14420 KB Output is correct
3 Correct 14 ms 14624 KB Output is correct
4 Incorrect 11 ms 14380 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 14 ms 14400 KB Output is correct
2 Correct 12 ms 14420 KB Output is correct
3 Correct 14 ms 14624 KB Output is correct
4 Incorrect 11 ms 14380 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 383 ms 35692 KB Output is correct
2 Correct 344 ms 35580 KB Output is correct
3 Correct 351 ms 35492 KB Output is correct
4 Correct 357 ms 35696 KB Output is correct
5 Correct 392 ms 35736 KB Output is correct
6 Incorrect 277 ms 34968 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 14 ms 14400 KB Output is correct
2 Correct 12 ms 14420 KB Output is correct
3 Correct 14 ms 14624 KB Output is correct
4 Incorrect 11 ms 14380 KB Output isn't correct
5 Halted 0 ms 0 KB -