Submission #564364

#TimeUsernameProblemLanguageResultExecution timeMemory
564364amunduzbaevLong Mansion (JOI17_long_mansion)C++17
10 / 100
3090 ms26608 KiB
#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]]; //~ 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++){ //~ 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"; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...