#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=(int)5e5;
int x[N+2],y[N+2],c[N+2],b[N+2];
vector<int>arr[N+2];
int n,q;
namespace subtask1{
bool check(){
return n<=5000;
}
const int LIMIT=5000;
bool f[LIMIT+2][LIMIT+2];
bool cnt[N+2]={};
void main_code(){
for(int i =1;i<=n;++i){
for(int j=0;j<=n;++j) cnt[j]=false;
int l=i,r=i;
bool nxt=true;
for(auto&x:arr[i]) cnt[x]=true;
while (nxt){
nxt=false;
while (r<n && cnt[c[r]]){
for(auto&x:arr[r+1]) cnt[x]=true;
f[i][r+1]=true;
nxt=true;
++r;
}
while (l>1 && cnt[c[l-1]]){
for(auto&x:arr[l-1]) cnt[x]=true;
f[i][l-1]=true;
nxt=true;
--l;
}
}
}
for(int i=1;i<=q;++i){
cout<<(f[x[i]][y[i]]?"YES":"NO")<<'\n';
}
}
}
namespace subtask2{
bool check(){
return *max_element(c+1,c+n+1)<=20;
}
const int MAXLOG=20;
int pre[N+2][MAXLOG+2]={};
int cur[N+2][MAXLOG+2]={};
int lef[N+2],rig[N+2];
bool possible(int mask,int l,int r){
for(int j=0;j<MAXLOG;++j){
if (pre[r][j]-pre[l-1][j]>0){
if ((mask>>j)&1) continue;
return false;
}
}
return true;
}
int lefmost(int mask,int id){
int low=1,high=id-1,pos=id;
while (low<=high){
int mid=(low+high)/2;
if (possible(mask,mid,id-1)) pos=mid,high=mid-1;
else low=mid+1;
}
return pos;
}
int rigmost(int mask,int id){
int low=id,high=n-1,pos=id-1;
while (low<=high){
int mid=(low+high)/2;
if (possible(mask,id,mid)) pos=mid,low=mid+1;
else high=mid-1;
}
return pos+1;
}
void main_code(){
for(int i=1;i<n;++i) {
for(int j=0;j<MAXLOG;++j) pre[i][j]=pre[i-1][j];
pre[i][c[i]-1]++;
}
for(int i=1;i<=n;++i){
for(int j=0;j<MAXLOG;++j) cur[i][j]=cur[i-1][j];
for(auto&x:arr[i]) cur[i][x-1]++;
}
for(int i=1;i<=n;++i){
bool nxt=true;
int curmask=0;
for(auto&x:arr[i]) curmask|=(1<<(x-1));
int l=i,r=i;
while (nxt){
nxt=false;
int nxt_l=lefmost(curmask,l);
if (l>1 && nxt_l!=l){
nxt=true;
for(int j=0;j<MAXLOG;++j){
if (cur[l][j]-cur[nxt_l-1][j]>0) curmask|=(1<<j);
}
l=nxt_l;
}
int nxt_r=rigmost(curmask,r);
if (r<n && nxt_r!=r){
nxt=true;
for(int j=0;j<MAXLOG;++j){
if (cur[nxt_r][j]-cur[r-1][j]>0) curmask|=(1<<j);
}
r=nxt_r;
}
}
lef[i]=l,rig[i]=r;
}
for(int i=1;i<=q;++i){
bool ok=(lef[x[i]] <= y[i] && y[i]<=rig[x[i]]);
cout<<(ok?"YES":"NO")<<'\n';
}
}
}
namespace subtask3{
int lef[N+2],rig[N+2];
vector<int>pos[N+2];
bool inside(int l,int r,int x){
int p=lower_bound(pos[x].begin(),pos[x].end(),l)-pos[x].begin();
if (p>=pos[x].size()) return false;
return l<=pos[x][p] && pos[x][p]<=r;
}
void main_code(){
for(int i=1;i<=n;++i){
for(auto&x:arr[i]) {
pos[x].push_back(i);
}
}
for(int i=1;i<=n;++i) sort(pos[i].begin(),pos[i].end());
for(int i=1;i<=n;++i){
lef[i]=rig[i]=i;
bool nxt=true;
while (nxt){
nxt=false;
while (lef[i]>1 && inside(lef[i],rig[i],c[lef[i]-1])){
rig[i]=max(rig[i],rig[lef[i]-1]);
lef[i]=lef[lef[i]-1];
nxt=true;
}
while (rig[i]<n && inside(lef[i],rig[i],c[rig[i]])) rig[i]++,nxt=true;
}
}
for(int i=1;i<=q;++i){
bool ok=(lef[x[i]] <= y[i] && y[i]<=rig[x[i]]);
cout<<(ok?"YES":"NO")<<'\n';
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0) ; cout.tie(0);
#define task "main"
if (fopen(task".inp","r")){
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin>>n;
for(int i=1;i<n;++i) cin>>c[i];
for(int i=1;i<=n;++i){
cin>>b[i];
arr[i].resize(b[i]);
for(auto&x:arr[i]) cin>>x;
}
cin>>q;
for(int i=1;i<=q;++i) cin>>x[i]>>y[i];
return subtask3::main_code(),0;
assert(false);
return 0;
}
Compilation message (stderr)
long_mansion.cpp: In function 'int main()':
long_mansion.cpp:174:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
174 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
long_mansion.cpp:175:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
175 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |