#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:44: 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:16: 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:12: 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:18: 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;
~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
31864 KB |
Output is correct |
2 |
Correct |
34 ms |
31992 KB |
Output is correct |
3 |
Correct |
43 ms |
32248 KB |
Output is correct |
4 |
Correct |
42 ms |
31864 KB |
Output is correct |
5 |
Correct |
33 ms |
31864 KB |
Output is correct |
6 |
Correct |
35 ms |
31872 KB |
Output is correct |
7 |
Correct |
45 ms |
31864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
31864 KB |
Output is correct |
2 |
Correct |
34 ms |
31992 KB |
Output is correct |
3 |
Correct |
43 ms |
32248 KB |
Output is correct |
4 |
Correct |
42 ms |
31864 KB |
Output is correct |
5 |
Correct |
33 ms |
31864 KB |
Output is correct |
6 |
Correct |
35 ms |
31872 KB |
Output is correct |
7 |
Correct |
45 ms |
31864 KB |
Output is correct |
8 |
Correct |
174 ms |
37776 KB |
Output is correct |
9 |
Correct |
156 ms |
37700 KB |
Output is correct |
10 |
Correct |
170 ms |
38008 KB |
Output is correct |
11 |
Correct |
176 ms |
38524 KB |
Output is correct |
12 |
Correct |
161 ms |
37368 KB |
Output is correct |
13 |
Correct |
196 ms |
37924 KB |
Output is correct |
14 |
Correct |
170 ms |
37852 KB |
Output is correct |
15 |
Correct |
172 ms |
38008 KB |
Output is correct |
16 |
Correct |
144 ms |
37752 KB |
Output is correct |
17 |
Correct |
157 ms |
37880 KB |
Output is correct |
18 |
Correct |
170 ms |
37980 KB |
Output is correct |
19 |
Correct |
165 ms |
37892 KB |
Output is correct |
20 |
Correct |
172 ms |
38008 KB |
Output is correct |
21 |
Correct |
155 ms |
37752 KB |
Output is correct |
22 |
Correct |
141 ms |
37752 KB |
Output is correct |
23 |
Correct |
145 ms |
37708 KB |
Output is correct |
24 |
Correct |
157 ms |
37672 KB |
Output is correct |
25 |
Correct |
150 ms |
37772 KB |
Output is correct |
26 |
Correct |
182 ms |
37752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
418 ms |
47844 KB |
Output is correct |
2 |
Correct |
358 ms |
49144 KB |
Output is correct |
3 |
Correct |
332 ms |
48828 KB |
Output is correct |
4 |
Correct |
363 ms |
49396 KB |
Output is correct |
5 |
Correct |
358 ms |
49260 KB |
Output is correct |
6 |
Correct |
265 ms |
47608 KB |
Output is correct |
7 |
Correct |
303 ms |
47532 KB |
Output is correct |
8 |
Correct |
283 ms |
47488 KB |
Output is correct |
9 |
Correct |
355 ms |
47452 KB |
Output is correct |
10 |
Correct |
253 ms |
47412 KB |
Output is correct |
11 |
Correct |
257 ms |
47352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
31864 KB |
Output is correct |
2 |
Correct |
34 ms |
31992 KB |
Output is correct |
3 |
Correct |
43 ms |
32248 KB |
Output is correct |
4 |
Correct |
42 ms |
31864 KB |
Output is correct |
5 |
Correct |
33 ms |
31864 KB |
Output is correct |
6 |
Correct |
35 ms |
31872 KB |
Output is correct |
7 |
Correct |
45 ms |
31864 KB |
Output is correct |
8 |
Correct |
174 ms |
37776 KB |
Output is correct |
9 |
Correct |
156 ms |
37700 KB |
Output is correct |
10 |
Correct |
170 ms |
38008 KB |
Output is correct |
11 |
Correct |
176 ms |
38524 KB |
Output is correct |
12 |
Correct |
161 ms |
37368 KB |
Output is correct |
13 |
Correct |
196 ms |
37924 KB |
Output is correct |
14 |
Correct |
170 ms |
37852 KB |
Output is correct |
15 |
Correct |
172 ms |
38008 KB |
Output is correct |
16 |
Correct |
144 ms |
37752 KB |
Output is correct |
17 |
Correct |
157 ms |
37880 KB |
Output is correct |
18 |
Correct |
170 ms |
37980 KB |
Output is correct |
19 |
Correct |
165 ms |
37892 KB |
Output is correct |
20 |
Correct |
172 ms |
38008 KB |
Output is correct |
21 |
Correct |
155 ms |
37752 KB |
Output is correct |
22 |
Correct |
141 ms |
37752 KB |
Output is correct |
23 |
Correct |
145 ms |
37708 KB |
Output is correct |
24 |
Correct |
157 ms |
37672 KB |
Output is correct |
25 |
Correct |
150 ms |
37772 KB |
Output is correct |
26 |
Correct |
182 ms |
37752 KB |
Output is correct |
27 |
Correct |
418 ms |
47844 KB |
Output is correct |
28 |
Correct |
358 ms |
49144 KB |
Output is correct |
29 |
Correct |
332 ms |
48828 KB |
Output is correct |
30 |
Correct |
363 ms |
49396 KB |
Output is correct |
31 |
Correct |
358 ms |
49260 KB |
Output is correct |
32 |
Correct |
265 ms |
47608 KB |
Output is correct |
33 |
Correct |
303 ms |
47532 KB |
Output is correct |
34 |
Correct |
283 ms |
47488 KB |
Output is correct |
35 |
Correct |
355 ms |
47452 KB |
Output is correct |
36 |
Correct |
253 ms |
47412 KB |
Output is correct |
37 |
Correct |
257 ms |
47352 KB |
Output is correct |
38 |
Correct |
626 ms |
74680 KB |
Output is correct |
39 |
Correct |
654 ms |
81068 KB |
Output is correct |
40 |
Correct |
535 ms |
66824 KB |
Output is correct |
41 |
Correct |
633 ms |
78720 KB |
Output is correct |
42 |
Correct |
383 ms |
47968 KB |
Output is correct |
43 |
Correct |
353 ms |
48392 KB |
Output is correct |
44 |
Correct |
528 ms |
58228 KB |
Output is correct |
45 |
Correct |
588 ms |
58284 KB |
Output is correct |
46 |
Correct |
522 ms |
58352 KB |
Output is correct |
47 |
Correct |
354 ms |
48284 KB |
Output is correct |
48 |
Correct |
335 ms |
48460 KB |
Output is correct |
49 |
Correct |
495 ms |
58884 KB |
Output is correct |
50 |
Correct |
531 ms |
58532 KB |
Output is correct |
51 |
Correct |
591 ms |
58332 KB |
Output is correct |
52 |
Correct |
409 ms |
56232 KB |
Output is correct |
53 |
Correct |
508 ms |
65268 KB |
Output is correct |
54 |
Correct |
601 ms |
73124 KB |
Output is correct |
55 |
Correct |
496 ms |
64908 KB |
Output is correct |