#include <bits/stdc++.h>
using namespace std;
#define ll int
#define pll pair<int, int>
#define pb push_back
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
const ll N = 5e5 + 100;
const ll inf = 1e9;
const ll mod = 1e9 + 7;
const ll block = 230;
ll n, q;
ll c[N], pos[N];
ll L[N][21], R[N][21], l[N], r[N];
struct segment_tree_max{
ll n;
vector<ll>st;
void init(ll _n){
n = _n;
st.clear(); st.resize(4 * n + 10, -inf);
}
void update(ll id, ll l, ll r, ll left, ll right, ll val){
if(l > right || r < left) return;
if(left <= l && r <= right){
st[id] = max(st[id], val);
return;
}
ll mid = (l + r) / 2;
update(2 * id, l, mid, left, right, val);
update(2 * id + 1, mid + 1, r, left, right, val);
st[id] = max(st[2 * id], st[2 * id + 1]);
}
ll get(ll id, ll l, ll r, ll left, ll right){
if(l > right || r < left) return -inf;
if(left <= l && r <= right) return st[id];
ll mid = (l + r) / 2;
return max(get(2 * id, l, mid, left, right), get(2 * id + 1, mid + 1, r, left, right));
}
void update(ll l, ll r, ll val){update(1,1,n,l,r,val);}
ll get(ll l, ll r){return get(1,1,n,l,r);}
} stmax;
struct segment_tree_min{
ll n;
vector<ll>st;
void init(ll _n){
n = _n;
st.clear(); st.resize(4 * n + 10, inf);
}
void update(ll id, ll l, ll r, ll left, ll right, ll val){
if(l > right || r < left) return;
if(left <= l && r <= right){
st[id] = min(st[id], val);
return;
}
ll mid = (l + r) / 2;
update(2 * id, l, mid, left, right, val);
update(2 * id + 1, mid + 1, r, left, right, val);
st[id] = min(st[2 * id], st[2 * id + 1]);
}
ll get(ll id, ll l, ll r, ll left, ll right){
if(l > right || r < left) return inf;
if(left <= l && r <= right) return st[id];
ll mid = (l + r) / 2;
return min(get(2 * id, l, mid, left, right), get(2 * id + 1, mid + 1, r, left, right));
}
void update(ll l, ll r, ll val){update(1,1,n,l,r,val);}
ll get(ll l, ll r){return get(1,1,n,l,r);}
} stmin;
vector<ll>g[N];
ll get_L(ll l, ll r){
ll p = 31 - __builtin_clz(r - l + 1);
return min(L[l][p], L[r - (1 << p) + 1][p]);
}
ll get_R(ll l, ll r){
ll p = 31 - __builtin_clz(r - l + 1);
return max(R[l][p], R[r - (1 << p) + 1][p]);
}
void build(){
for(int i = 1; i <= n;i++){
if(i >= 2) L[i][0] = (pos[c[i - 1]] == 0 ? -inf : pos[c[i-1]]);
else L[i][0] = -inf;
for(auto x : g[i]) pos[x] = i;
}
for(int i = 1; i <= n - 1;i++) pos[c[i]] = 0;
for(int i = n; i >= 1;i--){
if(i <= n - 1) R[i][0] = (pos[c[i]] == 0 ? inf : pos[c[i]]);
else R[i][0] = inf;
for(auto x : g[i]) pos[x] = i;
}
for(int j = 1; j <= 20;j++){
for(int i = 1; i + (1 << j) <= n + 1;i++){
L[i][j] = min(L[i][j-1], L[i + (1 << (j - 1))][j-1]);
R[i][j] = max(R[i][j-1], R[i + (1 << (j - 1))][j-1]);
}
}
stmin.init(n); stmax.init(n);
for(int i = 1; i <= n;i++){
ll x = i, y = i;
while(1){
pll lst = {x, y};
if(x > 1){
ll l = 1, r = x - 1, pos = -1;
while(l <= r){
ll mid = (l + r) / 2;
if(get_R(mid, x - 1) <= y) pos = mid, r = mid - 1;
else l = mid + 1;
}
if(pos != -1){
ll mn = stmin.get(pos, x - 1), mx = stmax.get(pos, x - 1);
x = min({x, mn, pos}); y = max(y, mx);
}
}
if(y < n){
ll l = y + 1, r = n, pos = -1;
while(l <= r){
ll mid = (l + r) / 2;
if(get_L(y + 1, mid) >= x) pos = mid, l = mid + 1;
else r = mid - 1;
}
if(pos != -1){
ll mn = stmin.get(y + 1, pos), mx = stmax.get(y + 1, pos);
x = min(x, mn); y = max({y, mx, pos});
}
}
if(lst.F == x && lst.S == y) break;
}
l[i] = x, r[i] = y;
stmin.update(i, i, l[i]); stmax.update(i, i, r[i]);
}
}
void to_thic_cau(){
cin >> n;
for(int i = 1; i <= n - 1;i++) cin >> c[i];
for(int i = 1; i <= n;i++){
ll sz; cin >> sz;
while(sz--){
ll x; cin >> x;
g[i].pb(x);
}
}
build();
cin >> q;
while(q--){
ll x,y; cin >> x >> y;
if(l[x] <= y && y <= r[x]) cout << "YES" << '\n';
else cout << "NO" << "\n";
}
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
ll tc = 1;
//cin >> tc;
while(tc--) to_thic_cau();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12636 KB |
Output is correct |
2 |
Correct |
9 ms |
12892 KB |
Output is correct |
3 |
Correct |
15 ms |
13444 KB |
Output is correct |
4 |
Correct |
9 ms |
12636 KB |
Output is correct |
5 |
Correct |
8 ms |
12636 KB |
Output is correct |
6 |
Correct |
12 ms |
12636 KB |
Output is correct |
7 |
Correct |
7 ms |
12804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12636 KB |
Output is correct |
2 |
Correct |
9 ms |
12892 KB |
Output is correct |
3 |
Correct |
15 ms |
13444 KB |
Output is correct |
4 |
Correct |
9 ms |
12636 KB |
Output is correct |
5 |
Correct |
8 ms |
12636 KB |
Output is correct |
6 |
Correct |
12 ms |
12636 KB |
Output is correct |
7 |
Correct |
7 ms |
12804 KB |
Output is correct |
8 |
Correct |
82 ms |
18516 KB |
Output is correct |
9 |
Correct |
68 ms |
18516 KB |
Output is correct |
10 |
Correct |
70 ms |
19024 KB |
Output is correct |
11 |
Correct |
71 ms |
19796 KB |
Output is correct |
12 |
Correct |
66 ms |
17952 KB |
Output is correct |
13 |
Correct |
66 ms |
18596 KB |
Output is correct |
14 |
Correct |
65 ms |
18812 KB |
Output is correct |
15 |
Correct |
65 ms |
18768 KB |
Output is correct |
16 |
Correct |
62 ms |
18516 KB |
Output is correct |
17 |
Correct |
71 ms |
18616 KB |
Output is correct |
18 |
Correct |
73 ms |
18720 KB |
Output is correct |
19 |
Correct |
88 ms |
18768 KB |
Output is correct |
20 |
Correct |
73 ms |
18772 KB |
Output is correct |
21 |
Correct |
83 ms |
18512 KB |
Output is correct |
22 |
Correct |
64 ms |
18684 KB |
Output is correct |
23 |
Correct |
68 ms |
18516 KB |
Output is correct |
24 |
Correct |
67 ms |
18500 KB |
Output is correct |
25 |
Correct |
96 ms |
18364 KB |
Output is correct |
26 |
Correct |
99 ms |
18512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
181 ms |
45648 KB |
Output is correct |
2 |
Correct |
184 ms |
45428 KB |
Output is correct |
3 |
Correct |
198 ms |
45136 KB |
Output is correct |
4 |
Correct |
196 ms |
45652 KB |
Output is correct |
5 |
Correct |
204 ms |
45396 KB |
Output is correct |
6 |
Correct |
184 ms |
44368 KB |
Output is correct |
7 |
Correct |
193 ms |
44116 KB |
Output is correct |
8 |
Correct |
163 ms |
44116 KB |
Output is correct |
9 |
Correct |
162 ms |
44112 KB |
Output is correct |
10 |
Correct |
163 ms |
44064 KB |
Output is correct |
11 |
Correct |
162 ms |
44028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12636 KB |
Output is correct |
2 |
Correct |
9 ms |
12892 KB |
Output is correct |
3 |
Correct |
15 ms |
13444 KB |
Output is correct |
4 |
Correct |
9 ms |
12636 KB |
Output is correct |
5 |
Correct |
8 ms |
12636 KB |
Output is correct |
6 |
Correct |
12 ms |
12636 KB |
Output is correct |
7 |
Correct |
7 ms |
12804 KB |
Output is correct |
8 |
Correct |
82 ms |
18516 KB |
Output is correct |
9 |
Correct |
68 ms |
18516 KB |
Output is correct |
10 |
Correct |
70 ms |
19024 KB |
Output is correct |
11 |
Correct |
71 ms |
19796 KB |
Output is correct |
12 |
Correct |
66 ms |
17952 KB |
Output is correct |
13 |
Correct |
66 ms |
18596 KB |
Output is correct |
14 |
Correct |
65 ms |
18812 KB |
Output is correct |
15 |
Correct |
65 ms |
18768 KB |
Output is correct |
16 |
Correct |
62 ms |
18516 KB |
Output is correct |
17 |
Correct |
71 ms |
18616 KB |
Output is correct |
18 |
Correct |
73 ms |
18720 KB |
Output is correct |
19 |
Correct |
88 ms |
18768 KB |
Output is correct |
20 |
Correct |
73 ms |
18772 KB |
Output is correct |
21 |
Correct |
83 ms |
18512 KB |
Output is correct |
22 |
Correct |
64 ms |
18684 KB |
Output is correct |
23 |
Correct |
68 ms |
18516 KB |
Output is correct |
24 |
Correct |
67 ms |
18500 KB |
Output is correct |
25 |
Correct |
96 ms |
18364 KB |
Output is correct |
26 |
Correct |
99 ms |
18512 KB |
Output is correct |
27 |
Correct |
181 ms |
45648 KB |
Output is correct |
28 |
Correct |
184 ms |
45428 KB |
Output is correct |
29 |
Correct |
198 ms |
45136 KB |
Output is correct |
30 |
Correct |
196 ms |
45652 KB |
Output is correct |
31 |
Correct |
204 ms |
45396 KB |
Output is correct |
32 |
Correct |
184 ms |
44368 KB |
Output is correct |
33 |
Correct |
193 ms |
44116 KB |
Output is correct |
34 |
Correct |
163 ms |
44116 KB |
Output is correct |
35 |
Correct |
162 ms |
44112 KB |
Output is correct |
36 |
Correct |
163 ms |
44064 KB |
Output is correct |
37 |
Correct |
162 ms |
44028 KB |
Output is correct |
38 |
Correct |
449 ms |
120748 KB |
Output is correct |
39 |
Correct |
548 ms |
144364 KB |
Output is correct |
40 |
Correct |
384 ms |
95824 KB |
Output is correct |
41 |
Correct |
725 ms |
142832 KB |
Output is correct |
42 |
Correct |
202 ms |
45392 KB |
Output is correct |
43 |
Correct |
201 ms |
45392 KB |
Output is correct |
44 |
Correct |
345 ms |
72108 KB |
Output is correct |
45 |
Correct |
324 ms |
72152 KB |
Output is correct |
46 |
Correct |
327 ms |
72524 KB |
Output is correct |
47 |
Correct |
190 ms |
45392 KB |
Output is correct |
48 |
Correct |
163 ms |
45140 KB |
Output is correct |
49 |
Correct |
326 ms |
72020 KB |
Output is correct |
50 |
Correct |
313 ms |
72020 KB |
Output is correct |
51 |
Correct |
337 ms |
72752 KB |
Output is correct |
52 |
Correct |
278 ms |
71248 KB |
Output is correct |
53 |
Correct |
443 ms |
97136 KB |
Output is correct |
54 |
Correct |
535 ms |
123040 KB |
Output is correct |
55 |
Correct |
378 ms |
97424 KB |
Output is correct |