#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
#define int long long
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef double db;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tree<iii, null_type, greater<iii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
int N, Q, X, Y, C[500005], L[500005], R[500005], SR[500005];
vector<int> rev[500005];
bool can(int x, int l, int r) { // collecting all keys from [l, r], possible to pass thru corridor x?
auto it = lower_bound(rev[C[x]].begin(), rev[C[x]].end(), l);
if (it != rev[C[x]].end() && *it <= r) return 1;
return 0;
}
ii expand(int l, int r) {
if (l > 1 && can(l - 1, l, r)) return expand(l - 1, r);
if (r < N && can(r, l, r)) return expand(l, r + 1);
return mp(l, r);
}
main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
for (int i = 1; i < N; i++) cin >> C[i];
for (int i = 1, B; i <= N; i++) {
cin >> B;
for (int j = 0, tmp; j < B; j++) {
cin >> tmp;
rev[tmp].pb(i);
}
}
for (int i = 1; i <= N; i++) sort(rev[i].begin(), rev[i].end());
for (int i = N; i >= 1; i--)
if (i < N && can(i, i, i)) {
SR[i] = i;
while (SR[i] < N && can(SR[i], i, SR[i])) SR[i] = SR[SR[i] + 1];
} else SR[i] = i;
for (int i = 1; i <= N; i++) {
if (i > 1 && can(i - 1, i, SR[i])) {
L[i] = L[i - 1];
R[i] = max(R[i - 1], SR[i]);
} else {
L[i] = i;
R[i] = SR[i];
}
tie(L[i], R[i]) = expand(L[i], R[i]);
}
cin >> Q;
while (Q--) {
cin >> X >> Y;
cout << (L[X] <= Y && Y <= R[X] ? "YES" : "NO") << '\n';
}
}
Compilation message
long_mansion.cpp:43:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
43 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
12220 KB |
Output is correct |
2 |
Correct |
52 ms |
12232 KB |
Output is correct |
3 |
Correct |
164 ms |
12368 KB |
Output is correct |
4 |
Correct |
10 ms |
12108 KB |
Output is correct |
5 |
Correct |
11 ms |
12236 KB |
Output is correct |
6 |
Correct |
12 ms |
12224 KB |
Output is correct |
7 |
Correct |
11 ms |
12152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
12220 KB |
Output is correct |
2 |
Correct |
52 ms |
12232 KB |
Output is correct |
3 |
Correct |
164 ms |
12368 KB |
Output is correct |
4 |
Correct |
10 ms |
12108 KB |
Output is correct |
5 |
Correct |
11 ms |
12236 KB |
Output is correct |
6 |
Correct |
12 ms |
12224 KB |
Output is correct |
7 |
Correct |
11 ms |
12152 KB |
Output is correct |
8 |
Correct |
162 ms |
13728 KB |
Output is correct |
9 |
Correct |
158 ms |
13680 KB |
Output is correct |
10 |
Correct |
211 ms |
13904 KB |
Output is correct |
11 |
Correct |
303 ms |
14056 KB |
Output is correct |
12 |
Correct |
136 ms |
13800 KB |
Output is correct |
13 |
Correct |
134 ms |
13916 KB |
Output is correct |
14 |
Correct |
139 ms |
13816 KB |
Output is correct |
15 |
Correct |
143 ms |
13984 KB |
Output is correct |
16 |
Correct |
127 ms |
14212 KB |
Output is correct |
17 |
Correct |
134 ms |
13868 KB |
Output is correct |
18 |
Correct |
148 ms |
13848 KB |
Output is correct |
19 |
Correct |
138 ms |
13916 KB |
Output is correct |
20 |
Correct |
163 ms |
14048 KB |
Output is correct |
21 |
Correct |
140 ms |
14160 KB |
Output is correct |
22 |
Correct |
153 ms |
14064 KB |
Output is correct |
23 |
Correct |
156 ms |
13800 KB |
Output is correct |
24 |
Correct |
157 ms |
13764 KB |
Output is correct |
25 |
Correct |
161 ms |
13796 KB |
Output is correct |
26 |
Correct |
143 ms |
13676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3082 ms |
18496 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
12220 KB |
Output is correct |
2 |
Correct |
52 ms |
12232 KB |
Output is correct |
3 |
Correct |
164 ms |
12368 KB |
Output is correct |
4 |
Correct |
10 ms |
12108 KB |
Output is correct |
5 |
Correct |
11 ms |
12236 KB |
Output is correct |
6 |
Correct |
12 ms |
12224 KB |
Output is correct |
7 |
Correct |
11 ms |
12152 KB |
Output is correct |
8 |
Correct |
162 ms |
13728 KB |
Output is correct |
9 |
Correct |
158 ms |
13680 KB |
Output is correct |
10 |
Correct |
211 ms |
13904 KB |
Output is correct |
11 |
Correct |
303 ms |
14056 KB |
Output is correct |
12 |
Correct |
136 ms |
13800 KB |
Output is correct |
13 |
Correct |
134 ms |
13916 KB |
Output is correct |
14 |
Correct |
139 ms |
13816 KB |
Output is correct |
15 |
Correct |
143 ms |
13984 KB |
Output is correct |
16 |
Correct |
127 ms |
14212 KB |
Output is correct |
17 |
Correct |
134 ms |
13868 KB |
Output is correct |
18 |
Correct |
148 ms |
13848 KB |
Output is correct |
19 |
Correct |
138 ms |
13916 KB |
Output is correct |
20 |
Correct |
163 ms |
14048 KB |
Output is correct |
21 |
Correct |
140 ms |
14160 KB |
Output is correct |
22 |
Correct |
153 ms |
14064 KB |
Output is correct |
23 |
Correct |
156 ms |
13800 KB |
Output is correct |
24 |
Correct |
157 ms |
13764 KB |
Output is correct |
25 |
Correct |
161 ms |
13796 KB |
Output is correct |
26 |
Correct |
143 ms |
13676 KB |
Output is correct |
27 |
Execution timed out |
3082 ms |
18496 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |