This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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], SL[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(SL[l - 1], r);
if (r < N && can(r, l, r)) return expand(l, SR[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, i)) {
SL[i] = i;
while (SL[i] > 1 && can(SL[i] - 1, SL[i], i)) SL[i] = SL[SL[i] - 1];
} else SL[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] = SL[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 (stderr)
long_mansion.cpp:43:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
43 | main() {
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |