#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<cassert>
#include<unordered_map>
#include <queue>
#include <cstdint>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric>
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-lopps")
#define int long long
#define double long double
using namespace std;
const int mod=998244353,mxn=5e5+5,inf=1e18,minf=-1e18,lg=62;
//#undef int
int n,k,m,q;
void setIO(string name){
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((name+".in").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
int door[mxn+10],where[mxn+10],wheredoor[mxn+10];
vector<int>add[mxn+10],have[mxn+10];
pii ans[mxn+10];
int on[mxn+10];
int32_t main(){
fastio
int n;cin>>n;
for(int i=1;i<=n-1;i++)cin>>door[i];
for(int i=1;i<=n;i++){
int a;cin>>a;
for(int j=0;j<a;j++){
int x;cin>>x;
have[i].pb(x);
}
}
set<int>st;
for(int i=1;i<=n-1;i++){
for(auto j:have[i]){
if(on[wheredoor[j]])st.erase(st.find(wheredoor[j])),on[wheredoor[j]]=0;
where[j]=i;
}
if(where[door[i]]){
auto it=st.lb(where[door[i]]);
if(it!=st.end())add[(*it)+1].pb(i);
}
else add[1].pb(i);
st.insert(i);
on[i]=1;
wheredoor[door[i]]=i;
}
priority_queue<int,vector<int>,greater<int>>r;
r.push(n);
for(int i=1;i<=n;i++){
for(auto j:add[i])r.push(j);
while(!r.empty()&&r.top()<i)r.pop();
ans[i].s=r.top();
add[i].clear();
on[i]=0;
wheredoor[i]=0;
where[i]=0;
}
st.clear();
for(int i=n-1;i>=1;i--){
for(auto j:have[i+1]){
if(on[wheredoor[j]])st.erase(st.find(wheredoor[j])),on[wheredoor[j]]=0;
where[j]=i;
}
if(where[door[i]]){
auto it=st.upper_bound(where[door[i]]);
if(it!=st.begin())add[(*prev(it))].pb(i+1);
}
else add[n].pb(i+1);
st.insert(i);
on[i]=1;
wheredoor[door[i]]=i;
}
priority_queue<int>l;
l.push(1);
for(int i=n;i>=1;i--){
for(auto j:add[i])l.push(j);
while(!l.empty()&&l.top()>i)l.pop();
ans[i].f=l.top();
}
int q;cin>>q;
while(q--){
int a,b;cin>>a>>b;
if(b>=ans[a].f&&b<=ans[a].s)cout<<"YES\n";
else cout<<"NO\n";
}
}
/*
lets say we are at point x
if there exist a "color" of key where
door i seperates room i,i+1
the last appearance of door before x and first after x (l,r)
l<x and x<=r
and the last appearance of room with that color of key before x and first after x (l2,r2)
if l2 <= l < r < r2
then no matter how we move we cant go outside the range (l+1,r)
so door[l] and doo[r] doesnt have to be the same type
for each possible r we can find the least l where the condition is met
then every thing in range (l,r) cant move outside r
then do the same for l
*/
Compilation message
long_mansion.cpp:32:40: warning: bad option '-funroll-lopps' to pragma 'optimize' [-Wpragmas]
32 | #pragma GCC optimize ("03,unroll-lopps")
| ^
long_mansion.cpp:39:23: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
39 | void setIO(string name){
| ^
long_mansion.cpp:48:14: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
48 | int32_t main(){
| ^
long_mansion.cpp: In function 'void setIO(std::string)':
long_mansion.cpp:41:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | freopen((name+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
long_mansion.cpp:42:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | freopen((name+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
27228 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
27228 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
151 ms |
48932 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
27228 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |