#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
const int N = 1 << 20;
int last[N];
signed main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#else
#define endl '\n'
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
int n;
cin >> n;
vector <int> c(n-1);
for (int i = 0; i < n - 1; ++i)
cin >> c[i];
vector <vector <int> > a(n);
for (int i = 0; i < n; ++i) {
int k;
cin >> k;
a[i].resize(k);
for (int j = 0; j < k; ++j)
cin >> a[i][j];
}
vector <int> l(n), r(n);
for (int i = 0; i < n; ++i) {
l[i] = 0;
r[i] = n - 1;
}
vector <int> linkl(n), linkr(n);
for (int i = 0; i < N; ++i)
last[i] = -1;
for (int i = 0; i < n - 1; ++i) {
for (auto e : a[i])
last[e] = i;
linkl[i] = last[c[i]]+1;
}
for (int i = 0; i < N; ++i)
last[i] = n;
for (int i = n - 1; i >= 0; --i) {
if (i < n - 1) {
linkr[i] = last[c[i]]-1;
}
for (auto e : a[i])
last[e] = i;
}
for (int i = 0; i < n - 1; ++i) {
//(i+1,linkr[i])
for (int j = i; j <= linkr[i]; ++j) {
if (linkl[j] <= i+1) {
int sl = i+1;
int sr = j;
/*
if (i == 1 && j == 2) {
cout << "LMAO " << sl << ' ' << sr << endl;
exit(0);
}
*/
//cout << "find " << sl << ' ' << sr << endl;
for (int k = sl; k <= sr; ++k) {
l[k] = max(l[k], sl);
r[k] = min(r[k], sr);
}
}
}
}
for (int i = 0; i < n; ++i) {
if (linkl[i] == 0) {
for (int j = 0; j <= i; ++j)
r[j] = min(r[j], i);
}
}
/*
for (int i = 0; i < n - 1; ++i) {
cout << linkl[i] << ' ' << linkr[i] << endl;
}
*/
vector <int> t = {0, n - 1};
for (int i : t) {
l[i] = r[i] = i;
set <int> cur;
for (auto e : a[i])
cur.insert(e);
while (1) {
if (l[i] && cur.find(c[l[i]-1]) != cur.end()) {
--l[i];
for (auto e : a[l[i]])
cur.insert(e);
}
else if (r[i] < n - 1 && cur.find(c[r[i]]) != cur.end()) {
++r[i];
for (auto e : a[r[i]])
cur.insert(e);
}
else {
break;
}
}
}
/*
cout << "seg : " << endl;
for (int i = 0; i < n; ++i)
cout << l[i] << ' ' << r[i] << endl;
*/
int q;
cin >> q;
while (q--) {
int i, j;
cin >> i >> j;
--i; --j;
if (l[i] <= j && j <= r[i]) {
cout << "YES" << endl;
}
else {
cout << "NO" << endl;
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
8704 KB |
Output is correct |
2 |
Correct |
12 ms |
8832 KB |
Output is correct |
3 |
Correct |
14 ms |
9088 KB |
Output is correct |
4 |
Correct |
13 ms |
8832 KB |
Output is correct |
5 |
Correct |
11 ms |
8832 KB |
Output is correct |
6 |
Correct |
12 ms |
8704 KB |
Output is correct |
7 |
Correct |
249 ms |
8704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
8704 KB |
Output is correct |
2 |
Correct |
12 ms |
8832 KB |
Output is correct |
3 |
Correct |
14 ms |
9088 KB |
Output is correct |
4 |
Correct |
13 ms |
8832 KB |
Output is correct |
5 |
Correct |
11 ms |
8832 KB |
Output is correct |
6 |
Correct |
12 ms |
8704 KB |
Output is correct |
7 |
Correct |
249 ms |
8704 KB |
Output is correct |
8 |
Correct |
130 ms |
10232 KB |
Output is correct |
9 |
Correct |
124 ms |
10512 KB |
Output is correct |
10 |
Correct |
156 ms |
10488 KB |
Output is correct |
11 |
Correct |
120 ms |
10744 KB |
Output is correct |
12 |
Correct |
117 ms |
10364 KB |
Output is correct |
13 |
Correct |
132 ms |
10488 KB |
Output is correct |
14 |
Correct |
125 ms |
10488 KB |
Output is correct |
15 |
Correct |
124 ms |
10616 KB |
Output is correct |
16 |
Correct |
128 ms |
10744 KB |
Output is correct |
17 |
Correct |
122 ms |
10488 KB |
Output is correct |
18 |
Correct |
126 ms |
10492 KB |
Output is correct |
19 |
Correct |
115 ms |
10616 KB |
Output is correct |
20 |
Correct |
124 ms |
10616 KB |
Output is correct |
21 |
Correct |
121 ms |
10744 KB |
Output is correct |
22 |
Correct |
123 ms |
10624 KB |
Output is correct |
23 |
Correct |
356 ms |
10360 KB |
Output is correct |
24 |
Correct |
364 ms |
10360 KB |
Output is correct |
25 |
Correct |
367 ms |
10360 KB |
Output is correct |
26 |
Correct |
451 ms |
10360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
213 ms |
21272 KB |
Output is correct |
2 |
Correct |
200 ms |
28232 KB |
Output is correct |
3 |
Correct |
212 ms |
27808 KB |
Output is correct |
4 |
Correct |
226 ms |
28280 KB |
Output is correct |
5 |
Correct |
264 ms |
28280 KB |
Output is correct |
6 |
Correct |
198 ms |
26236 KB |
Output is correct |
7 |
Correct |
200 ms |
26104 KB |
Output is correct |
8 |
Correct |
209 ms |
25976 KB |
Output is correct |
9 |
Correct |
216 ms |
25976 KB |
Output is correct |
10 |
Correct |
202 ms |
25976 KB |
Output is correct |
11 |
Correct |
205 ms |
26080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
8704 KB |
Output is correct |
2 |
Correct |
12 ms |
8832 KB |
Output is correct |
3 |
Correct |
14 ms |
9088 KB |
Output is correct |
4 |
Correct |
13 ms |
8832 KB |
Output is correct |
5 |
Correct |
11 ms |
8832 KB |
Output is correct |
6 |
Correct |
12 ms |
8704 KB |
Output is correct |
7 |
Correct |
249 ms |
8704 KB |
Output is correct |
8 |
Correct |
130 ms |
10232 KB |
Output is correct |
9 |
Correct |
124 ms |
10512 KB |
Output is correct |
10 |
Correct |
156 ms |
10488 KB |
Output is correct |
11 |
Correct |
120 ms |
10744 KB |
Output is correct |
12 |
Correct |
117 ms |
10364 KB |
Output is correct |
13 |
Correct |
132 ms |
10488 KB |
Output is correct |
14 |
Correct |
125 ms |
10488 KB |
Output is correct |
15 |
Correct |
124 ms |
10616 KB |
Output is correct |
16 |
Correct |
128 ms |
10744 KB |
Output is correct |
17 |
Correct |
122 ms |
10488 KB |
Output is correct |
18 |
Correct |
126 ms |
10492 KB |
Output is correct |
19 |
Correct |
115 ms |
10616 KB |
Output is correct |
20 |
Correct |
124 ms |
10616 KB |
Output is correct |
21 |
Correct |
121 ms |
10744 KB |
Output is correct |
22 |
Correct |
123 ms |
10624 KB |
Output is correct |
23 |
Correct |
356 ms |
10360 KB |
Output is correct |
24 |
Correct |
364 ms |
10360 KB |
Output is correct |
25 |
Correct |
367 ms |
10360 KB |
Output is correct |
26 |
Correct |
451 ms |
10360 KB |
Output is correct |
27 |
Correct |
213 ms |
21272 KB |
Output is correct |
28 |
Correct |
200 ms |
28232 KB |
Output is correct |
29 |
Correct |
212 ms |
27808 KB |
Output is correct |
30 |
Correct |
226 ms |
28280 KB |
Output is correct |
31 |
Correct |
264 ms |
28280 KB |
Output is correct |
32 |
Correct |
198 ms |
26236 KB |
Output is correct |
33 |
Correct |
200 ms |
26104 KB |
Output is correct |
34 |
Correct |
209 ms |
25976 KB |
Output is correct |
35 |
Correct |
216 ms |
25976 KB |
Output is correct |
36 |
Correct |
202 ms |
25976 KB |
Output is correct |
37 |
Correct |
205 ms |
26080 KB |
Output is correct |
38 |
Execution timed out |
3099 ms |
51448 KB |
Time limit exceeded |
39 |
Halted |
0 ms |
0 KB |
- |