// 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 |
9 ms |
39516 KB |
Output is correct |
2 |
Correct |
10 ms |
40020 KB |
Output is correct |
3 |
Correct |
11 ms |
39872 KB |
Output is correct |
4 |
Correct |
10 ms |
39516 KB |
Output is correct |
5 |
Correct |
11 ms |
39696 KB |
Output is correct |
6 |
Correct |
9 ms |
39624 KB |
Output is correct |
7 |
Correct |
9 ms |
39588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
39516 KB |
Output is correct |
2 |
Correct |
10 ms |
40020 KB |
Output is correct |
3 |
Correct |
11 ms |
39872 KB |
Output is correct |
4 |
Correct |
10 ms |
39516 KB |
Output is correct |
5 |
Correct |
11 ms |
39696 KB |
Output is correct |
6 |
Correct |
9 ms |
39624 KB |
Output is correct |
7 |
Correct |
9 ms |
39588 KB |
Output is correct |
8 |
Correct |
86 ms |
41040 KB |
Output is correct |
9 |
Correct |
85 ms |
41128 KB |
Output is correct |
10 |
Correct |
96 ms |
41292 KB |
Output is correct |
11 |
Correct |
97 ms |
41588 KB |
Output is correct |
12 |
Correct |
93 ms |
41040 KB |
Output is correct |
13 |
Correct |
96 ms |
41164 KB |
Output is correct |
14 |
Correct |
88 ms |
41308 KB |
Output is correct |
15 |
Correct |
88 ms |
41296 KB |
Output is correct |
16 |
Correct |
82 ms |
41740 KB |
Output is correct |
17 |
Correct |
101 ms |
41256 KB |
Output is correct |
18 |
Correct |
88 ms |
41300 KB |
Output is correct |
19 |
Correct |
89 ms |
41412 KB |
Output is correct |
20 |
Correct |
86 ms |
41520 KB |
Output is correct |
21 |
Correct |
83 ms |
41664 KB |
Output is correct |
22 |
Correct |
88 ms |
41196 KB |
Output is correct |
23 |
Correct |
84 ms |
41052 KB |
Output is correct |
24 |
Correct |
84 ms |
41044 KB |
Output is correct |
25 |
Correct |
85 ms |
41108 KB |
Output is correct |
26 |
Correct |
85 ms |
41184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
251 ms |
64644 KB |
Output is correct |
2 |
Correct |
197 ms |
64556 KB |
Output is correct |
3 |
Correct |
199 ms |
64436 KB |
Output is correct |
4 |
Correct |
197 ms |
64616 KB |
Output is correct |
5 |
Correct |
196 ms |
64416 KB |
Output is correct |
6 |
Correct |
170 ms |
64404 KB |
Output is correct |
7 |
Correct |
172 ms |
64784 KB |
Output is correct |
8 |
Correct |
181 ms |
64788 KB |
Output is correct |
9 |
Correct |
181 ms |
64820 KB |
Output is correct |
10 |
Correct |
176 ms |
64732 KB |
Output is correct |
11 |
Correct |
185 ms |
64636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
39516 KB |
Output is correct |
2 |
Correct |
10 ms |
40020 KB |
Output is correct |
3 |
Correct |
11 ms |
39872 KB |
Output is correct |
4 |
Correct |
10 ms |
39516 KB |
Output is correct |
5 |
Correct |
11 ms |
39696 KB |
Output is correct |
6 |
Correct |
9 ms |
39624 KB |
Output is correct |
7 |
Correct |
9 ms |
39588 KB |
Output is correct |
8 |
Correct |
86 ms |
41040 KB |
Output is correct |
9 |
Correct |
85 ms |
41128 KB |
Output is correct |
10 |
Correct |
96 ms |
41292 KB |
Output is correct |
11 |
Correct |
97 ms |
41588 KB |
Output is correct |
12 |
Correct |
93 ms |
41040 KB |
Output is correct |
13 |
Correct |
96 ms |
41164 KB |
Output is correct |
14 |
Correct |
88 ms |
41308 KB |
Output is correct |
15 |
Correct |
88 ms |
41296 KB |
Output is correct |
16 |
Correct |
82 ms |
41740 KB |
Output is correct |
17 |
Correct |
101 ms |
41256 KB |
Output is correct |
18 |
Correct |
88 ms |
41300 KB |
Output is correct |
19 |
Correct |
89 ms |
41412 KB |
Output is correct |
20 |
Correct |
86 ms |
41520 KB |
Output is correct |
21 |
Correct |
83 ms |
41664 KB |
Output is correct |
22 |
Correct |
88 ms |
41196 KB |
Output is correct |
23 |
Correct |
84 ms |
41052 KB |
Output is correct |
24 |
Correct |
84 ms |
41044 KB |
Output is correct |
25 |
Correct |
85 ms |
41108 KB |
Output is correct |
26 |
Correct |
85 ms |
41184 KB |
Output is correct |
27 |
Correct |
251 ms |
64644 KB |
Output is correct |
28 |
Correct |
197 ms |
64556 KB |
Output is correct |
29 |
Correct |
199 ms |
64436 KB |
Output is correct |
30 |
Correct |
197 ms |
64616 KB |
Output is correct |
31 |
Correct |
196 ms |
64416 KB |
Output is correct |
32 |
Correct |
170 ms |
64404 KB |
Output is correct |
33 |
Correct |
172 ms |
64784 KB |
Output is correct |
34 |
Correct |
181 ms |
64788 KB |
Output is correct |
35 |
Correct |
181 ms |
64820 KB |
Output is correct |
36 |
Correct |
176 ms |
64732 KB |
Output is correct |
37 |
Correct |
185 ms |
64636 KB |
Output is correct |
38 |
Correct |
682 ms |
143576 KB |
Output is correct |
39 |
Correct |
698 ms |
166732 KB |
Output is correct |
40 |
Correct |
472 ms |
117060 KB |
Output is correct |
41 |
Correct |
539 ms |
168812 KB |
Output is correct |
42 |
Correct |
189 ms |
64480 KB |
Output is correct |
43 |
Correct |
181 ms |
64480 KB |
Output is correct |
44 |
Correct |
277 ms |
91984 KB |
Output is correct |
45 |
Correct |
285 ms |
91832 KB |
Output is correct |
46 |
Correct |
276 ms |
91988 KB |
Output is correct |
47 |
Correct |
181 ms |
64664 KB |
Output is correct |
48 |
Correct |
168 ms |
64596 KB |
Output is correct |
49 |
Correct |
261 ms |
91852 KB |
Output is correct |
50 |
Correct |
268 ms |
91988 KB |
Output is correct |
51 |
Correct |
266 ms |
91920 KB |
Output is correct |
52 |
Correct |
276 ms |
92540 KB |
Output is correct |
53 |
Correct |
383 ms |
119992 KB |
Output is correct |
54 |
Correct |
472 ms |
148152 KB |
Output is correct |
55 |
Correct |
390 ms |
120004 KB |
Output is correct |