#include <bits/stdc++.h>
using namespace std;
inline char gc() { // like getchar()
static char buf[1 << 16];
static size_t bc, be;
if (bc >= be) {
buf[0] = 0, bc = 0;
be = fread(buf, 1, sizeof(buf), stdin);
}
return buf[bc++]; // returns 0 on EOF
}
int readInt() {
int a, c;
while ((a = gc()) < 40)
;
if (a == '-')
return -readInt();
while ((c = gc()) >= 48)
a = a * 10 + c - 480;
return a - 48;
}
string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
bool first = true;
string res = "[";
for (const auto &x : v) {
if (!first)
res += ", ";
first = false;
res += to_string(x);
}
res += "]";
return res;
}
void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
cout << ' ' << to_string(H);
dbg_out(T...);
}
#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
const int MAXN = 2e6;
int iDeb[MAXN], iFin[MAXN], seg[MAXN];
vector<int> lst;
void build(int node, int l, int r) {
iDeb[node] = l, iFin[node] = r;
if (l == r) {
seg[node] = lst[l];
return;
}
build(2 * node, l, (l + r) / 2);
build(2 * node + 1, (l + r) / 2 + 1, r);
seg[node] = min(seg[2 * node], seg[2 * node + 1]);
}
int trav(int node, int from, int bound) { // premier i >= from tq lst[i] < bound
if (seg[node] >= bound or iFin[node] < from)
return -1;
if (iDeb[node] == iFin[node])
return iDeb[node];
int lft = trav(2 * node, from, bound);
if (lft == -1)
return trav(2 * node + 1, from, bound);
return lft;
}
signed main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);
int nbChambres = readInt();
vector<int> type(nbChambres - 1);
for (int &t : type)
t = readInt() - 1;
vector<vector<int>> posType(nbChambres);
for (int i = 0; i < nbChambres; ++i) {
int cnt = readInt();
while (cnt--) {
int t = readInt() - 1;
posType[t].push_back(i);
}
}
lst.resize(nbChambres);
for (int i = 1; i < nbChambres; ++i) {
int t = type[i - 1];
int fst = lower_bound(posType[t].begin(), posType[t].end(), i) -
posType[t].begin();
--fst;
if (fst < 0)
lst[i] = -1;
else
lst[i] = posType[t][fst];
}
build(1, 1, nbChambres - 1);
vector<int> L(nbChambres), R(nbChambres);
for (int iChambre = 0; iChambre < nbChambres; ++iChambre) {
L[iChambre] = R[iChambre] = iChambre;
while (L[iChambre] >= 0) {
if (R[iChambre] + 1 < nbChambres) {
int q = trav(1, R[iChambre] + 1, L[iChambre]);
if (q == -1)
R[iChambre] = nbChambres - 1;
else
R[iChambre] = q - 1;
}
if (L[iChambre] == 0)
break;
// can we reduce L[iChambre] ?
int t = type[L[iChambre] - 1];
auto fst =
lower_bound(posType[t].begin(), posType[t].end(), L[iChambre]) -
posType[t].begin();
if (fst == (int)posType[t].size() or posType[t][fst] > R[iChambre])
break;
L[iChambre] = L[L[iChambre] - 1];
R[iChambre] = max(R[iChambre], R[L[iChambre]]);
}
}
int Q = readInt();
while (Q--) {
int from = readInt() - 1, to = readInt() - 1;
cout << (L[from] <= to and to <= R[from] ? "YES" : "NO") << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
2 ms |
852 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
2 ms |
852 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
38 ms |
2076 KB |
Output is correct |
9 |
Correct |
40 ms |
2064 KB |
Output is correct |
10 |
Correct |
41 ms |
2308 KB |
Output is correct |
11 |
Correct |
36 ms |
2584 KB |
Output is correct |
12 |
Correct |
35 ms |
2252 KB |
Output is correct |
13 |
Correct |
34 ms |
2288 KB |
Output is correct |
14 |
Correct |
40 ms |
2260 KB |
Output is correct |
15 |
Correct |
37 ms |
2324 KB |
Output is correct |
16 |
Correct |
37 ms |
2496 KB |
Output is correct |
17 |
Correct |
34 ms |
2256 KB |
Output is correct |
18 |
Correct |
38 ms |
2248 KB |
Output is correct |
19 |
Correct |
50 ms |
2212 KB |
Output is correct |
20 |
Correct |
38 ms |
2396 KB |
Output is correct |
21 |
Correct |
37 ms |
2516 KB |
Output is correct |
22 |
Correct |
42 ms |
2332 KB |
Output is correct |
23 |
Correct |
41 ms |
2176 KB |
Output is correct |
24 |
Correct |
42 ms |
2156 KB |
Output is correct |
25 |
Correct |
38 ms |
2088 KB |
Output is correct |
26 |
Correct |
38 ms |
2112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
85 ms |
11376 KB |
Output is correct |
2 |
Correct |
83 ms |
10908 KB |
Output is correct |
3 |
Correct |
83 ms |
11016 KB |
Output is correct |
4 |
Correct |
87 ms |
11304 KB |
Output is correct |
5 |
Correct |
84 ms |
11384 KB |
Output is correct |
6 |
Correct |
81 ms |
10340 KB |
Output is correct |
7 |
Correct |
73 ms |
10528 KB |
Output is correct |
8 |
Correct |
69 ms |
10572 KB |
Output is correct |
9 |
Correct |
71 ms |
10568 KB |
Output is correct |
10 |
Correct |
73 ms |
10620 KB |
Output is correct |
11 |
Correct |
65 ms |
10560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
2 ms |
852 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
38 ms |
2076 KB |
Output is correct |
9 |
Correct |
40 ms |
2064 KB |
Output is correct |
10 |
Correct |
41 ms |
2308 KB |
Output is correct |
11 |
Correct |
36 ms |
2584 KB |
Output is correct |
12 |
Correct |
35 ms |
2252 KB |
Output is correct |
13 |
Correct |
34 ms |
2288 KB |
Output is correct |
14 |
Correct |
40 ms |
2260 KB |
Output is correct |
15 |
Correct |
37 ms |
2324 KB |
Output is correct |
16 |
Correct |
37 ms |
2496 KB |
Output is correct |
17 |
Correct |
34 ms |
2256 KB |
Output is correct |
18 |
Correct |
38 ms |
2248 KB |
Output is correct |
19 |
Correct |
50 ms |
2212 KB |
Output is correct |
20 |
Correct |
38 ms |
2396 KB |
Output is correct |
21 |
Correct |
37 ms |
2516 KB |
Output is correct |
22 |
Correct |
42 ms |
2332 KB |
Output is correct |
23 |
Correct |
41 ms |
2176 KB |
Output is correct |
24 |
Correct |
42 ms |
2156 KB |
Output is correct |
25 |
Correct |
38 ms |
2088 KB |
Output is correct |
26 |
Correct |
38 ms |
2112 KB |
Output is correct |
27 |
Correct |
85 ms |
11376 KB |
Output is correct |
28 |
Correct |
83 ms |
10908 KB |
Output is correct |
29 |
Correct |
83 ms |
11016 KB |
Output is correct |
30 |
Correct |
87 ms |
11304 KB |
Output is correct |
31 |
Correct |
84 ms |
11384 KB |
Output is correct |
32 |
Correct |
81 ms |
10340 KB |
Output is correct |
33 |
Correct |
73 ms |
10528 KB |
Output is correct |
34 |
Correct |
69 ms |
10572 KB |
Output is correct |
35 |
Correct |
71 ms |
10568 KB |
Output is correct |
36 |
Correct |
73 ms |
10620 KB |
Output is correct |
37 |
Correct |
65 ms |
10560 KB |
Output is correct |
38 |
Correct |
228 ms |
32844 KB |
Output is correct |
39 |
Correct |
204 ms |
36864 KB |
Output is correct |
40 |
Correct |
141 ms |
29132 KB |
Output is correct |
41 |
Correct |
211 ms |
36140 KB |
Output is correct |
42 |
Correct |
87 ms |
10652 KB |
Output is correct |
43 |
Correct |
85 ms |
10560 KB |
Output is correct |
44 |
Correct |
160 ms |
19600 KB |
Output is correct |
45 |
Correct |
157 ms |
19716 KB |
Output is correct |
46 |
Correct |
159 ms |
19920 KB |
Output is correct |
47 |
Correct |
87 ms |
11056 KB |
Output is correct |
48 |
Correct |
80 ms |
10536 KB |
Output is correct |
49 |
Correct |
146 ms |
19076 KB |
Output is correct |
50 |
Correct |
137 ms |
19528 KB |
Output is correct |
51 |
Correct |
208 ms |
20300 KB |
Output is correct |
52 |
Correct |
101 ms |
21872 KB |
Output is correct |
53 |
Correct |
139 ms |
34780 KB |
Output is correct |
54 |
Correct |
212 ms |
42100 KB |
Output is correct |
55 |
Correct |
137 ms |
34872 KB |
Output is correct |