#include <iostream>
#include <vector>
#include <array>
#include <map>
#define ll long long
using namespace std;
vector <ll> V[500000], X, Y;
map <ll, ll> mp;
ll n, k, a, b, q, C[500000], L[500000], R[500000], P[500000];
vector <ll> MX[500000], MN[500000];
struct SegTree{
vector <ll> st{vector <ll>(2000000, 1e18)};
vector <ll> st2{vector <ll>(2000000, -1e18)};
void push(ll id) {
st[id*2] = min(st[id], st[id*2]);
st[id*2+1] = min(st[id], st[id*2+1]);
st2[id*2] = max(st2[id], st2[id*2]);
st2[id*2+1] = max(st2[id], st2[id*2+1]);
}
void updateMN(ll id, ll l, ll r, ll ql, ll qr, ll w) {
if (qr < l || r < ql) return;
else if (ql <= l && r <= qr) {
st[id] = min(st[id], w);
return;
}
ll mid = (l+r)/2;
updateMN(id*2, l, mid, ql, qr, w);
updateMN(id*2+1, mid+1, r, ql, qr, w);
}
void updateMX(ll id, ll l, ll r, ll ql, ll qr, ll w) {
if (qr < l || r < ql) return;
else if (ql <= l && r <= qr) {
st2[id] = max(st2[id], w);
return;
}
ll mid = (l+r)/2;
updateMX(id*2, l, mid, ql, qr, w);
updateMX(id*2+1, mid+1, r, ql, qr, w);
}
array<ll, 2> query(ll id, ll l, ll r, ll q) {
if (l == r) return {st[id], st2[id]};
push(id);
ll mid = (l+r)/2;
if (q <= mid) return query(id*2, l, mid, q);
else return query(id*2+1, mid+1, r, q);
}
} ST;
int main() {
cin >> n;
for (int i=0; i<n-1; ++i) {
cin >> C[i];
--C[i];
}
for (int i=0; i<n; ++i) {
P[i] = -1e18;
cin >> k;
while (k--) {
cin >> a;
--a;
V[i].push_back(a);
}
}
for (int i=0; i<n-1; ++i) {
for (auto u : V[i]) {
P[u] = i;
}
if (P[C[i]] != -1e18) MX[P[C[i]]].push_back(i);
else X.push_back(i);
L[i] = P[C[i]];
}
for (int i=0; i<n; ++i) P[i] = 1e18;
for (int i=n-1; i>=0; --i) {
if (i != n-1) {
R[i] = P[C[i]];
if (P[C[i]] != 1e18) MN[P[C[i]]].push_back(i);
else Y.push_back(i);
}
for (auto u : V[i]) {
P[u] = i;
}
}
for (auto u : X) {
++mp[u];
}
auto it = mp.lower_bound(0);
if (it != mp.end()) {
ST.updateMX(1, 0, n-1, 0, it->first, 0);
}
for (ll i=0; i<n-1; ++i) {
for (auto u : MX[i]) {
++mp[u];
}
if (R[i] == 1e18) {
ST.updateMX(1, 0, n-1, i+1, n-1, i+1);
continue;
}
auto it = mp.lower_bound(R[i]);
if (it != mp.begin()) {
it = prev(it);
if (i+1 <= it->first) ST.updateMX(1, 0, n-1, i+1, it->first, i+1);
}
}
mp.clear();
for (auto u : Y) {
++mp[u];
}
it = mp.end();
if (it != mp.begin()) {
it = prev(it);
ST.updateMN(1, 0, n-1, it->first+1, n-1, n-1);
}
for (ll i=n-1; i>=0; --i) {
if (i != n-1) {
if (L[i] == -1e18) {
ST.updateMN(1, 0, n-1, 0, i, i);
}
else {
auto it = mp.lower_bound(L[i]);
if (it != mp.end()) {
if (it->first+1 <= i) ST.updateMN(1, 0, n-1, it->first+1, i, i);
}
}
}
for (auto u : MN[i]) {
++mp[u];
}
}
cin >> q;
while (q--) {
cin >> a >> b;
--a, --b;
auto u = ST.query(1, 0, n-1, a);
if (a <= b) {
if (b <= u[0]) cout << "YES\n";
else cout << "NO\n";
}
else {
if (u[1] <= b) cout << "YES\n";
else cout << "NO\n";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
76624 KB |
Output is correct |
2 |
Correct |
29 ms |
76888 KB |
Output is correct |
3 |
Correct |
30 ms |
77184 KB |
Output is correct |
4 |
Correct |
25 ms |
76632 KB |
Output is correct |
5 |
Correct |
26 ms |
76636 KB |
Output is correct |
6 |
Correct |
26 ms |
76736 KB |
Output is correct |
7 |
Correct |
25 ms |
76636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
76624 KB |
Output is correct |
2 |
Correct |
29 ms |
76888 KB |
Output is correct |
3 |
Correct |
30 ms |
77184 KB |
Output is correct |
4 |
Correct |
25 ms |
76632 KB |
Output is correct |
5 |
Correct |
26 ms |
76636 KB |
Output is correct |
6 |
Correct |
26 ms |
76736 KB |
Output is correct |
7 |
Correct |
25 ms |
76636 KB |
Output is correct |
8 |
Correct |
769 ms |
78772 KB |
Output is correct |
9 |
Correct |
800 ms |
78436 KB |
Output is correct |
10 |
Correct |
816 ms |
78460 KB |
Output is correct |
11 |
Correct |
866 ms |
78772 KB |
Output is correct |
12 |
Correct |
789 ms |
78344 KB |
Output is correct |
13 |
Correct |
772 ms |
78444 KB |
Output is correct |
14 |
Correct |
770 ms |
78416 KB |
Output is correct |
15 |
Correct |
826 ms |
78496 KB |
Output is correct |
16 |
Correct |
761 ms |
78572 KB |
Output is correct |
17 |
Correct |
786 ms |
78436 KB |
Output is correct |
18 |
Correct |
762 ms |
78604 KB |
Output is correct |
19 |
Correct |
761 ms |
83164 KB |
Output is correct |
20 |
Correct |
813 ms |
82876 KB |
Output is correct |
21 |
Correct |
763 ms |
82776 KB |
Output is correct |
22 |
Correct |
761 ms |
82776 KB |
Output is correct |
23 |
Correct |
764 ms |
82512 KB |
Output is correct |
24 |
Correct |
828 ms |
82648 KB |
Output is correct |
25 |
Correct |
779 ms |
82652 KB |
Output is correct |
26 |
Correct |
781 ms |
82812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1018 ms |
94360 KB |
Output is correct |
2 |
Correct |
979 ms |
94596 KB |
Output is correct |
3 |
Correct |
986 ms |
94376 KB |
Output is correct |
4 |
Correct |
988 ms |
94256 KB |
Output is correct |
5 |
Correct |
979 ms |
94480 KB |
Output is correct |
6 |
Correct |
960 ms |
93384 KB |
Output is correct |
7 |
Correct |
904 ms |
93780 KB |
Output is correct |
8 |
Correct |
898 ms |
94008 KB |
Output is correct |
9 |
Correct |
926 ms |
93948 KB |
Output is correct |
10 |
Correct |
904 ms |
94184 KB |
Output is correct |
11 |
Correct |
910 ms |
93980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
76624 KB |
Output is correct |
2 |
Correct |
29 ms |
76888 KB |
Output is correct |
3 |
Correct |
30 ms |
77184 KB |
Output is correct |
4 |
Correct |
25 ms |
76632 KB |
Output is correct |
5 |
Correct |
26 ms |
76636 KB |
Output is correct |
6 |
Correct |
26 ms |
76736 KB |
Output is correct |
7 |
Correct |
25 ms |
76636 KB |
Output is correct |
8 |
Correct |
769 ms |
78772 KB |
Output is correct |
9 |
Correct |
800 ms |
78436 KB |
Output is correct |
10 |
Correct |
816 ms |
78460 KB |
Output is correct |
11 |
Correct |
866 ms |
78772 KB |
Output is correct |
12 |
Correct |
789 ms |
78344 KB |
Output is correct |
13 |
Correct |
772 ms |
78444 KB |
Output is correct |
14 |
Correct |
770 ms |
78416 KB |
Output is correct |
15 |
Correct |
826 ms |
78496 KB |
Output is correct |
16 |
Correct |
761 ms |
78572 KB |
Output is correct |
17 |
Correct |
786 ms |
78436 KB |
Output is correct |
18 |
Correct |
762 ms |
78604 KB |
Output is correct |
19 |
Correct |
761 ms |
83164 KB |
Output is correct |
20 |
Correct |
813 ms |
82876 KB |
Output is correct |
21 |
Correct |
763 ms |
82776 KB |
Output is correct |
22 |
Correct |
761 ms |
82776 KB |
Output is correct |
23 |
Correct |
764 ms |
82512 KB |
Output is correct |
24 |
Correct |
828 ms |
82648 KB |
Output is correct |
25 |
Correct |
779 ms |
82652 KB |
Output is correct |
26 |
Correct |
781 ms |
82812 KB |
Output is correct |
27 |
Correct |
1018 ms |
94360 KB |
Output is correct |
28 |
Correct |
979 ms |
94596 KB |
Output is correct |
29 |
Correct |
986 ms |
94376 KB |
Output is correct |
30 |
Correct |
988 ms |
94256 KB |
Output is correct |
31 |
Correct |
979 ms |
94480 KB |
Output is correct |
32 |
Correct |
960 ms |
93384 KB |
Output is correct |
33 |
Correct |
904 ms |
93780 KB |
Output is correct |
34 |
Correct |
898 ms |
94008 KB |
Output is correct |
35 |
Correct |
926 ms |
93948 KB |
Output is correct |
36 |
Correct |
904 ms |
94184 KB |
Output is correct |
37 |
Correct |
910 ms |
93980 KB |
Output is correct |
38 |
Correct |
1774 ms |
148400 KB |
Output is correct |
39 |
Correct |
1806 ms |
161424 KB |
Output is correct |
40 |
Correct |
1409 ms |
134720 KB |
Output is correct |
41 |
Correct |
1358 ms |
163072 KB |
Output is correct |
42 |
Correct |
954 ms |
100432 KB |
Output is correct |
43 |
Correct |
990 ms |
100812 KB |
Output is correct |
44 |
Correct |
1093 ms |
123088 KB |
Output is correct |
45 |
Correct |
1111 ms |
123644 KB |
Output is correct |
46 |
Correct |
1084 ms |
123808 KB |
Output is correct |
47 |
Correct |
925 ms |
101292 KB |
Output is correct |
48 |
Correct |
922 ms |
100916 KB |
Output is correct |
49 |
Correct |
1082 ms |
123552 KB |
Output is correct |
50 |
Correct |
1116 ms |
123528 KB |
Output is correct |
51 |
Correct |
1086 ms |
124032 KB |
Output is correct |
52 |
Correct |
1134 ms |
118716 KB |
Output is correct |
53 |
Correct |
1252 ms |
136648 KB |
Output is correct |
54 |
Correct |
1417 ms |
150640 KB |
Output is correct |
55 |
Correct |
1259 ms |
135628 KB |
Output is correct |