#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=5e5+7;
vector<int>V[LIM];
map<pair<int,int>,pair<int,int>>mp;
pair<int,int>P[LIM];
int T[LIM], kto[LIM], trlewo[4*LIM], trprawo[4*LIM], N=1, n;
int cntprawo(int l, int r) {
l+=N; r+=N;
int ans=max(trprawo[l], trprawo[r]);
while(l/2!=r/2) {
if(l%2==0) ans=max(ans, trprawo[l+1]);
if(r%2==1) ans=max(ans, trprawo[r-1]);
l/=2; r/=2;
}
return ans;
}
int cntlewo(int l, int r) {
l+=N; r+=N;
int ans=min(trlewo[l], trlewo[r]);
while(l/2!=r/2) {
if(l%2==0) ans=min(ans, trlewo[l+1]);
if(r%2==1) ans=min(ans, trlewo[r-1]);
l/=2; r/=2;
}
return ans;
}
pair<int,int>solve(pair<int,int>x) {
if(mp.find(x)!=mp.end()) return mp[x];
int po=0, ko=x.st-1;
pair<int,int>nxt=x;
if(x.st && cntprawo(x.st-1, x.st-1)<=x.nd) {
int po=0, ko=x.st-1;
while(po<ko) {
int sr=(po+ko)/2;
if(cntprawo(sr, x.st-1)<=x.nd) ko=sr; else po=sr+1;
}
nxt.st=po;
}
if(x.nd<n-1 && cntlewo(x.nd+1, x.nd+1)>=x.st) {
int po=x.nd+1, ko=n-1;
while(po<ko) {
int sr=(po+ko+1)/2;
if(cntlewo(x.nd+1, sr)>=x.st) po=sr; else ko=sr-1;
}
nxt.nd=ko;
}
if(nxt==x) return mp[x]=nxt;
return mp[x]=solve(nxt);
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n;
while(N<n) N*=2;
rep(i, n-1) {
cin >> T[i]; --T[i];
}
rep(i, n) {
int k;
cin >> k;
while(k--) {
int a;
cin >> a; --a;
V[i].pb(a);
}
}
rep(i, n) kto[i]=-1;
rep(i, n) {
for(auto j : V[i]) kto[j]=i;
if(i<n-1) trlewo[N+i+1]=kto[T[i]];
}
rep(i, n) kto[i]=n;
for(int i=n-1; i>=0; --i) {
for(auto j : V[i]) kto[j]=i;
if(i) trprawo[N+i-1]=kto[T[i-1]];
}
for(int i=N-1; i; --i) {
trlewo[i]=min(trlewo[2*i], trlewo[2*i+1]);
trprawo[i]=max(trprawo[2*i], trprawo[2*i+1]);
}
rep(i, n) P[i]=solve({i, i});
int q;
cin >> q;
while(q--) {
int a, b;
cin >> a >> b; --a; --b;
cout << (P[a].st<=b&&b<=P[a].nd?"YES":"NO") << '\n';
}
}
Compilation message
long_mansion.cpp: In function 'std::pair<int, int> solve(std::pair<int, int>)':
long_mansion.cpp:36:6: warning: unused variable 'po' [-Wunused-variable]
36 | int po=0, ko=x.st-1;
| ^~
long_mansion.cpp:36:12: warning: unused variable 'ko' [-Wunused-variable]
36 | int po=0, ko=x.st-1;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12372 KB |
Output is correct |
2 |
Correct |
13 ms |
12616 KB |
Output is correct |
3 |
Correct |
13 ms |
12880 KB |
Output is correct |
4 |
Correct |
9 ms |
12372 KB |
Output is correct |
5 |
Correct |
13 ms |
12444 KB |
Output is correct |
6 |
Correct |
9 ms |
12372 KB |
Output is correct |
7 |
Correct |
10 ms |
12628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12372 KB |
Output is correct |
2 |
Correct |
13 ms |
12616 KB |
Output is correct |
3 |
Correct |
13 ms |
12880 KB |
Output is correct |
4 |
Correct |
9 ms |
12372 KB |
Output is correct |
5 |
Correct |
13 ms |
12444 KB |
Output is correct |
6 |
Correct |
9 ms |
12372 KB |
Output is correct |
7 |
Correct |
10 ms |
12628 KB |
Output is correct |
8 |
Correct |
102 ms |
18248 KB |
Output is correct |
9 |
Correct |
95 ms |
18156 KB |
Output is correct |
10 |
Correct |
97 ms |
18676 KB |
Output is correct |
11 |
Correct |
94 ms |
19296 KB |
Output is correct |
12 |
Correct |
77 ms |
17732 KB |
Output is correct |
13 |
Correct |
88 ms |
18476 KB |
Output is correct |
14 |
Correct |
82 ms |
18392 KB |
Output is correct |
15 |
Correct |
83 ms |
18396 KB |
Output is correct |
16 |
Correct |
83 ms |
18184 KB |
Output is correct |
17 |
Correct |
113 ms |
18388 KB |
Output is correct |
18 |
Correct |
81 ms |
18496 KB |
Output is correct |
19 |
Correct |
85 ms |
18436 KB |
Output is correct |
20 |
Correct |
81 ms |
18428 KB |
Output is correct |
21 |
Correct |
93 ms |
18176 KB |
Output is correct |
22 |
Correct |
82 ms |
18252 KB |
Output is correct |
23 |
Correct |
81 ms |
18348 KB |
Output is correct |
24 |
Correct |
80 ms |
18376 KB |
Output is correct |
25 |
Correct |
81 ms |
18380 KB |
Output is correct |
26 |
Correct |
82 ms |
18496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
226 ms |
37300 KB |
Output is correct |
2 |
Correct |
260 ms |
37568 KB |
Output is correct |
3 |
Correct |
306 ms |
37488 KB |
Output is correct |
4 |
Correct |
237 ms |
37472 KB |
Output is correct |
5 |
Correct |
246 ms |
37412 KB |
Output is correct |
6 |
Correct |
365 ms |
35428 KB |
Output is correct |
7 |
Correct |
380 ms |
33728 KB |
Output is correct |
8 |
Correct |
402 ms |
33284 KB |
Output is correct |
9 |
Correct |
385 ms |
33224 KB |
Output is correct |
10 |
Correct |
383 ms |
32956 KB |
Output is correct |
11 |
Correct |
390 ms |
32916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12372 KB |
Output is correct |
2 |
Correct |
13 ms |
12616 KB |
Output is correct |
3 |
Correct |
13 ms |
12880 KB |
Output is correct |
4 |
Correct |
9 ms |
12372 KB |
Output is correct |
5 |
Correct |
13 ms |
12444 KB |
Output is correct |
6 |
Correct |
9 ms |
12372 KB |
Output is correct |
7 |
Correct |
10 ms |
12628 KB |
Output is correct |
8 |
Correct |
102 ms |
18248 KB |
Output is correct |
9 |
Correct |
95 ms |
18156 KB |
Output is correct |
10 |
Correct |
97 ms |
18676 KB |
Output is correct |
11 |
Correct |
94 ms |
19296 KB |
Output is correct |
12 |
Correct |
77 ms |
17732 KB |
Output is correct |
13 |
Correct |
88 ms |
18476 KB |
Output is correct |
14 |
Correct |
82 ms |
18392 KB |
Output is correct |
15 |
Correct |
83 ms |
18396 KB |
Output is correct |
16 |
Correct |
83 ms |
18184 KB |
Output is correct |
17 |
Correct |
113 ms |
18388 KB |
Output is correct |
18 |
Correct |
81 ms |
18496 KB |
Output is correct |
19 |
Correct |
85 ms |
18436 KB |
Output is correct |
20 |
Correct |
81 ms |
18428 KB |
Output is correct |
21 |
Correct |
93 ms |
18176 KB |
Output is correct |
22 |
Correct |
82 ms |
18252 KB |
Output is correct |
23 |
Correct |
81 ms |
18348 KB |
Output is correct |
24 |
Correct |
80 ms |
18376 KB |
Output is correct |
25 |
Correct |
81 ms |
18380 KB |
Output is correct |
26 |
Correct |
82 ms |
18496 KB |
Output is correct |
27 |
Correct |
226 ms |
37300 KB |
Output is correct |
28 |
Correct |
260 ms |
37568 KB |
Output is correct |
29 |
Correct |
306 ms |
37488 KB |
Output is correct |
30 |
Correct |
237 ms |
37472 KB |
Output is correct |
31 |
Correct |
246 ms |
37412 KB |
Output is correct |
32 |
Correct |
365 ms |
35428 KB |
Output is correct |
33 |
Correct |
380 ms |
33728 KB |
Output is correct |
34 |
Correct |
402 ms |
33284 KB |
Output is correct |
35 |
Correct |
385 ms |
33224 KB |
Output is correct |
36 |
Correct |
383 ms |
32956 KB |
Output is correct |
37 |
Correct |
390 ms |
32916 KB |
Output is correct |
38 |
Correct |
303 ms |
76224 KB |
Output is correct |
39 |
Correct |
361 ms |
87988 KB |
Output is correct |
40 |
Correct |
284 ms |
63464 KB |
Output is correct |
41 |
Correct |
1440 ms |
99264 KB |
Output is correct |
42 |
Correct |
283 ms |
36016 KB |
Output is correct |
43 |
Correct |
320 ms |
34796 KB |
Output is correct |
44 |
Correct |
651 ms |
50372 KB |
Output is correct |
45 |
Correct |
672 ms |
50220 KB |
Output is correct |
46 |
Correct |
670 ms |
49968 KB |
Output is correct |
47 |
Correct |
390 ms |
34032 KB |
Output is correct |
48 |
Correct |
378 ms |
33972 KB |
Output is correct |
49 |
Correct |
761 ms |
49456 KB |
Output is correct |
50 |
Correct |
753 ms |
49572 KB |
Output is correct |
51 |
Correct |
792 ms |
49792 KB |
Output is correct |
52 |
Correct |
466 ms |
79012 KB |
Output is correct |
53 |
Correct |
697 ms |
108504 KB |
Output is correct |
54 |
Correct |
847 ms |
142720 KB |
Output is correct |
55 |
Correct |
624 ms |
107932 KB |
Output is correct |