#include <bits/stdc++.h>
const int INF = 1e9;
template <class T> struct MinSegTree {
const T ID = INF;
T comb(T a, T b) {
return std::min(a, b);
}
int n;
std::vector<T> seg;
void init(int _n) {
n = _n;
seg.assign(2 * n, ID);
}
void pull(int p) {
seg[p] = comb(seg[2 * p], seg[2 * p + 1]);
}
void upd(int p, T val) {
seg[p += n] = val;
for (p /= 2; p; p /= 2) pull(p);
}
void add(int p, T val) {
seg[p += n] += val;
for (p /= 2; p; p /= 2) pull(p);
}
T query(int l, int r) {
if (l > r) return ID;
T ra = ID, rb = ID;
for (l += n, r += n + 1; l < r; l /= 2, r /= 2) {
if (l & 1) ra = comb(ra, seg[l++]);
if (r & 1) rb = comb(seg[--r], rb);
}
return comb(ra, rb);
}
};
int main() {
using namespace std;
ios_base::sync_with_stdio(0);
int n; cin >> n;
vector<int> c(n);
c.back() = -1;
for (int i = 0; i < n - 1; i++)
cin >> c[i], c[i]--;
vector<vector<int>> a(n);
vector<int> b(n);
for (int i = 0; i < n; i++) {
cin >> b[i];
a[i].resize(b[i]);
for (int j = 0; j < b[i]; j++)
cin >> a[i][j], a[i][j]--;
}
int q; cin >> q;
vector<array<int, 2>> queries(q);
for (int i = 0 ; i< q; i++) {
cin >> queries[i][0] >> queries[i][1];
queries[i][0]--, queries[i][1]--;
}
vector<int> ans(q);
auto solve = [&]() {
vector<int> L(n), R(n);
vector<vector<int>> loc(n);
for (int i = 0; i < n; i++)
for (int j = 0; j < b[i]; j++)
loc[a[i][j]].push_back(i);
L.back() = -1;
R.back() = n;
for (int i = 0; i < n - 1; i++) {
auto it = lower_bound(loc[c[i]].begin(), loc[c[i]].end(), i + 1);
if (it == loc[c[i]].end()) R[i] = n;
else R[i] = *it;
it = upper_bound(loc[c[i]].begin(), loc[c[i]].end(), i);
if (it == loc[c[i]].begin()) L[i] = -1;
else L[i] = *prev(it);
}
vector<int> go_left(n);
vector<vector<int>> rem(n);
for (int i = 0; i < n; i++) {
int right = n;
if (i) right = R[i - 1];
if (right < n) rem[right].push_back(i);
}
MinSegTree<int> mst;
mst.init(n);
for (int i = 0; i < n; i++)
mst.upd(i, n);
for (int i = 0; i < n; i++) {
mst.upd(i, i);
for (int r : rem[i])
mst.upd(r, n);
go_left[i] = mst.query(L[i] + 1, i);
}
MinSegTree<int> seg;
seg.init(n);
for (int i = 0; i < n; i++)
seg.upd(i, go_left[i]);
for (int i = 0; i < q; i++) {
if (queries[i][0] < queries[i][1]) {
int l = queries[i][0];
int r = queries[i][1];
if (seg.query(l, r - 1) <= l)
ans[i] = 0;
else
ans[i] = 1;
}
}
};
solve();
reverse(a.begin(), a.end());
c.pop_back();
reverse(c.begin(), c.end());
c.push_back(-1);
reverse(b.begin(), b.end());
for (int i = 0; i < q; i++) {
queries[i][0] = n - 1 - queries[i][0];
queries[i][1] = n - 1 - queries[i][1];
}
solve();
for (int i = 0; i < q; i++)
cout << (ans[i] ? "YES" : "NO") << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
864 KB |
Output is correct |
2 |
Correct |
7 ms |
1004 KB |
Output is correct |
3 |
Correct |
8 ms |
1344 KB |
Output is correct |
4 |
Correct |
5 ms |
876 KB |
Output is correct |
5 |
Correct |
5 ms |
876 KB |
Output is correct |
6 |
Correct |
5 ms |
876 KB |
Output is correct |
7 |
Correct |
4 ms |
876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
864 KB |
Output is correct |
2 |
Correct |
7 ms |
1004 KB |
Output is correct |
3 |
Correct |
8 ms |
1344 KB |
Output is correct |
4 |
Correct |
5 ms |
876 KB |
Output is correct |
5 |
Correct |
5 ms |
876 KB |
Output is correct |
6 |
Correct |
5 ms |
876 KB |
Output is correct |
7 |
Correct |
4 ms |
876 KB |
Output is correct |
8 |
Correct |
193 ms |
8788 KB |
Output is correct |
9 |
Correct |
193 ms |
8792 KB |
Output is correct |
10 |
Correct |
194 ms |
9068 KB |
Output is correct |
11 |
Correct |
199 ms |
9424 KB |
Output is correct |
12 |
Correct |
177 ms |
8812 KB |
Output is correct |
13 |
Correct |
157 ms |
8940 KB |
Output is correct |
14 |
Correct |
159 ms |
9068 KB |
Output is correct |
15 |
Correct |
163 ms |
9068 KB |
Output is correct |
16 |
Correct |
168 ms |
9196 KB |
Output is correct |
17 |
Correct |
149 ms |
8940 KB |
Output is correct |
18 |
Correct |
162 ms |
8940 KB |
Output is correct |
19 |
Correct |
163 ms |
8960 KB |
Output is correct |
20 |
Correct |
166 ms |
9196 KB |
Output is correct |
21 |
Correct |
168 ms |
9196 KB |
Output is correct |
22 |
Correct |
168 ms |
9068 KB |
Output is correct |
23 |
Correct |
154 ms |
8940 KB |
Output is correct |
24 |
Correct |
152 ms |
8812 KB |
Output is correct |
25 |
Correct |
152 ms |
8812 KB |
Output is correct |
26 |
Correct |
148 ms |
8812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
392 ms |
26080 KB |
Output is correct |
2 |
Correct |
392 ms |
31352 KB |
Output is correct |
3 |
Correct |
387 ms |
31584 KB |
Output is correct |
4 |
Correct |
388 ms |
31148 KB |
Output is correct |
5 |
Correct |
397 ms |
29676 KB |
Output is correct |
6 |
Correct |
305 ms |
29292 KB |
Output is correct |
7 |
Correct |
331 ms |
31700 KB |
Output is correct |
8 |
Correct |
334 ms |
29780 KB |
Output is correct |
9 |
Correct |
330 ms |
29780 KB |
Output is correct |
10 |
Correct |
334 ms |
29908 KB |
Output is correct |
11 |
Correct |
332 ms |
29780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
864 KB |
Output is correct |
2 |
Correct |
7 ms |
1004 KB |
Output is correct |
3 |
Correct |
8 ms |
1344 KB |
Output is correct |
4 |
Correct |
5 ms |
876 KB |
Output is correct |
5 |
Correct |
5 ms |
876 KB |
Output is correct |
6 |
Correct |
5 ms |
876 KB |
Output is correct |
7 |
Correct |
4 ms |
876 KB |
Output is correct |
8 |
Correct |
193 ms |
8788 KB |
Output is correct |
9 |
Correct |
193 ms |
8792 KB |
Output is correct |
10 |
Correct |
194 ms |
9068 KB |
Output is correct |
11 |
Correct |
199 ms |
9424 KB |
Output is correct |
12 |
Correct |
177 ms |
8812 KB |
Output is correct |
13 |
Correct |
157 ms |
8940 KB |
Output is correct |
14 |
Correct |
159 ms |
9068 KB |
Output is correct |
15 |
Correct |
163 ms |
9068 KB |
Output is correct |
16 |
Correct |
168 ms |
9196 KB |
Output is correct |
17 |
Correct |
149 ms |
8940 KB |
Output is correct |
18 |
Correct |
162 ms |
8940 KB |
Output is correct |
19 |
Correct |
163 ms |
8960 KB |
Output is correct |
20 |
Correct |
166 ms |
9196 KB |
Output is correct |
21 |
Correct |
168 ms |
9196 KB |
Output is correct |
22 |
Correct |
168 ms |
9068 KB |
Output is correct |
23 |
Correct |
154 ms |
8940 KB |
Output is correct |
24 |
Correct |
152 ms |
8812 KB |
Output is correct |
25 |
Correct |
152 ms |
8812 KB |
Output is correct |
26 |
Correct |
148 ms |
8812 KB |
Output is correct |
27 |
Correct |
392 ms |
26080 KB |
Output is correct |
28 |
Correct |
392 ms |
31352 KB |
Output is correct |
29 |
Correct |
387 ms |
31584 KB |
Output is correct |
30 |
Correct |
388 ms |
31148 KB |
Output is correct |
31 |
Correct |
397 ms |
29676 KB |
Output is correct |
32 |
Correct |
305 ms |
29292 KB |
Output is correct |
33 |
Correct |
331 ms |
31700 KB |
Output is correct |
34 |
Correct |
334 ms |
29780 KB |
Output is correct |
35 |
Correct |
330 ms |
29780 KB |
Output is correct |
36 |
Correct |
334 ms |
29908 KB |
Output is correct |
37 |
Correct |
332 ms |
29780 KB |
Output is correct |
38 |
Correct |
925 ms |
77516 KB |
Output is correct |
39 |
Correct |
1006 ms |
90912 KB |
Output is correct |
40 |
Correct |
738 ms |
60652 KB |
Output is correct |
41 |
Correct |
916 ms |
92768 KB |
Output is correct |
42 |
Correct |
300 ms |
29420 KB |
Output is correct |
43 |
Correct |
303 ms |
29676 KB |
Output is correct |
44 |
Correct |
495 ms |
48632 KB |
Output is correct |
45 |
Correct |
500 ms |
48696 KB |
Output is correct |
46 |
Correct |
515 ms |
49096 KB |
Output is correct |
47 |
Correct |
346 ms |
30060 KB |
Output is correct |
48 |
Correct |
331 ms |
29548 KB |
Output is correct |
49 |
Correct |
498 ms |
46828 KB |
Output is correct |
50 |
Correct |
521 ms |
46892 KB |
Output is correct |
51 |
Correct |
568 ms |
47468 KB |
Output is correct |
52 |
Correct |
410 ms |
47188 KB |
Output is correct |
53 |
Correct |
539 ms |
64672 KB |
Output is correct |
54 |
Correct |
652 ms |
82036 KB |
Output is correct |
55 |
Correct |
542 ms |
62964 KB |
Output is correct |