/* cerberus97 - Hanit Banga */
#include <iostream>
#include <iomanip>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
#define pb push_back
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL)
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
const int N = 5e5 + 10, LOG = log2(N) + 5;
int c[N], lkey[N], rkey[N], lg2[N];
int lmost[N], lmost_table[N][LOG];
int rmost[N], rmost_table[N][LOG];
vector<int> pos[N];
void build_table(int *a, int table[N][LOG], int n, int f(int, int));
int query(int table[N][LOG], int l, int r, int f(int, int));
int my_min(int x, int y);
int my_max(int x, int y);
int main() {
fast_cin();
int n; cin >> n;
for (int i = 1; i <= n - 1; ++i) {
cin >> c[i];
}
for (int i = 1; i <= n; ++i) {
int b; cin >> b;
for (int j = 0; j < b; ++j) {
int k; cin >> k;
pos[k].pb(i);
}
}
for (int i = 1; i <= n; ++i) {
pos[i].pb(0);
pos[i].pb(n + 1);
sort(pos[i].begin(), pos[i].end());
}
for (int i = 1; i <= n - 1; ++i) {
auto it = lower_bound(pos[c[i]].begin(), pos[c[i]].end(), i + 1);
rkey[i] = *it;
lkey[i] = *(it - 1);
}
rkey[0] = n + 1;
lkey[n] = 0;
set<pii> s;
for (int i = 1; i <= n - 1; ++i) {
s.insert({i - 1, rkey[i - 1]});
int p = lkey[i];
lmost[i] = i;
while (true) {
auto it = s.lower_bound({p, 0});
if (it == s.end()) {
break;
} else if (it->second < i + 1) {
s.erase(it);
} else {
lmost[i] = it->first;
break;
}
}
}
s.clear();
for (int i = n - 1; i >= 1; --i) {
s.insert({i + 1, lkey[i + 1]});
int p = rkey[i];
rmost[i] = i;
while (true) {
auto it = s.lower_bound({p, 0});
if (it == s.begin()) {
break;
}
--it;
if (it->second > i) {
s.erase(it);
} else {
rmost[i] = it->first;
break;
}
}
}
for (int i = 1; i <= n; ++i) {
lg2[i] = lg2[i - 1];
while (1 << (lg2[i] + 1) <= i) {
++lg2[i];
}
}
build_table(lmost, lmost_table, n - 1, my_min);
build_table(rmost, rmost_table, n - 1, my_max);
int q; cin >> q;
while (q--) {
int x, y;
cin >> x >> y;
if (x < y) {
if (query(lmost_table, x, y - 1, my_min) < x) {
cout << "NO\n";
} else {
cout << "YES\n";
}
} else {
if (query(rmost_table, y, x - 1, my_max) >= x) {
cout << "NO\n";
} else {
cout << "YES\n";
}
}
}
}
void build_table(int *a, int table[N][LOG], int n, int f(int, int)) {
for (int i = n; i >= 1; --i) {
table[i][0] = a[i];
for (int j = 1; i + (1 << j) - 1 <= n; ++j) {
table[i][j] = f(table[i][j - 1], table[i + (1 << (j - 1))][j - 1]);
}
}
}
int query(int table[N][LOG], int l, int r, int f(int, int)) {
int j = lg2[r - l + 1];
return f(table[l][j], table[r - (1 << j) + 1][j]);
}
int my_min(int x, int y) {
return min(x, y);
}
int my_max(int x, int y) {
return max(x, y);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
12672 KB |
Output is correct |
2 |
Correct |
16 ms |
13056 KB |
Output is correct |
3 |
Correct |
17 ms |
13568 KB |
Output is correct |
4 |
Correct |
15 ms |
12800 KB |
Output is correct |
5 |
Correct |
15 ms |
12800 KB |
Output is correct |
6 |
Correct |
16 ms |
12800 KB |
Output is correct |
7 |
Correct |
15 ms |
12672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
12672 KB |
Output is correct |
2 |
Correct |
16 ms |
13056 KB |
Output is correct |
3 |
Correct |
17 ms |
13568 KB |
Output is correct |
4 |
Correct |
15 ms |
12800 KB |
Output is correct |
5 |
Correct |
15 ms |
12800 KB |
Output is correct |
6 |
Correct |
16 ms |
12800 KB |
Output is correct |
7 |
Correct |
15 ms |
12672 KB |
Output is correct |
8 |
Correct |
118 ms |
14376 KB |
Output is correct |
9 |
Correct |
117 ms |
14328 KB |
Output is correct |
10 |
Correct |
125 ms |
14716 KB |
Output is correct |
11 |
Correct |
139 ms |
15352 KB |
Output is correct |
12 |
Correct |
108 ms |
14268 KB |
Output is correct |
13 |
Correct |
116 ms |
14648 KB |
Output is correct |
14 |
Correct |
117 ms |
14584 KB |
Output is correct |
15 |
Correct |
112 ms |
14584 KB |
Output is correct |
16 |
Correct |
107 ms |
14812 KB |
Output is correct |
17 |
Correct |
113 ms |
14456 KB |
Output is correct |
18 |
Correct |
115 ms |
14496 KB |
Output is correct |
19 |
Correct |
113 ms |
14556 KB |
Output is correct |
20 |
Correct |
108 ms |
14764 KB |
Output is correct |
21 |
Correct |
106 ms |
14944 KB |
Output is correct |
22 |
Correct |
115 ms |
14584 KB |
Output is correct |
23 |
Correct |
110 ms |
14456 KB |
Output is correct |
24 |
Correct |
112 ms |
14456 KB |
Output is correct |
25 |
Correct |
111 ms |
14480 KB |
Output is correct |
26 |
Correct |
111 ms |
14380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
332 ms |
41416 KB |
Output is correct |
2 |
Correct |
453 ms |
41336 KB |
Output is correct |
3 |
Correct |
334 ms |
43828 KB |
Output is correct |
4 |
Correct |
331 ms |
48516 KB |
Output is correct |
5 |
Correct |
336 ms |
48708 KB |
Output is correct |
6 |
Correct |
292 ms |
46524 KB |
Output is correct |
7 |
Correct |
290 ms |
47544 KB |
Output is correct |
8 |
Correct |
291 ms |
48196 KB |
Output is correct |
9 |
Correct |
293 ms |
48096 KB |
Output is correct |
10 |
Correct |
289 ms |
48760 KB |
Output is correct |
11 |
Correct |
295 ms |
48872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
12672 KB |
Output is correct |
2 |
Correct |
16 ms |
13056 KB |
Output is correct |
3 |
Correct |
17 ms |
13568 KB |
Output is correct |
4 |
Correct |
15 ms |
12800 KB |
Output is correct |
5 |
Correct |
15 ms |
12800 KB |
Output is correct |
6 |
Correct |
16 ms |
12800 KB |
Output is correct |
7 |
Correct |
15 ms |
12672 KB |
Output is correct |
8 |
Correct |
118 ms |
14376 KB |
Output is correct |
9 |
Correct |
117 ms |
14328 KB |
Output is correct |
10 |
Correct |
125 ms |
14716 KB |
Output is correct |
11 |
Correct |
139 ms |
15352 KB |
Output is correct |
12 |
Correct |
108 ms |
14268 KB |
Output is correct |
13 |
Correct |
116 ms |
14648 KB |
Output is correct |
14 |
Correct |
117 ms |
14584 KB |
Output is correct |
15 |
Correct |
112 ms |
14584 KB |
Output is correct |
16 |
Correct |
107 ms |
14812 KB |
Output is correct |
17 |
Correct |
113 ms |
14456 KB |
Output is correct |
18 |
Correct |
115 ms |
14496 KB |
Output is correct |
19 |
Correct |
113 ms |
14556 KB |
Output is correct |
20 |
Correct |
108 ms |
14764 KB |
Output is correct |
21 |
Correct |
106 ms |
14944 KB |
Output is correct |
22 |
Correct |
115 ms |
14584 KB |
Output is correct |
23 |
Correct |
110 ms |
14456 KB |
Output is correct |
24 |
Correct |
112 ms |
14456 KB |
Output is correct |
25 |
Correct |
111 ms |
14480 KB |
Output is correct |
26 |
Correct |
111 ms |
14380 KB |
Output is correct |
27 |
Correct |
332 ms |
41416 KB |
Output is correct |
28 |
Correct |
453 ms |
41336 KB |
Output is correct |
29 |
Correct |
334 ms |
43828 KB |
Output is correct |
30 |
Correct |
331 ms |
48516 KB |
Output is correct |
31 |
Correct |
336 ms |
48708 KB |
Output is correct |
32 |
Correct |
292 ms |
46524 KB |
Output is correct |
33 |
Correct |
290 ms |
47544 KB |
Output is correct |
34 |
Correct |
291 ms |
48196 KB |
Output is correct |
35 |
Correct |
293 ms |
48096 KB |
Output is correct |
36 |
Correct |
289 ms |
48760 KB |
Output is correct |
37 |
Correct |
295 ms |
48872 KB |
Output is correct |
38 |
Correct |
940 ms |
123768 KB |
Output is correct |
39 |
Correct |
879 ms |
147816 KB |
Output is correct |
40 |
Correct |
675 ms |
98552 KB |
Output is correct |
41 |
Correct |
1028 ms |
161380 KB |
Output is correct |
42 |
Correct |
304 ms |
48368 KB |
Output is correct |
43 |
Correct |
283 ms |
47452 KB |
Output is correct |
44 |
Correct |
466 ms |
77528 KB |
Output is correct |
45 |
Correct |
457 ms |
77872 KB |
Output is correct |
46 |
Correct |
466 ms |
78200 KB |
Output is correct |
47 |
Correct |
291 ms |
49528 KB |
Output is correct |
48 |
Correct |
275 ms |
46968 KB |
Output is correct |
49 |
Correct |
428 ms |
76536 KB |
Output is correct |
50 |
Correct |
442 ms |
78840 KB |
Output is correct |
51 |
Correct |
506 ms |
79944 KB |
Output is correct |
52 |
Correct |
461 ms |
75388 KB |
Output is correct |
53 |
Correct |
598 ms |
103668 KB |
Output is correct |
54 |
Correct |
793 ms |
130172 KB |
Output is correct |
55 |
Correct |
588 ms |
103116 KB |
Output is correct |