#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;
const int N = 500'010;
namespace seg {
int t[4*N];
int n;
void build(int *a, int i, int b, int e) {
if (e-b == 1) {
t[i] = a[b];
return;
}
build(a, 2*i+1, b, (b+e)/2);
build(a, 2*i+2, (b+e)/2, e);
t[i] = min(t[2*i+1], t[2*i+2]);
}
void build(int *a, int len) {
n = len;
build(a, 0, 0, n);
}
int get(int l, int r, int x, int i, int b, int e) {
if (r <= b || e <= l || t[i] >= x)
return r;
if (e-b == 1)
return b;
int ans = r;
ans = get(l, r, x, 2*i+1, b, (b+e)/2);
if (ans != r)
return ans;
ans = get(l, r, x, 2*i+2, (b+e)/2, e);
if (ans != r)
return ans;
return r;
}
int get(int l, int r, int x) {
if (l == r)
return r;
return get(l, r, x, 0, 0, n);
}
}
int col[N], lst[N];
int left_key[N], right_key[N];
int L[N], R[N];
vector<int> keys[N];
int n;
int main()
{
cin.tie(0) -> sync_with_stdio(false);
cin >> n;
Loop (i,0,n-1) {
cin >> col[i];
--col[i];
}
Loop (i,0,n) {
int k;
cin >> k;
while (k--) {
int x;
cin >> x;
keys[i].push_back(x-1);
}
}
fill(lst, lst+n, -1);
Loop (i,0,n-1) {
for (int k : keys[i])
lst[k] = i;
left_key[i] = lst[col[i]];
}
fill(lst, lst+n, n);
LoopR (i,1,n) {
for (int k : keys[i])
lst[k] = i;
right_key[i-1] = lst[col[i-1]];
}
seg::build(left_key, n-1);
Loop (i,0,n) {
L[i] = R[i] = i;
for (;;) {
int j = seg::get(R[i], n-1, L[i]);
R[i] = j;
if (L[i] == 0 || right_key[L[i]-1] > R[i])
break;
R[i] = max(R[i], R[L[i]-1]);
L[i] = min(L[i], L[L[i]-1]);
}
}
int q;
cin >> q;
while (q--) {
int x, y;
cin >> x >> y;
--x; --y;
cout << (L[x] <= y && y <= R[x]? "YES": "NO") << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
12244 KB |
Output is correct |
2 |
Correct |
9 ms |
12300 KB |
Output is correct |
3 |
Correct |
11 ms |
12468 KB |
Output is correct |
4 |
Correct |
9 ms |
12244 KB |
Output is correct |
5 |
Correct |
10 ms |
12320 KB |
Output is correct |
6 |
Correct |
9 ms |
12340 KB |
Output is correct |
7 |
Correct |
8 ms |
12244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
12244 KB |
Output is correct |
2 |
Correct |
9 ms |
12300 KB |
Output is correct |
3 |
Correct |
11 ms |
12468 KB |
Output is correct |
4 |
Correct |
9 ms |
12244 KB |
Output is correct |
5 |
Correct |
10 ms |
12320 KB |
Output is correct |
6 |
Correct |
9 ms |
12340 KB |
Output is correct |
7 |
Correct |
8 ms |
12244 KB |
Output is correct |
8 |
Correct |
98 ms |
18080 KB |
Output is correct |
9 |
Correct |
95 ms |
18092 KB |
Output is correct |
10 |
Correct |
99 ms |
18508 KB |
Output is correct |
11 |
Correct |
100 ms |
18824 KB |
Output is correct |
12 |
Correct |
91 ms |
17656 KB |
Output is correct |
13 |
Correct |
107 ms |
18284 KB |
Output is correct |
14 |
Correct |
91 ms |
18300 KB |
Output is correct |
15 |
Correct |
94 ms |
18320 KB |
Output is correct |
16 |
Correct |
95 ms |
18252 KB |
Output is correct |
17 |
Correct |
96 ms |
18280 KB |
Output is correct |
18 |
Correct |
90 ms |
18252 KB |
Output is correct |
19 |
Correct |
91 ms |
18308 KB |
Output is correct |
20 |
Correct |
94 ms |
18240 KB |
Output is correct |
21 |
Correct |
91 ms |
18180 KB |
Output is correct |
22 |
Correct |
100 ms |
18124 KB |
Output is correct |
23 |
Correct |
97 ms |
18268 KB |
Output is correct |
24 |
Correct |
101 ms |
18072 KB |
Output is correct |
25 |
Correct |
93 ms |
18012 KB |
Output is correct |
26 |
Correct |
98 ms |
18044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
174 ms |
28208 KB |
Output is correct |
2 |
Correct |
173 ms |
28072 KB |
Output is correct |
3 |
Correct |
166 ms |
27748 KB |
Output is correct |
4 |
Correct |
173 ms |
28164 KB |
Output is correct |
5 |
Correct |
177 ms |
28056 KB |
Output is correct |
6 |
Correct |
165 ms |
26756 KB |
Output is correct |
7 |
Correct |
143 ms |
26568 KB |
Output is correct |
8 |
Correct |
143 ms |
26616 KB |
Output is correct |
9 |
Correct |
152 ms |
26532 KB |
Output is correct |
10 |
Correct |
146 ms |
26620 KB |
Output is correct |
11 |
Correct |
147 ms |
26516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
12244 KB |
Output is correct |
2 |
Correct |
9 ms |
12300 KB |
Output is correct |
3 |
Correct |
11 ms |
12468 KB |
Output is correct |
4 |
Correct |
9 ms |
12244 KB |
Output is correct |
5 |
Correct |
10 ms |
12320 KB |
Output is correct |
6 |
Correct |
9 ms |
12340 KB |
Output is correct |
7 |
Correct |
8 ms |
12244 KB |
Output is correct |
8 |
Correct |
98 ms |
18080 KB |
Output is correct |
9 |
Correct |
95 ms |
18092 KB |
Output is correct |
10 |
Correct |
99 ms |
18508 KB |
Output is correct |
11 |
Correct |
100 ms |
18824 KB |
Output is correct |
12 |
Correct |
91 ms |
17656 KB |
Output is correct |
13 |
Correct |
107 ms |
18284 KB |
Output is correct |
14 |
Correct |
91 ms |
18300 KB |
Output is correct |
15 |
Correct |
94 ms |
18320 KB |
Output is correct |
16 |
Correct |
95 ms |
18252 KB |
Output is correct |
17 |
Correct |
96 ms |
18280 KB |
Output is correct |
18 |
Correct |
90 ms |
18252 KB |
Output is correct |
19 |
Correct |
91 ms |
18308 KB |
Output is correct |
20 |
Correct |
94 ms |
18240 KB |
Output is correct |
21 |
Correct |
91 ms |
18180 KB |
Output is correct |
22 |
Correct |
100 ms |
18124 KB |
Output is correct |
23 |
Correct |
97 ms |
18268 KB |
Output is correct |
24 |
Correct |
101 ms |
18072 KB |
Output is correct |
25 |
Correct |
93 ms |
18012 KB |
Output is correct |
26 |
Correct |
98 ms |
18044 KB |
Output is correct |
27 |
Correct |
174 ms |
28208 KB |
Output is correct |
28 |
Correct |
173 ms |
28072 KB |
Output is correct |
29 |
Correct |
166 ms |
27748 KB |
Output is correct |
30 |
Correct |
173 ms |
28164 KB |
Output is correct |
31 |
Correct |
177 ms |
28056 KB |
Output is correct |
32 |
Correct |
165 ms |
26756 KB |
Output is correct |
33 |
Correct |
143 ms |
26568 KB |
Output is correct |
34 |
Correct |
143 ms |
26616 KB |
Output is correct |
35 |
Correct |
152 ms |
26532 KB |
Output is correct |
36 |
Correct |
146 ms |
26620 KB |
Output is correct |
37 |
Correct |
147 ms |
26516 KB |
Output is correct |
38 |
Correct |
269 ms |
51220 KB |
Output is correct |
39 |
Correct |
302 ms |
56476 KB |
Output is correct |
40 |
Correct |
245 ms |
44680 KB |
Output is correct |
41 |
Correct |
305 ms |
54920 KB |
Output is correct |
42 |
Correct |
155 ms |
27764 KB |
Output is correct |
43 |
Correct |
157 ms |
27764 KB |
Output is correct |
44 |
Correct |
225 ms |
36940 KB |
Output is correct |
45 |
Correct |
224 ms |
37000 KB |
Output is correct |
46 |
Correct |
233 ms |
37196 KB |
Output is correct |
47 |
Correct |
156 ms |
27832 KB |
Output is correct |
48 |
Correct |
163 ms |
27516 KB |
Output is correct |
49 |
Correct |
225 ms |
36848 KB |
Output is correct |
50 |
Correct |
236 ms |
36964 KB |
Output is correct |
51 |
Correct |
232 ms |
37228 KB |
Output is correct |
52 |
Correct |
202 ms |
35788 KB |
Output is correct |
53 |
Correct |
251 ms |
45080 KB |
Output is correct |
54 |
Correct |
303 ms |
52204 KB |
Output is correct |
55 |
Correct |
273 ms |
45160 KB |
Output is correct |