#include<bits/stdc++.h>
using namespace std;
template<class T> void chmin(T& a,const T& b) { if(a>b) a=b; }
template<class T> void chmax(T& a,const T& b) { if(a<b) a=b; }
typedef pair<int,int> pi;
const int INF=5e8;
const int MAX_N=500005;
struct uf{
int par[MAX_N];
int size[MAX_N];
void init(){
memset(par,-1,sizeof(par));
for(int i=1;i<MAX_N;++i) size[i]=1;
}
int root(int a){
if(par[a]==-1) return a;
return par[a]=root(par[a]);
}
void unite(int a,int b){
a=root(a);b=root(b);
if(a==b) return;
par[b]=a;
size[a]+=size[b];
}
bool same(int a,int b){
return root(a)==root(b);
}
};
uf u;
int n,q;
int ar[MAX_N];
vector<int> keys[MAX_N];
pi query[MAX_N];
bool vis[MAX_N];
pi memo[MAX_N];
vector<int> pos[MAX_N];
bool open(int id,int l,int r){
return (*lower_bound(pos[id].begin(),pos[id].end(),l)<=r);
}
pair<int,pi> rec(int p){//(id,(l,r))
if(vis[p]){
return {p,{p,p}};
}
if(memo[p].first>=0){
return {-1,memo[p]};
}
if(u.root(p)!=p){
return rec(u.root(p));
}
vis[p]=1;
int l=p,r=p;
while(1){
if(l>0 && open(ar[l-1],l,r)){
auto range=rec(l-1);
chmin(l,range.second.first);
chmax(r,range.second.second);
if(range.first>=0 && range.first!=p){
u.unite(range.first,p);
vis[p]=false;
return {range.first,{l,r}};
}
continue;
}
if(r+1<n && open(ar[r],l,r)){
auto range=rec(r+1);
chmin(l,range.second.first);
chmax(r,range.second.second);
if(range.first>=0 && range.first!=p){
u.unite(range.first,p);
vis[p]=false;
return {range.first,{l,r}};
}
continue;
}
break;
}
vis[p]=0;
return {-1, memo[p]={l,r} };
}
int main(){
u.init();
cin>>n;
for(int i=0;i<n-1;++i) scanf("%d",&ar[i]),--ar[i];
int sum=0;
for(int i=0;i<n;++i){
int K;scanf("%d",&K);
keys[i].resize(K);
sum+=K;
for(int j=0;j<K;++j){
scanf("%d",&keys[i][j]);
--keys[i][j];
pos[keys[i][j]].push_back(i);
}
}
for(int i=0;i<n;++i) pos[i].push_back(INF);
memset(memo,-1,sizeof(memo));
for(int i=0;i<n;++i) rec(i);
for(int i=0;i<n;++i) if(u.root(i)!=i){
memo[i]=memo[u.root(i)];
}
cin>>q;
for(int i=0;i<q;++i){
int l,r;scanf("%d%d",&l,&r);--l;--r;
if(memo[l].first<=r && r<=memo[l].second) puts("YES");
else puts("NO");
}
return 0;
}
Compilation message
long_mansion.cpp: In function 'int main()':
long_mansion.cpp:91:52: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=0;i<n-1;++i) scanf("%d",&ar[i]),--ar[i];
^
long_mansion.cpp:94:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int K;scanf("%d",&K);
^
long_mansion.cpp:98:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&keys[i][j]);
^
long_mansion.cpp:111:32: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int l,r;scanf("%d%d",&l,&r);--l;--r;
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
39752 KB |
Output is correct |
2 |
Correct |
13 ms |
39884 KB |
Output is correct |
3 |
Correct |
13 ms |
40016 KB |
Output is correct |
4 |
Correct |
13 ms |
39752 KB |
Output is correct |
5 |
Correct |
16 ms |
39752 KB |
Output is correct |
6 |
Correct |
9 ms |
39752 KB |
Output is correct |
7 |
Correct |
6 ms |
39752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
39752 KB |
Output is correct |
2 |
Correct |
13 ms |
39884 KB |
Output is correct |
3 |
Correct |
13 ms |
40016 KB |
Output is correct |
4 |
Correct |
13 ms |
39752 KB |
Output is correct |
5 |
Correct |
16 ms |
39752 KB |
Output is correct |
6 |
Correct |
9 ms |
39752 KB |
Output is correct |
7 |
Correct |
6 ms |
39752 KB |
Output is correct |
8 |
Correct |
159 ms |
39752 KB |
Output is correct |
9 |
Correct |
139 ms |
39752 KB |
Output is correct |
10 |
Correct |
146 ms |
39884 KB |
Output is correct |
11 |
Correct |
153 ms |
40016 KB |
Output is correct |
12 |
Correct |
129 ms |
39752 KB |
Output is correct |
13 |
Correct |
136 ms |
39752 KB |
Output is correct |
14 |
Correct |
149 ms |
39752 KB |
Output is correct |
15 |
Correct |
126 ms |
39752 KB |
Output is correct |
16 |
Correct |
126 ms |
39752 KB |
Output is correct |
17 |
Correct |
149 ms |
39752 KB |
Output is correct |
18 |
Correct |
146 ms |
39752 KB |
Output is correct |
19 |
Correct |
129 ms |
39752 KB |
Output is correct |
20 |
Correct |
136 ms |
39752 KB |
Output is correct |
21 |
Correct |
109 ms |
39752 KB |
Output is correct |
22 |
Correct |
133 ms |
39752 KB |
Output is correct |
23 |
Correct |
136 ms |
39752 KB |
Output is correct |
24 |
Correct |
133 ms |
39752 KB |
Output is correct |
25 |
Correct |
139 ms |
39752 KB |
Output is correct |
26 |
Correct |
133 ms |
39752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
296 ms |
48708 KB |
Output is correct |
2 |
Correct |
299 ms |
48000 KB |
Output is correct |
3 |
Correct |
303 ms |
48600 KB |
Output is correct |
4 |
Correct |
303 ms |
48404 KB |
Output is correct |
5 |
Correct |
293 ms |
48312 KB |
Output is correct |
6 |
Correct |
256 ms |
47232 KB |
Output is correct |
7 |
Correct |
246 ms |
47192 KB |
Output is correct |
8 |
Correct |
226 ms |
47208 KB |
Output is correct |
9 |
Correct |
223 ms |
47232 KB |
Output is correct |
10 |
Correct |
243 ms |
47104 KB |
Output is correct |
11 |
Correct |
233 ms |
47144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
39752 KB |
Output is correct |
2 |
Correct |
13 ms |
39884 KB |
Output is correct |
3 |
Correct |
13 ms |
40016 KB |
Output is correct |
4 |
Correct |
13 ms |
39752 KB |
Output is correct |
5 |
Correct |
16 ms |
39752 KB |
Output is correct |
6 |
Correct |
9 ms |
39752 KB |
Output is correct |
7 |
Correct |
6 ms |
39752 KB |
Output is correct |
8 |
Correct |
159 ms |
39752 KB |
Output is correct |
9 |
Correct |
139 ms |
39752 KB |
Output is correct |
10 |
Correct |
146 ms |
39884 KB |
Output is correct |
11 |
Correct |
153 ms |
40016 KB |
Output is correct |
12 |
Correct |
129 ms |
39752 KB |
Output is correct |
13 |
Correct |
136 ms |
39752 KB |
Output is correct |
14 |
Correct |
149 ms |
39752 KB |
Output is correct |
15 |
Correct |
126 ms |
39752 KB |
Output is correct |
16 |
Correct |
126 ms |
39752 KB |
Output is correct |
17 |
Correct |
149 ms |
39752 KB |
Output is correct |
18 |
Correct |
146 ms |
39752 KB |
Output is correct |
19 |
Correct |
129 ms |
39752 KB |
Output is correct |
20 |
Correct |
136 ms |
39752 KB |
Output is correct |
21 |
Correct |
109 ms |
39752 KB |
Output is correct |
22 |
Correct |
133 ms |
39752 KB |
Output is correct |
23 |
Correct |
136 ms |
39752 KB |
Output is correct |
24 |
Correct |
133 ms |
39752 KB |
Output is correct |
25 |
Correct |
139 ms |
39752 KB |
Output is correct |
26 |
Correct |
133 ms |
39752 KB |
Output is correct |
27 |
Correct |
296 ms |
48708 KB |
Output is correct |
28 |
Correct |
299 ms |
48000 KB |
Output is correct |
29 |
Correct |
303 ms |
48600 KB |
Output is correct |
30 |
Correct |
303 ms |
48404 KB |
Output is correct |
31 |
Correct |
293 ms |
48312 KB |
Output is correct |
32 |
Correct |
256 ms |
47232 KB |
Output is correct |
33 |
Correct |
246 ms |
47192 KB |
Output is correct |
34 |
Correct |
226 ms |
47208 KB |
Output is correct |
35 |
Correct |
223 ms |
47232 KB |
Output is correct |
36 |
Correct |
243 ms |
47104 KB |
Output is correct |
37 |
Correct |
233 ms |
47144 KB |
Output is correct |
38 |
Correct |
546 ms |
67472 KB |
Output is correct |
39 |
Correct |
536 ms |
73544 KB |
Output is correct |
40 |
Correct |
429 ms |
61268 KB |
Output is correct |
41 |
Correct |
569 ms |
72880 KB |
Output is correct |
42 |
Correct |
299 ms |
46352 KB |
Output is correct |
43 |
Correct |
263 ms |
46748 KB |
Output is correct |
44 |
Correct |
386 ms |
53348 KB |
Output is correct |
45 |
Correct |
429 ms |
53348 KB |
Output is correct |
46 |
Correct |
449 ms |
53216 KB |
Output is correct |
47 |
Correct |
266 ms |
46616 KB |
Output is correct |
48 |
Correct |
256 ms |
47012 KB |
Output is correct |
49 |
Correct |
426 ms |
54140 KB |
Output is correct |
50 |
Correct |
453 ms |
53612 KB |
Output is correct |
51 |
Correct |
469 ms |
53084 KB |
Output is correct |
52 |
Correct |
313 ms |
52432 KB |
Output is correct |
53 |
Correct |
373 ms |
59152 KB |
Output is correct |
54 |
Correct |
459 ms |
64964 KB |
Output is correct |
55 |
Correct |
386 ms |
58556 KB |
Output is correct |