#include "bits/stdc++.h"
using namespace std;
#define ar array
typedef long long ll;
//~ #define int ll
const int N = 5e5 + 5;
int c[N], b[N], last[N];
vector<int> a[N];
int l[N], r[N];
int L[N], R[N];
int mn[N][20], mx[N][20];
struct ST{
int L[N << 2], R[N << 2];
ST(){
memset(R, 63, sizeof R);
}
void umin(int l, int r, int v, int lx = 0, int rx = N, int x = 1){
if(lx > r || rx < l) return;
if(lx >= l && rx <= r){
R[x] = min(R[x], v);
return;
}
int m = (lx + rx) >> 1;
umin(l, r, v, lx, m, x << 1);
umin(l, r, v, m + 1, rx, x << 1 | 1);
}
void umax(int l, int r, int v, int lx = 0, int rx = N, int x = 1){
if(lx > r || rx < l) return;
if(lx >= l && rx <= r){
L[x] = max(L[x], v);
return;
}
int m = (lx + rx) >> 1;
umax(l, r, v, lx, m, x << 1);
umax(l, r, v, m + 1, rx, x << 1 | 1);
}
ar<int, 2> get(int i, int lx = 0, int rx = N, int x = 1){
if(lx == rx) return {L[x], R[x]};
int m = (lx + rx) >> 1;
ar<int, 2> res {};
if(i <= m){
res = get(i, lx, m, x << 1);
} else {
res = get(i, m + 1, rx, x << 1 | 1);
}
res[0] = max(res[0], L[x]);
res[1] = min(res[1], R[x]);
return res;
}
}tree;
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n; cin >> n;
for(int i=1;i<n;i++){
cin >> c[i];
}
for(int i=1;i<=n;i++){
cin >> b[i];
L[i] = 1, R[i] = n;
a[i].resize(b[i]);
for(int j=0;j<b[i];j++){
cin >> a[i][j];
}
}
for(int i=0;i<N;i++) last[i] = 0;
for(int i=1;i<=n;i++){
for(auto x : a[i]){
last[x] = i;
}
l[i] = last[c[i]];
}
for(int i=0;i<N;i++) last[i] = n + 1;
for(int i=n;i>0;i--){
r[i] = last[c[i]];
for(auto x : a[i]){
last[x] = i;
}
}
r[0] = N, l[n] = 0;
for(int i=0;i<=n;i++){
mx[i][0] = r[i]; // r[i]
mn[i][0] = l[i]; // l[i]
}
for(int j=1;j<20;j++){
for(int i=0;i + (1 << (j - 1)) <= n;i++){
mx[i][j] = max(mx[i][j-1], mx[i + (1 << (j - 1))][j-1]);
mn[i][j] = min(mn[i][j-1], mn[i + (1 << (j - 1))][j-1]);
}
}
auto get_mn = [&](int l_, int r_){
int lg = __lg(r_ - l_ + 1);
return min(mn[l_][lg], mn[r_ - (1 << lg) + 1][lg]);
};
auto get_mx = [&](int l_, int r_){
int lg = __lg(r_ - l_ + 1);
return max(mx[l_][lg], mx[r_ - (1 << lg) + 1][lg]);
};
for(int i=1;i<n;i++){
{
//~ int j = get_mn(i, r[i] - 1);
int l_ = i + 1, r_ = r[i] - 1;
while(l_ < r_){
int m = (l_ + r_ + 1) >> 1;
if(get_mn(m, r[i] - 1) <= i){
l_ = m;
} else {
r_ = m - 1;
}
}
int j = l_;
if(l_ == r_ && l[j] <= i){
tree.umax(i + 1, j, i + 1);
}
}
{
//~ int j = get_mx(l[i], i);
int l_ = l[i], r_ = i - 1;
while(l_ < r_){
int m = (l_ + r_ ) >> 1;
if(get_mx(l[i], m) > i){
r_ = m;
} else {
l_ = m + 1;
}
}
int j = l_;
if(l_ == r_ && i < r[j]){
tree.umin(j + 1, i, i);
}
}
}
for(int i=1;i<=n;i++){
auto res = tree.get(i);
L[i] = max(L[i], res[0]);
R[i] = min(R[i], res[1]);
}
int q; cin >> q;
while(q--){
int a, b; cin >> a >> b;
if(L[a] <= b && b <= R[a]){
cout<<"YES\n";
} else {
cout<<"NO\n";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
22356 KB |
Output is correct |
2 |
Correct |
13 ms |
22612 KB |
Output is correct |
3 |
Correct |
12 ms |
22956 KB |
Output is correct |
4 |
Correct |
12 ms |
22404 KB |
Output is correct |
5 |
Correct |
14 ms |
22392 KB |
Output is correct |
6 |
Correct |
12 ms |
22356 KB |
Output is correct |
7 |
Correct |
13 ms |
22356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
22356 KB |
Output is correct |
2 |
Correct |
13 ms |
22612 KB |
Output is correct |
3 |
Correct |
12 ms |
22956 KB |
Output is correct |
4 |
Correct |
12 ms |
22404 KB |
Output is correct |
5 |
Correct |
14 ms |
22392 KB |
Output is correct |
6 |
Correct |
12 ms |
22356 KB |
Output is correct |
7 |
Correct |
13 ms |
22356 KB |
Output is correct |
8 |
Correct |
94 ms |
23860 KB |
Output is correct |
9 |
Correct |
90 ms |
23856 KB |
Output is correct |
10 |
Correct |
100 ms |
24140 KB |
Output is correct |
11 |
Correct |
91 ms |
24780 KB |
Output is correct |
12 |
Correct |
84 ms |
23832 KB |
Output is correct |
13 |
Correct |
85 ms |
24080 KB |
Output is correct |
14 |
Correct |
88 ms |
24024 KB |
Output is correct |
15 |
Correct |
86 ms |
24192 KB |
Output is correct |
16 |
Correct |
87 ms |
24256 KB |
Output is correct |
17 |
Correct |
89 ms |
24064 KB |
Output is correct |
18 |
Correct |
86 ms |
24084 KB |
Output is correct |
19 |
Correct |
86 ms |
24044 KB |
Output is correct |
20 |
Correct |
85 ms |
24284 KB |
Output is correct |
21 |
Correct |
86 ms |
24296 KB |
Output is correct |
22 |
Correct |
108 ms |
24136 KB |
Output is correct |
23 |
Correct |
90 ms |
23964 KB |
Output is correct |
24 |
Correct |
89 ms |
23924 KB |
Output is correct |
25 |
Correct |
90 ms |
23976 KB |
Output is correct |
26 |
Correct |
95 ms |
23924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
196 ms |
45584 KB |
Output is correct |
2 |
Correct |
183 ms |
45516 KB |
Output is correct |
3 |
Correct |
182 ms |
45604 KB |
Output is correct |
4 |
Correct |
193 ms |
45732 KB |
Output is correct |
5 |
Correct |
192 ms |
45580 KB |
Output is correct |
6 |
Correct |
158 ms |
45668 KB |
Output is correct |
7 |
Correct |
152 ms |
45732 KB |
Output is correct |
8 |
Correct |
164 ms |
45808 KB |
Output is correct |
9 |
Correct |
149 ms |
45816 KB |
Output is correct |
10 |
Correct |
160 ms |
45480 KB |
Output is correct |
11 |
Correct |
152 ms |
45268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
22356 KB |
Output is correct |
2 |
Correct |
13 ms |
22612 KB |
Output is correct |
3 |
Correct |
12 ms |
22956 KB |
Output is correct |
4 |
Correct |
12 ms |
22404 KB |
Output is correct |
5 |
Correct |
14 ms |
22392 KB |
Output is correct |
6 |
Correct |
12 ms |
22356 KB |
Output is correct |
7 |
Correct |
13 ms |
22356 KB |
Output is correct |
8 |
Correct |
94 ms |
23860 KB |
Output is correct |
9 |
Correct |
90 ms |
23856 KB |
Output is correct |
10 |
Correct |
100 ms |
24140 KB |
Output is correct |
11 |
Correct |
91 ms |
24780 KB |
Output is correct |
12 |
Correct |
84 ms |
23832 KB |
Output is correct |
13 |
Correct |
85 ms |
24080 KB |
Output is correct |
14 |
Correct |
88 ms |
24024 KB |
Output is correct |
15 |
Correct |
86 ms |
24192 KB |
Output is correct |
16 |
Correct |
87 ms |
24256 KB |
Output is correct |
17 |
Correct |
89 ms |
24064 KB |
Output is correct |
18 |
Correct |
86 ms |
24084 KB |
Output is correct |
19 |
Correct |
86 ms |
24044 KB |
Output is correct |
20 |
Correct |
85 ms |
24284 KB |
Output is correct |
21 |
Correct |
86 ms |
24296 KB |
Output is correct |
22 |
Correct |
108 ms |
24136 KB |
Output is correct |
23 |
Correct |
90 ms |
23964 KB |
Output is correct |
24 |
Correct |
89 ms |
23924 KB |
Output is correct |
25 |
Correct |
90 ms |
23976 KB |
Output is correct |
26 |
Correct |
95 ms |
23924 KB |
Output is correct |
27 |
Correct |
196 ms |
45584 KB |
Output is correct |
28 |
Correct |
183 ms |
45516 KB |
Output is correct |
29 |
Correct |
182 ms |
45604 KB |
Output is correct |
30 |
Correct |
193 ms |
45732 KB |
Output is correct |
31 |
Correct |
192 ms |
45580 KB |
Output is correct |
32 |
Correct |
158 ms |
45668 KB |
Output is correct |
33 |
Correct |
152 ms |
45732 KB |
Output is correct |
34 |
Correct |
164 ms |
45808 KB |
Output is correct |
35 |
Correct |
149 ms |
45816 KB |
Output is correct |
36 |
Correct |
160 ms |
45480 KB |
Output is correct |
37 |
Correct |
152 ms |
45268 KB |
Output is correct |
38 |
Correct |
689 ms |
117560 KB |
Output is correct |
39 |
Correct |
683 ms |
144512 KB |
Output is correct |
40 |
Correct |
478 ms |
99764 KB |
Output is correct |
41 |
Correct |
485 ms |
142660 KB |
Output is correct |
42 |
Correct |
179 ms |
53008 KB |
Output is correct |
43 |
Correct |
189 ms |
53096 KB |
Output is correct |
44 |
Correct |
247 ms |
77644 KB |
Output is correct |
45 |
Correct |
246 ms |
77856 KB |
Output is correct |
46 |
Correct |
244 ms |
77900 KB |
Output is correct |
47 |
Correct |
163 ms |
52640 KB |
Output is correct |
48 |
Correct |
156 ms |
52748 KB |
Output is correct |
49 |
Correct |
251 ms |
76460 KB |
Output is correct |
50 |
Correct |
229 ms |
76576 KB |
Output is correct |
51 |
Correct |
294 ms |
76608 KB |
Output is correct |
52 |
Correct |
342 ms |
76468 KB |
Output is correct |
53 |
Correct |
428 ms |
100208 KB |
Output is correct |
54 |
Correct |
576 ms |
123700 KB |
Output is correct |
55 |
Correct |
458 ms |
100172 KB |
Output is correct |