#include <bits/stdc++.h>
using namespace std;
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;
cin >> nbChambres;
vector<int> type(nbChambres - 1);
for (int &t : type) {
cin >> t;
--t;
}
vector<vector<int>> posType(nbChambres);
for (int i = 0; i < nbChambres; ++i) {
int cnt;
cin >> cnt;
while (cnt--) {
int t;
cin >> t;
--t;
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];
}
dbg(lst);
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]);
dbg(L[iChambre], R[iChambre], q);
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]]);
}
}
dbg(L);
dbg(R);
int Q;
cin >> Q;
while (Q--) {
int from, to;
cin >> from >> to;
--from, --to;
cout << (L[from] <= to and to <= R[from] ? "YES" : "NO") << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
3 ms |
596 KB |
Output is correct |
3 |
Correct |
4 ms |
724 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
3 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
3 ms |
596 KB |
Output is correct |
3 |
Correct |
4 ms |
724 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
3 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
92 ms |
2036 KB |
Output is correct |
9 |
Correct |
89 ms |
1968 KB |
Output is correct |
10 |
Correct |
93 ms |
2268 KB |
Output is correct |
11 |
Correct |
91 ms |
2460 KB |
Output is correct |
12 |
Correct |
81 ms |
2228 KB |
Output is correct |
13 |
Correct |
90 ms |
2200 KB |
Output is correct |
14 |
Correct |
90 ms |
2156 KB |
Output is correct |
15 |
Correct |
86 ms |
2252 KB |
Output is correct |
16 |
Correct |
93 ms |
2400 KB |
Output is correct |
17 |
Correct |
87 ms |
2312 KB |
Output is correct |
18 |
Correct |
85 ms |
2252 KB |
Output is correct |
19 |
Correct |
100 ms |
2252 KB |
Output is correct |
20 |
Correct |
91 ms |
2340 KB |
Output is correct |
21 |
Correct |
88 ms |
2404 KB |
Output is correct |
22 |
Correct |
111 ms |
2252 KB |
Output is correct |
23 |
Correct |
93 ms |
2020 KB |
Output is correct |
24 |
Correct |
91 ms |
2052 KB |
Output is correct |
25 |
Correct |
106 ms |
2032 KB |
Output is correct |
26 |
Correct |
96 ms |
2124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
201 ms |
11256 KB |
Output is correct |
2 |
Correct |
171 ms |
10884 KB |
Output is correct |
3 |
Correct |
167 ms |
10972 KB |
Output is correct |
4 |
Correct |
167 ms |
11216 KB |
Output is correct |
5 |
Correct |
166 ms |
11380 KB |
Output is correct |
6 |
Correct |
152 ms |
10332 KB |
Output is correct |
7 |
Correct |
143 ms |
10400 KB |
Output is correct |
8 |
Correct |
146 ms |
10444 KB |
Output is correct |
9 |
Correct |
148 ms |
10420 KB |
Output is correct |
10 |
Correct |
145 ms |
10480 KB |
Output is correct |
11 |
Correct |
145 ms |
10448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
3 ms |
596 KB |
Output is correct |
3 |
Correct |
4 ms |
724 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
3 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
92 ms |
2036 KB |
Output is correct |
9 |
Correct |
89 ms |
1968 KB |
Output is correct |
10 |
Correct |
93 ms |
2268 KB |
Output is correct |
11 |
Correct |
91 ms |
2460 KB |
Output is correct |
12 |
Correct |
81 ms |
2228 KB |
Output is correct |
13 |
Correct |
90 ms |
2200 KB |
Output is correct |
14 |
Correct |
90 ms |
2156 KB |
Output is correct |
15 |
Correct |
86 ms |
2252 KB |
Output is correct |
16 |
Correct |
93 ms |
2400 KB |
Output is correct |
17 |
Correct |
87 ms |
2312 KB |
Output is correct |
18 |
Correct |
85 ms |
2252 KB |
Output is correct |
19 |
Correct |
100 ms |
2252 KB |
Output is correct |
20 |
Correct |
91 ms |
2340 KB |
Output is correct |
21 |
Correct |
88 ms |
2404 KB |
Output is correct |
22 |
Correct |
111 ms |
2252 KB |
Output is correct |
23 |
Correct |
93 ms |
2020 KB |
Output is correct |
24 |
Correct |
91 ms |
2052 KB |
Output is correct |
25 |
Correct |
106 ms |
2032 KB |
Output is correct |
26 |
Correct |
96 ms |
2124 KB |
Output is correct |
27 |
Correct |
201 ms |
11256 KB |
Output is correct |
28 |
Correct |
171 ms |
10884 KB |
Output is correct |
29 |
Correct |
167 ms |
10972 KB |
Output is correct |
30 |
Correct |
167 ms |
11216 KB |
Output is correct |
31 |
Correct |
166 ms |
11380 KB |
Output is correct |
32 |
Correct |
152 ms |
10332 KB |
Output is correct |
33 |
Correct |
143 ms |
10400 KB |
Output is correct |
34 |
Correct |
146 ms |
10444 KB |
Output is correct |
35 |
Correct |
148 ms |
10420 KB |
Output is correct |
36 |
Correct |
145 ms |
10480 KB |
Output is correct |
37 |
Correct |
145 ms |
10448 KB |
Output is correct |
38 |
Correct |
292 ms |
32780 KB |
Output is correct |
39 |
Correct |
330 ms |
36756 KB |
Output is correct |
40 |
Correct |
259 ms |
29196 KB |
Output is correct |
41 |
Correct |
352 ms |
36180 KB |
Output is correct |
42 |
Correct |
164 ms |
10648 KB |
Output is correct |
43 |
Correct |
194 ms |
10608 KB |
Output is correct |
44 |
Correct |
246 ms |
19480 KB |
Output is correct |
45 |
Correct |
271 ms |
19684 KB |
Output is correct |
46 |
Correct |
276 ms |
19848 KB |
Output is correct |
47 |
Correct |
168 ms |
10896 KB |
Output is correct |
48 |
Correct |
166 ms |
10504 KB |
Output is correct |
49 |
Correct |
242 ms |
19108 KB |
Output is correct |
50 |
Correct |
303 ms |
19400 KB |
Output is correct |
51 |
Correct |
267 ms |
20332 KB |
Output is correct |
52 |
Correct |
203 ms |
21868 KB |
Output is correct |
53 |
Correct |
253 ms |
34700 KB |
Output is correct |
54 |
Correct |
323 ms |
41948 KB |
Output is correct |
55 |
Correct |
249 ms |
34736 KB |
Output is correct |