// vaziat meshki-ghermeze !
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
const ll Mod = 1000000007LL;
const int N = 5e5 + 10;
const ll Inf = 2242545357980376863LL;
const int Log = 20;
int n;
int L[Log][N], R[Log][N];
int C[N];
vector<int> B[N];
int vis[N];
typedef pair<int, int> pii;
map<pii, pii> mp;
pii Solve(pii A){
// cerr << "^ " << A.F << ' ' << A.S << '\n';
if(mp.count(A))
return mp[A];
int l = A.F, r = A.S;
for(int lg = Log - 1; lg >= 0; lg --){
if(l <= L[lg][r + 1])
r += (1 << lg);
}
for(int lg = Log - 1; lg >= 0; lg --){
if(l <= (1 << lg)) continue;
if(R[lg][l - (1 << lg)] <= r)
l -= (1 << lg);
}
if(r == n)
return mp[A] = {l, r};
int mt = L[0][r + 1];
if(mt < l)
return mp[A] = {l, r};
l = mt;
r ++;
// cerr << "# " << l << ", " << r << '\n';
return mp[A] = Solve({l, r});
}
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 1; i < n; i++) cin >> C[i];
for(int i = 1; i <= n; i++){
int sz;
cin >> sz;
int v;
for(int j = 0; j < sz; j++){
cin >> v;
B[i].pb(v);
}
}
// cerr << "! ";
for(int i = 1; i <= n; i++){
L[0][i] = vis[C[i - 1]];
for(auto x : B[i])
vis[x] = i;
// cerr << L[0][i] << ' ';
}
L[0][n + 1] = 0;
// cerr << '\n';
fill(vis, vis + N, n + 1);
// cerr << "# ";
for(int i = n; i >= 1; i--){
R[0][i] = vis[C[i]];
for(auto x : B[i])
vis[x] = i;
// cerr << R[0][i] << ' ';
}
R[0][0] = n + 1;
// cerr << '\n';
for(int l = 1; l < Log; l++){
for(int i = 1; i <= n + 1; i++){
L[l][i] = L[l - 1][i];
if(i + (1 << (l - 1)) <= n + 1)
L[l][i] = min(L[l][i], L[l - 1][i + (1 << (l - 1))]);
}
}
for(int l = 1; l < Log; l++){
for(int i = 0; i <= n; i++){
R[l][i] = R[l - 1][i];
if(i + (1 << (l - 1)) <= n)
R[l][i] = max(R[l][i], R[l - 1][i + (1 << (l - 1))]);
}
}
// auto [l,r] = Solve({3, 3});
// cerr << "!!! [" << l << ", " << r << "]\n";
// exit(0);
int q;
cin >> q;
int x, y;
for(int i = 0; i < q; i++){
cin >> x >> y;
auto [l, r] = Solve({x, x});
// debug(l);
// debug(r);
cout << (l <= y && y <= r ? "YES" : "NO") << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
14792 KB |
Output is correct |
2 |
Correct |
13 ms |
15052 KB |
Output is correct |
3 |
Correct |
15 ms |
15496 KB |
Output is correct |
4 |
Correct |
14 ms |
14684 KB |
Output is correct |
5 |
Correct |
13 ms |
14768 KB |
Output is correct |
6 |
Correct |
12 ms |
14796 KB |
Output is correct |
7 |
Correct |
13 ms |
14912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
14792 KB |
Output is correct |
2 |
Correct |
13 ms |
15052 KB |
Output is correct |
3 |
Correct |
15 ms |
15496 KB |
Output is correct |
4 |
Correct |
14 ms |
14684 KB |
Output is correct |
5 |
Correct |
13 ms |
14768 KB |
Output is correct |
6 |
Correct |
12 ms |
14796 KB |
Output is correct |
7 |
Correct |
13 ms |
14912 KB |
Output is correct |
8 |
Correct |
227 ms |
16356 KB |
Output is correct |
9 |
Correct |
224 ms |
16480 KB |
Output is correct |
10 |
Correct |
236 ms |
16736 KB |
Output is correct |
11 |
Correct |
252 ms |
17408 KB |
Output is correct |
12 |
Correct |
202 ms |
16448 KB |
Output is correct |
13 |
Correct |
192 ms |
16544 KB |
Output is correct |
14 |
Correct |
193 ms |
16560 KB |
Output is correct |
15 |
Correct |
192 ms |
16592 KB |
Output is correct |
16 |
Correct |
209 ms |
16820 KB |
Output is correct |
17 |
Correct |
210 ms |
16548 KB |
Output is correct |
18 |
Correct |
198 ms |
16556 KB |
Output is correct |
19 |
Correct |
191 ms |
16660 KB |
Output is correct |
20 |
Correct |
237 ms |
16640 KB |
Output is correct |
21 |
Correct |
211 ms |
16820 KB |
Output is correct |
22 |
Correct |
195 ms |
16604 KB |
Output is correct |
23 |
Correct |
253 ms |
16468 KB |
Output is correct |
24 |
Correct |
224 ms |
16580 KB |
Output is correct |
25 |
Correct |
260 ms |
16496 KB |
Output is correct |
26 |
Correct |
229 ms |
16456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
751 ms |
42632 KB |
Output is correct |
2 |
Correct |
709 ms |
43024 KB |
Output is correct |
3 |
Correct |
712 ms |
43304 KB |
Output is correct |
4 |
Correct |
746 ms |
42820 KB |
Output is correct |
5 |
Correct |
745 ms |
42952 KB |
Output is correct |
6 |
Correct |
527 ms |
41096 KB |
Output is correct |
7 |
Correct |
479 ms |
41352 KB |
Output is correct |
8 |
Correct |
497 ms |
41276 KB |
Output is correct |
9 |
Correct |
488 ms |
41352 KB |
Output is correct |
10 |
Correct |
448 ms |
41260 KB |
Output is correct |
11 |
Correct |
464 ms |
41328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
14792 KB |
Output is correct |
2 |
Correct |
13 ms |
15052 KB |
Output is correct |
3 |
Correct |
15 ms |
15496 KB |
Output is correct |
4 |
Correct |
14 ms |
14684 KB |
Output is correct |
5 |
Correct |
13 ms |
14768 KB |
Output is correct |
6 |
Correct |
12 ms |
14796 KB |
Output is correct |
7 |
Correct |
13 ms |
14912 KB |
Output is correct |
8 |
Correct |
227 ms |
16356 KB |
Output is correct |
9 |
Correct |
224 ms |
16480 KB |
Output is correct |
10 |
Correct |
236 ms |
16736 KB |
Output is correct |
11 |
Correct |
252 ms |
17408 KB |
Output is correct |
12 |
Correct |
202 ms |
16448 KB |
Output is correct |
13 |
Correct |
192 ms |
16544 KB |
Output is correct |
14 |
Correct |
193 ms |
16560 KB |
Output is correct |
15 |
Correct |
192 ms |
16592 KB |
Output is correct |
16 |
Correct |
209 ms |
16820 KB |
Output is correct |
17 |
Correct |
210 ms |
16548 KB |
Output is correct |
18 |
Correct |
198 ms |
16556 KB |
Output is correct |
19 |
Correct |
191 ms |
16660 KB |
Output is correct |
20 |
Correct |
237 ms |
16640 KB |
Output is correct |
21 |
Correct |
211 ms |
16820 KB |
Output is correct |
22 |
Correct |
195 ms |
16604 KB |
Output is correct |
23 |
Correct |
253 ms |
16468 KB |
Output is correct |
24 |
Correct |
224 ms |
16580 KB |
Output is correct |
25 |
Correct |
260 ms |
16496 KB |
Output is correct |
26 |
Correct |
229 ms |
16456 KB |
Output is correct |
27 |
Correct |
751 ms |
42632 KB |
Output is correct |
28 |
Correct |
709 ms |
43024 KB |
Output is correct |
29 |
Correct |
712 ms |
43304 KB |
Output is correct |
30 |
Correct |
746 ms |
42820 KB |
Output is correct |
31 |
Correct |
745 ms |
42952 KB |
Output is correct |
32 |
Correct |
527 ms |
41096 KB |
Output is correct |
33 |
Correct |
479 ms |
41352 KB |
Output is correct |
34 |
Correct |
497 ms |
41276 KB |
Output is correct |
35 |
Correct |
488 ms |
41352 KB |
Output is correct |
36 |
Correct |
448 ms |
41260 KB |
Output is correct |
37 |
Correct |
464 ms |
41328 KB |
Output is correct |
38 |
Correct |
1621 ms |
118220 KB |
Output is correct |
39 |
Correct |
1695 ms |
142588 KB |
Output is correct |
40 |
Correct |
1548 ms |
99084 KB |
Output is correct |
41 |
Correct |
1760 ms |
144640 KB |
Output is correct |
42 |
Correct |
568 ms |
47276 KB |
Output is correct |
43 |
Correct |
521 ms |
47132 KB |
Output is correct |
44 |
Correct |
685 ms |
70340 KB |
Output is correct |
45 |
Correct |
671 ms |
70460 KB |
Output is correct |
46 |
Correct |
668 ms |
70492 KB |
Output is correct |
47 |
Correct |
500 ms |
47604 KB |
Output is correct |
48 |
Correct |
502 ms |
47496 KB |
Output is correct |
49 |
Correct |
561 ms |
70392 KB |
Output is correct |
50 |
Correct |
561 ms |
70604 KB |
Output is correct |
51 |
Correct |
567 ms |
70676 KB |
Output is correct |
52 |
Correct |
1134 ms |
84800 KB |
Output is correct |
53 |
Correct |
1433 ms |
113032 KB |
Output is correct |
54 |
Correct |
1623 ms |
143424 KB |
Output is correct |
55 |
Correct |
1394 ms |
113440 KB |
Output is correct |