// In the name of Allah
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef complex<ld> cld;
#define all(x) (x).begin(),(x).end()
#define len(x) ((ll) (x).size())
#define F first
#define S second
#define X real()
#define Y imag()
#define pb push_back
#define sep ' '
#define endl '\n'
#define Mp make_pair
#define kill(x) cout << x << '\n', exit(0)
#define set_dec(x) cout << fixed << setprecision(x);
#define file_io(x,y) freopen(x, "r", stdin); freopen(y, "w", stdout);
int n, q;
const int maxn = 5e5 + 7;
const int maxlg = 20;
int rmnq[maxn][maxlg], rmxq[maxn][maxlg];
int A[maxn]; vector<int> ls[maxn];
int A1[maxn], A2[maxn], ind[maxn];
vector<int> lsx[maxn]; set<int> st;
int val1[maxn], val2[maxn];
int get_min(int l, int r) {
int j = __lg(r - l);
return min(rmnq[l][j], rmnq[r - (1 << j)][j]);
}
int get_max(int l, int r) {
int j = __lg(r - l);
return max(rmxq[l][j], rmxq[r - (1 << j)][j]);
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i <= n - 1; i++) {
cin >> A[i]; A[i]--;
}
for (int i = 1; i <= n; i++) {
int T;
cin >> T;
while (T--) {
int x;
cin >> x; x--;
ls[i].pb(x);
}
}
fill(ind, ind + n, 0);
for (int i = 1; i <= n; i++) {
for (int j : ls[i]) ind[j] = i;
if (i <= n - 1) A1[i] = ind[A[i]];
}
fill(ind, ind + n, n + 1);
for (int i = n; i >= 1; i--) {
for (int j : ls[i]) ind[j] = i;
if (i - 1 >= 1) A2[i - 1] = ind[A[i - 1]];
}
st.clear();
for (int i = 0; i <= n + 1; i++) lsx[i].clear();
for (int i = 1; i <= n - 1; i++) lsx[A2[i]].pb(i);
for (int i = n + 1; i >= 0; i--) {
for (int j : lsx[i]) st.insert(j);
if (i - 1 >= 1 && i - 1 <= n - 1) {
if (A1[i - 1] == 0) val1[i - 1] = 0;
else {
auto it = st.lower_bound(A1[i - 1]);
if (it == st.end()) val1[i - 1] = n + 1;
else val1[i - 1] = (*it) + 1;
}
}
}
st.clear();
for (int i = 0; i <= n + 1; i++) lsx[i].clear();
for (int i = 1; i <= n - 1; i++) lsx[A1[i]].pb(i);
for (int i = 0; i <= n + 1; i++) {
for (int j : lsx[i]) st.insert(j);
if (i >= 1 && i <= n - 1) {
if (A2[i] == n + 1) val2[i + 1] = n + 1;
else {
auto it = st.lower_bound(A2[i]);
if (it == st.begin()) val2[i + 1] = 0;
else {
it--; val2[i + 1] = *it;
}
}
}
}
for (int i = n; i >= 1; i--) {
rmnq[i][0] = val1[i]; rmxq[i][0] = val2[i];
for (int j = 1; j < maxlg; j++) {
rmnq[i][j] = rmnq[i][j - 1]; rmxq[i][j] = rmxq[i][j - 1];
if (i + (1 << (j - 1)) <= n) {
rmnq[i][j] = min(rmnq[i][j], rmnq[i + (1 << (j - 1))][j - 1]);
rmxq[i][j] = max(rmxq[i][j], rmxq[i + (1 << (j - 1))][j - 1]);
}
}
}
cin >> q;
while (q--) {
int u, v;
cin >> u >> v;
if (u < v) {
if (get_min(u, v) <= u) cout << "NO" << endl;
else cout << "YES" << endl;
}
else {
if (get_max(v + 1, u + 1) >= u) cout << "NO" << endl;
else cout << "YES" << endl;
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
39680 KB |
Output is correct |
2 |
Correct |
11 ms |
39772 KB |
Output is correct |
3 |
Correct |
13 ms |
40028 KB |
Output is correct |
4 |
Correct |
9 ms |
39516 KB |
Output is correct |
5 |
Correct |
10 ms |
39640 KB |
Output is correct |
6 |
Correct |
10 ms |
39516 KB |
Output is correct |
7 |
Correct |
11 ms |
39516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
39680 KB |
Output is correct |
2 |
Correct |
11 ms |
39772 KB |
Output is correct |
3 |
Correct |
13 ms |
40028 KB |
Output is correct |
4 |
Correct |
9 ms |
39516 KB |
Output is correct |
5 |
Correct |
10 ms |
39640 KB |
Output is correct |
6 |
Correct |
10 ms |
39516 KB |
Output is correct |
7 |
Correct |
11 ms |
39516 KB |
Output is correct |
8 |
Correct |
86 ms |
41196 KB |
Output is correct |
9 |
Correct |
88 ms |
41044 KB |
Output is correct |
10 |
Correct |
113 ms |
41396 KB |
Output is correct |
11 |
Correct |
100 ms |
41620 KB |
Output is correct |
12 |
Correct |
106 ms |
41164 KB |
Output is correct |
13 |
Correct |
101 ms |
41264 KB |
Output is correct |
14 |
Correct |
95 ms |
41216 KB |
Output is correct |
15 |
Correct |
110 ms |
41720 KB |
Output is correct |
16 |
Correct |
88 ms |
41528 KB |
Output is correct |
17 |
Correct |
97 ms |
41336 KB |
Output is correct |
18 |
Correct |
103 ms |
41212 KB |
Output is correct |
19 |
Correct |
103 ms |
41300 KB |
Output is correct |
20 |
Correct |
101 ms |
41424 KB |
Output is correct |
21 |
Correct |
114 ms |
41608 KB |
Output is correct |
22 |
Correct |
94 ms |
41252 KB |
Output is correct |
23 |
Correct |
90 ms |
41176 KB |
Output is correct |
24 |
Correct |
92 ms |
41032 KB |
Output is correct |
25 |
Correct |
95 ms |
41208 KB |
Output is correct |
26 |
Correct |
114 ms |
41372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
258 ms |
68416 KB |
Output is correct |
2 |
Correct |
228 ms |
71740 KB |
Output is correct |
3 |
Correct |
233 ms |
71648 KB |
Output is correct |
4 |
Correct |
211 ms |
71760 KB |
Output is correct |
5 |
Correct |
245 ms |
71672 KB |
Output is correct |
6 |
Correct |
208 ms |
71072 KB |
Output is correct |
7 |
Correct |
172 ms |
71000 KB |
Output is correct |
8 |
Correct |
164 ms |
70836 KB |
Output is correct |
9 |
Correct |
180 ms |
70768 KB |
Output is correct |
10 |
Correct |
193 ms |
70652 KB |
Output is correct |
11 |
Correct |
172 ms |
70892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
39680 KB |
Output is correct |
2 |
Correct |
11 ms |
39772 KB |
Output is correct |
3 |
Correct |
13 ms |
40028 KB |
Output is correct |
4 |
Correct |
9 ms |
39516 KB |
Output is correct |
5 |
Correct |
10 ms |
39640 KB |
Output is correct |
6 |
Correct |
10 ms |
39516 KB |
Output is correct |
7 |
Correct |
11 ms |
39516 KB |
Output is correct |
8 |
Correct |
86 ms |
41196 KB |
Output is correct |
9 |
Correct |
88 ms |
41044 KB |
Output is correct |
10 |
Correct |
113 ms |
41396 KB |
Output is correct |
11 |
Correct |
100 ms |
41620 KB |
Output is correct |
12 |
Correct |
106 ms |
41164 KB |
Output is correct |
13 |
Correct |
101 ms |
41264 KB |
Output is correct |
14 |
Correct |
95 ms |
41216 KB |
Output is correct |
15 |
Correct |
110 ms |
41720 KB |
Output is correct |
16 |
Correct |
88 ms |
41528 KB |
Output is correct |
17 |
Correct |
97 ms |
41336 KB |
Output is correct |
18 |
Correct |
103 ms |
41212 KB |
Output is correct |
19 |
Correct |
103 ms |
41300 KB |
Output is correct |
20 |
Correct |
101 ms |
41424 KB |
Output is correct |
21 |
Correct |
114 ms |
41608 KB |
Output is correct |
22 |
Correct |
94 ms |
41252 KB |
Output is correct |
23 |
Correct |
90 ms |
41176 KB |
Output is correct |
24 |
Correct |
92 ms |
41032 KB |
Output is correct |
25 |
Correct |
95 ms |
41208 KB |
Output is correct |
26 |
Correct |
114 ms |
41372 KB |
Output is correct |
27 |
Correct |
258 ms |
68416 KB |
Output is correct |
28 |
Correct |
228 ms |
71740 KB |
Output is correct |
29 |
Correct |
233 ms |
71648 KB |
Output is correct |
30 |
Correct |
211 ms |
71760 KB |
Output is correct |
31 |
Correct |
245 ms |
71672 KB |
Output is correct |
32 |
Correct |
208 ms |
71072 KB |
Output is correct |
33 |
Correct |
172 ms |
71000 KB |
Output is correct |
34 |
Correct |
164 ms |
70836 KB |
Output is correct |
35 |
Correct |
180 ms |
70768 KB |
Output is correct |
36 |
Correct |
193 ms |
70652 KB |
Output is correct |
37 |
Correct |
172 ms |
70892 KB |
Output is correct |
38 |
Correct |
652 ms |
155072 KB |
Output is correct |
39 |
Correct |
691 ms |
178228 KB |
Output is correct |
40 |
Correct |
520 ms |
127368 KB |
Output is correct |
41 |
Correct |
535 ms |
178500 KB |
Output is correct |
42 |
Correct |
226 ms |
71968 KB |
Output is correct |
43 |
Correct |
224 ms |
71968 KB |
Output is correct |
44 |
Correct |
323 ms |
101948 KB |
Output is correct |
45 |
Correct |
287 ms |
102216 KB |
Output is correct |
46 |
Correct |
309 ms |
102492 KB |
Output is correct |
47 |
Correct |
180 ms |
72060 KB |
Output is correct |
48 |
Correct |
172 ms |
71688 KB |
Output is correct |
49 |
Correct |
270 ms |
101968 KB |
Output is correct |
50 |
Correct |
285 ms |
101940 KB |
Output is correct |
51 |
Correct |
304 ms |
102328 KB |
Output is correct |
52 |
Correct |
313 ms |
101668 KB |
Output is correct |
53 |
Correct |
395 ms |
131068 KB |
Output is correct |
54 |
Correct |
503 ms |
160812 KB |
Output is correct |
55 |
Correct |
390 ms |
131056 KB |
Output is correct |