#include "bits/stdc++.h"
using namespace std;
void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T a, U ...b) {
cout << a << ' ', abc(b...);
}
template <typename T> void printv(T l, T r) {
while (l != r) cout << *l << " \n"[++l == r];
}
template <typename A, typename B> istream& operator >> (istream& o, pair<A, B> &a) {
return o >> a.first >> a.second;
}
template <typename A, typename B> ostream& operator << (ostream& o, pair<A, B> a) {
return o << '(' << a.first << ", " << a.second << ')';
}
template <typename T> ostream& operator << (ostream& o, vector<T> a) {
bool is = false;
for (T i : a) {o << (is ? ' ' : '{'), is = true, o << i;}
return o << '}';
}
#ifdef local
#define test(args...) abc("[" + string(#args) + "]", args)
#else
#define test(args...) void(0)
#endif
using ll = long long;
int col[500005];
vector<int> keys[500005];
int seen[500005];
int leftt[500005], rightt[500005], par[500005];
int find(int node) {
if (par[node] == node) return node;
return (par[node] = find(par[node]));
}
void Union(int a, int b) {
a = find(a); b = find(b);
if (a == b) return;
par[a] = b;
}
void dfs(int node) {
seen[node] = 1;
par[node] = node;
leftt[node] = rightt[node] = node;
while (1) {
// try to extend
int curleft = leftt[find(node)], curright = rightt[find(node)];
if (lower_bound(keys[col[curleft - 1]].begin(), keys[col[curleft - 1]].end(), curleft) != upper_bound(keys[col[curleft - 1]].begin(), keys[col[curleft - 1]].end(), curright)) {
// can extend to left
leftt[find(node)] = leftt[find(curleft - 1)];
if (seen[curleft - 1] == 1 && find(curleft - 1) != find(node)) {
Union(node, curleft - 1);
rightt[find(node)] = rightt[node];
}
}
if (lower_bound(keys[col[curright]].begin(), keys[col[curright]].end(), curleft) != upper_bound(keys[col[curright]].begin(), keys[col[curright]].end(), curright)) {
if (seen[curright + 1] == 0) dfs(curright + 1);
rightt[find(node)] = rightt[find(curright + 1)];
}
if (curleft == leftt[find(node)] && curright == rightt[find(node)]) break;
}
seen[node] = 2;
}
int ansl[500005], ansr[500005];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen("", "r", stdin);
// freopen("", "w", stdout);
int n; cin >> n;
for (int i = 1; i < n; i++) cin >> col[i];
for (int i = 1; i <= n; i++) {
int x; cin >> x;
for (int j = 0; j < x; j++) {
int t; cin >> t;
keys[t].push_back(i);
}
}
for (int i = 1; i <= n; i++) {
if (!seen[i]) dfs(i);
}
for (int i = 1; i <= n; i++) {
ansl[i] = leftt[find(i)];
ansr[i] = rightt[find(i)];
}
int q ;cin >> q;
while (q--) {
int x, y; cin >> x >> y;
if (y > ansr[x] || y < ansl[x]) {
cout << "NO\n";
} else cout << "YES\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12244 KB |
Output is correct |
2 |
Correct |
9 ms |
12372 KB |
Output is correct |
3 |
Correct |
10 ms |
12628 KB |
Output is correct |
4 |
Correct |
9 ms |
12244 KB |
Output is correct |
5 |
Correct |
8 ms |
12476 KB |
Output is correct |
6 |
Correct |
8 ms |
12244 KB |
Output is correct |
7 |
Correct |
7 ms |
12352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12244 KB |
Output is correct |
2 |
Correct |
9 ms |
12372 KB |
Output is correct |
3 |
Correct |
10 ms |
12628 KB |
Output is correct |
4 |
Correct |
9 ms |
12244 KB |
Output is correct |
5 |
Correct |
8 ms |
12476 KB |
Output is correct |
6 |
Correct |
8 ms |
12244 KB |
Output is correct |
7 |
Correct |
7 ms |
12352 KB |
Output is correct |
8 |
Correct |
93 ms |
18116 KB |
Output is correct |
9 |
Correct |
86 ms |
18068 KB |
Output is correct |
10 |
Correct |
99 ms |
18476 KB |
Output is correct |
11 |
Correct |
88 ms |
19008 KB |
Output is correct |
12 |
Correct |
78 ms |
17740 KB |
Output is correct |
13 |
Correct |
86 ms |
18300 KB |
Output is correct |
14 |
Correct |
84 ms |
18252 KB |
Output is correct |
15 |
Correct |
84 ms |
18324 KB |
Output is correct |
16 |
Correct |
83 ms |
18380 KB |
Output is correct |
17 |
Correct |
84 ms |
18308 KB |
Output is correct |
18 |
Correct |
84 ms |
18324 KB |
Output is correct |
19 |
Correct |
83 ms |
18276 KB |
Output is correct |
20 |
Correct |
85 ms |
18432 KB |
Output is correct |
21 |
Correct |
90 ms |
18296 KB |
Output is correct |
22 |
Correct |
89 ms |
18272 KB |
Output is correct |
23 |
Correct |
87 ms |
18100 KB |
Output is correct |
24 |
Correct |
85 ms |
18132 KB |
Output is correct |
25 |
Correct |
86 ms |
18044 KB |
Output is correct |
26 |
Correct |
90 ms |
18108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
176 ms |
27144 KB |
Output is correct |
2 |
Correct |
172 ms |
27900 KB |
Output is correct |
3 |
Correct |
172 ms |
30108 KB |
Output is correct |
4 |
Correct |
175 ms |
27300 KB |
Output is correct |
5 |
Correct |
181 ms |
27628 KB |
Output is correct |
6 |
Correct |
151 ms |
29096 KB |
Output is correct |
7 |
Correct |
152 ms |
34192 KB |
Output is correct |
8 |
Correct |
147 ms |
34604 KB |
Output is correct |
9 |
Correct |
140 ms |
34500 KB |
Output is correct |
10 |
Correct |
139 ms |
34900 KB |
Output is correct |
11 |
Correct |
144 ms |
34908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12244 KB |
Output is correct |
2 |
Correct |
9 ms |
12372 KB |
Output is correct |
3 |
Correct |
10 ms |
12628 KB |
Output is correct |
4 |
Correct |
9 ms |
12244 KB |
Output is correct |
5 |
Correct |
8 ms |
12476 KB |
Output is correct |
6 |
Correct |
8 ms |
12244 KB |
Output is correct |
7 |
Correct |
7 ms |
12352 KB |
Output is correct |
8 |
Correct |
93 ms |
18116 KB |
Output is correct |
9 |
Correct |
86 ms |
18068 KB |
Output is correct |
10 |
Correct |
99 ms |
18476 KB |
Output is correct |
11 |
Correct |
88 ms |
19008 KB |
Output is correct |
12 |
Correct |
78 ms |
17740 KB |
Output is correct |
13 |
Correct |
86 ms |
18300 KB |
Output is correct |
14 |
Correct |
84 ms |
18252 KB |
Output is correct |
15 |
Correct |
84 ms |
18324 KB |
Output is correct |
16 |
Correct |
83 ms |
18380 KB |
Output is correct |
17 |
Correct |
84 ms |
18308 KB |
Output is correct |
18 |
Correct |
84 ms |
18324 KB |
Output is correct |
19 |
Correct |
83 ms |
18276 KB |
Output is correct |
20 |
Correct |
85 ms |
18432 KB |
Output is correct |
21 |
Correct |
90 ms |
18296 KB |
Output is correct |
22 |
Correct |
89 ms |
18272 KB |
Output is correct |
23 |
Correct |
87 ms |
18100 KB |
Output is correct |
24 |
Correct |
85 ms |
18132 KB |
Output is correct |
25 |
Correct |
86 ms |
18044 KB |
Output is correct |
26 |
Correct |
90 ms |
18108 KB |
Output is correct |
27 |
Correct |
176 ms |
27144 KB |
Output is correct |
28 |
Correct |
172 ms |
27900 KB |
Output is correct |
29 |
Correct |
172 ms |
30108 KB |
Output is correct |
30 |
Correct |
175 ms |
27300 KB |
Output is correct |
31 |
Correct |
181 ms |
27628 KB |
Output is correct |
32 |
Correct |
151 ms |
29096 KB |
Output is correct |
33 |
Correct |
152 ms |
34192 KB |
Output is correct |
34 |
Correct |
147 ms |
34604 KB |
Output is correct |
35 |
Correct |
140 ms |
34500 KB |
Output is correct |
36 |
Correct |
139 ms |
34900 KB |
Output is correct |
37 |
Correct |
144 ms |
34908 KB |
Output is correct |
38 |
Correct |
235 ms |
39236 KB |
Output is correct |
39 |
Correct |
242 ms |
41676 KB |
Output is correct |
40 |
Correct |
210 ms |
35476 KB |
Output is correct |
41 |
Correct |
380 ms |
69832 KB |
Output is correct |
42 |
Correct |
148 ms |
25580 KB |
Output is correct |
43 |
Correct |
139 ms |
25576 KB |
Output is correct |
44 |
Correct |
217 ms |
32908 KB |
Output is correct |
45 |
Correct |
213 ms |
33228 KB |
Output is correct |
46 |
Correct |
242 ms |
33808 KB |
Output is correct |
47 |
Correct |
144 ms |
31168 KB |
Output is correct |
48 |
Correct |
142 ms |
30600 KB |
Output is correct |
49 |
Correct |
208 ms |
34616 KB |
Output is correct |
50 |
Correct |
226 ms |
37036 KB |
Output is correct |
51 |
Correct |
236 ms |
38308 KB |
Output is correct |
52 |
Correct |
170 ms |
36404 KB |
Output is correct |
53 |
Correct |
202 ms |
45840 KB |
Output is correct |
54 |
Correct |
230 ms |
51416 KB |
Output is correct |
55 |
Correct |
194 ms |
43896 KB |
Output is correct |