#include <bits/stdc++.h>
#define int long long
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 |
3 ms |
596 KB |
Output is correct |
2 |
Correct |
3 ms |
724 KB |
Output is correct |
3 |
Correct |
5 ms |
980 KB |
Output is correct |
4 |
Correct |
3 ms |
616 KB |
Output is correct |
5 |
Correct |
3 ms |
596 KB |
Output is correct |
6 |
Correct |
3 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
596 KB |
Output is correct |
2 |
Correct |
3 ms |
724 KB |
Output is correct |
3 |
Correct |
5 ms |
980 KB |
Output is correct |
4 |
Correct |
3 ms |
616 KB |
Output is correct |
5 |
Correct |
3 ms |
596 KB |
Output is correct |
6 |
Correct |
3 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
109 ms |
2100 KB |
Output is correct |
9 |
Correct |
93 ms |
2040 KB |
Output is correct |
10 |
Correct |
99 ms |
2380 KB |
Output is correct |
11 |
Correct |
102 ms |
2892 KB |
Output is correct |
12 |
Correct |
84 ms |
2148 KB |
Output is correct |
13 |
Correct |
94 ms |
2216 KB |
Output is correct |
14 |
Correct |
97 ms |
2264 KB |
Output is correct |
15 |
Correct |
87 ms |
2368 KB |
Output is correct |
16 |
Correct |
122 ms |
2520 KB |
Output is correct |
17 |
Correct |
87 ms |
2272 KB |
Output is correct |
18 |
Correct |
94 ms |
2288 KB |
Output is correct |
19 |
Correct |
123 ms |
2276 KB |
Output is correct |
20 |
Correct |
88 ms |
2396 KB |
Output is correct |
21 |
Correct |
100 ms |
2508 KB |
Output is correct |
22 |
Correct |
102 ms |
2244 KB |
Output is correct |
23 |
Correct |
96 ms |
2152 KB |
Output is correct |
24 |
Correct |
95 ms |
2228 KB |
Output is correct |
25 |
Correct |
98 ms |
2124 KB |
Output is correct |
26 |
Correct |
92 ms |
2140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
197 ms |
18060 KB |
Output is correct |
2 |
Correct |
177 ms |
17360 KB |
Output is correct |
3 |
Correct |
195 ms |
16972 KB |
Output is correct |
4 |
Correct |
179 ms |
17508 KB |
Output is correct |
5 |
Correct |
183 ms |
17512 KB |
Output is correct |
6 |
Correct |
167 ms |
16080 KB |
Output is correct |
7 |
Correct |
155 ms |
16208 KB |
Output is correct |
8 |
Correct |
160 ms |
16288 KB |
Output is correct |
9 |
Correct |
179 ms |
16196 KB |
Output is correct |
10 |
Correct |
147 ms |
16360 KB |
Output is correct |
11 |
Correct |
152 ms |
16208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
596 KB |
Output is correct |
2 |
Correct |
3 ms |
724 KB |
Output is correct |
3 |
Correct |
5 ms |
980 KB |
Output is correct |
4 |
Correct |
3 ms |
616 KB |
Output is correct |
5 |
Correct |
3 ms |
596 KB |
Output is correct |
6 |
Correct |
3 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
109 ms |
2100 KB |
Output is correct |
9 |
Correct |
93 ms |
2040 KB |
Output is correct |
10 |
Correct |
99 ms |
2380 KB |
Output is correct |
11 |
Correct |
102 ms |
2892 KB |
Output is correct |
12 |
Correct |
84 ms |
2148 KB |
Output is correct |
13 |
Correct |
94 ms |
2216 KB |
Output is correct |
14 |
Correct |
97 ms |
2264 KB |
Output is correct |
15 |
Correct |
87 ms |
2368 KB |
Output is correct |
16 |
Correct |
122 ms |
2520 KB |
Output is correct |
17 |
Correct |
87 ms |
2272 KB |
Output is correct |
18 |
Correct |
94 ms |
2288 KB |
Output is correct |
19 |
Correct |
123 ms |
2276 KB |
Output is correct |
20 |
Correct |
88 ms |
2396 KB |
Output is correct |
21 |
Correct |
100 ms |
2508 KB |
Output is correct |
22 |
Correct |
102 ms |
2244 KB |
Output is correct |
23 |
Correct |
96 ms |
2152 KB |
Output is correct |
24 |
Correct |
95 ms |
2228 KB |
Output is correct |
25 |
Correct |
98 ms |
2124 KB |
Output is correct |
26 |
Correct |
92 ms |
2140 KB |
Output is correct |
27 |
Correct |
197 ms |
18060 KB |
Output is correct |
28 |
Correct |
177 ms |
17360 KB |
Output is correct |
29 |
Correct |
195 ms |
16972 KB |
Output is correct |
30 |
Correct |
179 ms |
17508 KB |
Output is correct |
31 |
Correct |
183 ms |
17512 KB |
Output is correct |
32 |
Correct |
167 ms |
16080 KB |
Output is correct |
33 |
Correct |
155 ms |
16208 KB |
Output is correct |
34 |
Correct |
160 ms |
16288 KB |
Output is correct |
35 |
Correct |
179 ms |
16196 KB |
Output is correct |
36 |
Correct |
147 ms |
16360 KB |
Output is correct |
37 |
Correct |
152 ms |
16208 KB |
Output is correct |
38 |
Correct |
321 ms |
54204 KB |
Output is correct |
39 |
Correct |
402 ms |
59924 KB |
Output is correct |
40 |
Correct |
286 ms |
49176 KB |
Output is correct |
41 |
Correct |
352 ms |
58228 KB |
Output is correct |
42 |
Correct |
189 ms |
16012 KB |
Output is correct |
43 |
Correct |
171 ms |
16276 KB |
Output is correct |
44 |
Correct |
276 ms |
30560 KB |
Output is correct |
45 |
Correct |
295 ms |
30592 KB |
Output is correct |
46 |
Correct |
287 ms |
30516 KB |
Output is correct |
47 |
Correct |
201 ms |
16388 KB |
Output is correct |
48 |
Correct |
170 ms |
16368 KB |
Output is correct |
49 |
Correct |
270 ms |
30536 KB |
Output is correct |
50 |
Correct |
305 ms |
30776 KB |
Output is correct |
51 |
Correct |
365 ms |
30692 KB |
Output is correct |
52 |
Correct |
226 ms |
37416 KB |
Output is correct |
53 |
Correct |
333 ms |
63204 KB |
Output is correct |
54 |
Correct |
340 ms |
73452 KB |
Output is correct |
55 |
Correct |
285 ms |
63388 KB |
Output is correct |