#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
#define arr3 array<int, 3>
struct ST{
vector<pii> t;
vector<int> a;
int n;
ST(int ns){
n = ns;
t.resize(4 * n);
a.resize(n + 1);
}
void upd(int v, int tl, int tr, int& p, int& x){
if (tl == tr){
t[v] = {x, x};
return;
}
int tm = (tl + tr) / 2, vv = 2 * v;
if (p <= tm){
upd(vv, tl, tm, p, x);
}
else {
upd(vv + 1, tm + 1, tr, p, x);
}
t[v].ff = min(t[vv].ff, t[vv + 1].ff);
t[v].ss = max(t[vv].ss, t[vv + 1].ss);
}
void upd(int p, int x){
upd(1, 1, n, p, x);
a[p] = x;
}
vector<arr3> all;
void dec(int v, int tl, int tr, int& l, int& r){
if (l > tr || r < tl) return;
if (l <= tl && tr <= r){
all.pb({v, tl, tr});
return;
}
int tm = (tl + tr) / 2, vv = 2 * v;
dec(vv, tl, tm, l, r);
dec(vv + 1, tm + 1, tr, l, r);
}
int fn1(int v, int tl, int tr, int& x){
if (tl == tr) return tl;
int tm = (tl + tr) / 2, vv = 2 * v;
if (t[vv + 1].ss > x){
return fn1(vv + 1, tm + 1, tr, x);
}
return fn1(vv, tl, tm, x);
}
int find1(int l, int r, int x){
if (l > r || a[r] > x) return -1;
all.clear();
dec(1, 1, n, l, r);
reverse(all.begin(), all.end());
for (auto [v, tl, tr]: all){
if (t[v].ss > x){
return fn1(v, tl, tr, x) + 1;
}
}
return l;
}
int fn2(int v, int tl, int tr, int& x){
if (tl == tr) return tl;
int tm = (tl + tr) / 2, vv = 2 * v;
if (t[vv].ff < x){
return fn2(vv, tl, tm, x);
}
return fn2(vv + 1, tm + 1, tr, x);
}
int find2(int l, int r, int x){
if (l > r || a[l] < x) return -1;
all.clear();
dec(1, 1, n, l, r);
for (auto [v, tl, tr]: all){
if (t[v].ff < x){
return fn2(v, tl, tr, x) - 1;
}
}
return r;
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n; cin>>n;
vector<int> c(n);
for (int i = 1; i < n; i++){
cin>>c[i];
}
vector<int> A[n + 1];
for (int i = 1; i <= n; i++){
int x; cin>>x;
while (x--){
int y; cin>>y;
A[i].pb(y);
}
}
vector<int> x(n), lst(n + 1, n + 1);
for (int i = n; i > 0; i--){
if (i < n){
x[i] = lst[c[i]];
}
for (int j: A[i]){
lst[j] = i;
}
}
fill(lst.begin(), lst.end(), 0);
vector<int> y(n);
for (int i = 1; i < n; i++){
for (int j: A[i]){
lst[j] = i;
}
y[i] = lst[c[i]];
}
ST T1(n - 1), T2(n - 1);
for (int i = 1; i < n; i++){
T1.upd(i, x[i]);
T2.upd(i, y[i]);
}
vector<pii> rr(n + 1);
for (int i = 1; i <= n; i++){
int l = i, r = i;
while (true){
bool ind1 = 0, ind2 = 0;
int j = T1.find1(1, l - 1, r);
if (j != -1){
l = j;
ind1 = 1;
}
j = T2.find2(r, n - 1, l);
if (j != -1){
r = j + 1;
ind2 = 1;
}
if (!ind1 && !ind2) break;
}
rr[i] = {l, r};
}
int Q; cin>>Q;
while (Q--){
int x, y; cin>>x>>y;
cout<<((rr[x].ff <= y && y <= rr[x].ss) ? "YES" : "NO")<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
604 KB |
Output is correct |
2 |
Correct |
2 ms |
860 KB |
Output is correct |
3 |
Correct |
3 ms |
1116 KB |
Output is correct |
4 |
Correct |
3 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
3 ms |
604 KB |
Output is correct |
7 |
Correct |
38 ms |
812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
604 KB |
Output is correct |
2 |
Correct |
2 ms |
860 KB |
Output is correct |
3 |
Correct |
3 ms |
1116 KB |
Output is correct |
4 |
Correct |
3 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
3 ms |
604 KB |
Output is correct |
7 |
Correct |
38 ms |
812 KB |
Output is correct |
8 |
Correct |
76 ms |
6480 KB |
Output is correct |
9 |
Correct |
62 ms |
6480 KB |
Output is correct |
10 |
Correct |
63 ms |
6940 KB |
Output is correct |
11 |
Correct |
67 ms |
7508 KB |
Output is correct |
12 |
Correct |
64 ms |
5972 KB |
Output is correct |
13 |
Correct |
61 ms |
6740 KB |
Output is correct |
14 |
Correct |
62 ms |
6740 KB |
Output is correct |
15 |
Correct |
60 ms |
6792 KB |
Output is correct |
16 |
Correct |
59 ms |
6484 KB |
Output is correct |
17 |
Correct |
65 ms |
6740 KB |
Output is correct |
18 |
Correct |
64 ms |
6840 KB |
Output is correct |
19 |
Correct |
65 ms |
6744 KB |
Output is correct |
20 |
Correct |
63 ms |
6740 KB |
Output is correct |
21 |
Correct |
60 ms |
6480 KB |
Output is correct |
22 |
Correct |
74 ms |
6548 KB |
Output is correct |
23 |
Correct |
88 ms |
6484 KB |
Output is correct |
24 |
Correct |
97 ms |
6708 KB |
Output is correct |
25 |
Correct |
111 ms |
6480 KB |
Output is correct |
26 |
Correct |
86 ms |
6476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
151 ms |
24964 KB |
Output is correct |
2 |
Correct |
153 ms |
24612 KB |
Output is correct |
3 |
Correct |
158 ms |
24400 KB |
Output is correct |
4 |
Correct |
148 ms |
24764 KB |
Output is correct |
5 |
Correct |
163 ms |
24748 KB |
Output is correct |
6 |
Correct |
164 ms |
23376 KB |
Output is correct |
7 |
Correct |
139 ms |
23360 KB |
Output is correct |
8 |
Correct |
139 ms |
23120 KB |
Output is correct |
9 |
Correct |
148 ms |
23124 KB |
Output is correct |
10 |
Correct |
145 ms |
23124 KB |
Output is correct |
11 |
Correct |
136 ms |
23124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
604 KB |
Output is correct |
2 |
Correct |
2 ms |
860 KB |
Output is correct |
3 |
Correct |
3 ms |
1116 KB |
Output is correct |
4 |
Correct |
3 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
3 ms |
604 KB |
Output is correct |
7 |
Correct |
38 ms |
812 KB |
Output is correct |
8 |
Correct |
76 ms |
6480 KB |
Output is correct |
9 |
Correct |
62 ms |
6480 KB |
Output is correct |
10 |
Correct |
63 ms |
6940 KB |
Output is correct |
11 |
Correct |
67 ms |
7508 KB |
Output is correct |
12 |
Correct |
64 ms |
5972 KB |
Output is correct |
13 |
Correct |
61 ms |
6740 KB |
Output is correct |
14 |
Correct |
62 ms |
6740 KB |
Output is correct |
15 |
Correct |
60 ms |
6792 KB |
Output is correct |
16 |
Correct |
59 ms |
6484 KB |
Output is correct |
17 |
Correct |
65 ms |
6740 KB |
Output is correct |
18 |
Correct |
64 ms |
6840 KB |
Output is correct |
19 |
Correct |
65 ms |
6744 KB |
Output is correct |
20 |
Correct |
63 ms |
6740 KB |
Output is correct |
21 |
Correct |
60 ms |
6480 KB |
Output is correct |
22 |
Correct |
74 ms |
6548 KB |
Output is correct |
23 |
Correct |
88 ms |
6484 KB |
Output is correct |
24 |
Correct |
97 ms |
6708 KB |
Output is correct |
25 |
Correct |
111 ms |
6480 KB |
Output is correct |
26 |
Correct |
86 ms |
6476 KB |
Output is correct |
27 |
Correct |
151 ms |
24964 KB |
Output is correct |
28 |
Correct |
153 ms |
24612 KB |
Output is correct |
29 |
Correct |
158 ms |
24400 KB |
Output is correct |
30 |
Correct |
148 ms |
24764 KB |
Output is correct |
31 |
Correct |
163 ms |
24748 KB |
Output is correct |
32 |
Correct |
164 ms |
23376 KB |
Output is correct |
33 |
Correct |
139 ms |
23360 KB |
Output is correct |
34 |
Correct |
139 ms |
23120 KB |
Output is correct |
35 |
Correct |
148 ms |
23124 KB |
Output is correct |
36 |
Correct |
145 ms |
23124 KB |
Output is correct |
37 |
Correct |
136 ms |
23124 KB |
Output is correct |
38 |
Correct |
272 ms |
72884 KB |
Output is correct |
39 |
Correct |
302 ms |
87632 KB |
Output is correct |
40 |
Correct |
226 ms |
56936 KB |
Output is correct |
41 |
Correct |
427 ms |
86120 KB |
Output is correct |
42 |
Correct |
128 ms |
24404 KB |
Output is correct |
43 |
Correct |
131 ms |
24400 KB |
Output is correct |
44 |
Correct |
205 ms |
41968 KB |
Output is correct |
45 |
Correct |
205 ms |
42064 KB |
Output is correct |
46 |
Correct |
204 ms |
42320 KB |
Output is correct |
47 |
Correct |
132 ms |
24400 KB |
Output is correct |
48 |
Correct |
168 ms |
24148 KB |
Output is correct |
49 |
Correct |
233 ms |
41808 KB |
Output is correct |
50 |
Correct |
235 ms |
41980 KB |
Output is correct |
51 |
Correct |
252 ms |
42328 KB |
Output is correct |
52 |
Execution timed out |
3051 ms |
33360 KB |
Time limit exceeded |
53 |
Halted |
0 ms |
0 KB |
- |