#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
//#define int ll
const int N = 5e5 + 10;
const int INF = 1e9 + 10;
struct ST {
vector<int> mn, mx, a;
ST(int n, vector<int> _a) {
a = _a;
mn.resize(4 * n + 10);
mx.resize(4 * n + 10);
build(1, 1, n);
}
void build(int v, int l, int r) {
if (l == r) {
mn[v] = mx[v] = a[l];
return;
}
int mid = (l + r) / 2;
build(2 * v, l, mid);
build(2 * v + 1, mid + 1, r);
mn[v] = min(mn[2 * v], mn[2 * v + 1]);
mx[v] = max(mx[2 * v], mx[2 * v + 1]);
}
int get_max(int v, int l, int r, int a, int b) {
if (r < a || l > b) {
return 0;
}
if (l >= a && r <= b) {
return mx[v];
}
int mid = (l + r) / 2;
return max(get_max(2 * v, l, mid, a, b), get_max(2 * v + 1, mid + 1, r, a, b));
}
int get_min(int v, int l, int r, int a, int b) {
if (r < a ||l > b) {
return INF;
}
if (l >= a && r <= b) {
return mn[v];
}
int mid = (l + r) / 2;
return min(get_min(2 * v, l, mid, a, b), get_min(2 * v + 1, mid + 1, r, a, b));
}
};
int c[N];
vector<int> have[N];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i < n; i++) {
cin >> c[i];
}
for (int i = 1; i <= n; i++) {
int cnt;
cin >> cnt;
have[i].resize(cnt);
for (auto &it : have[i]) {
cin >> it;
}
}
map<int, int> prv;
vector<int> need_l(n + 1);
for (int i = 1; i < n; i++) {
for (auto it : have[i]) {
prv[it] = i;
}
need_l[i] = prv[c[i]];
}
prv = {};
vector<int> need_r(n + 1);
for (int i = n; i > 1; i--) {
for (auto it : have[i]) {
prv[it] = i;
}
need_r[i] = prv[c[i - 1]];
if (need_r[i] == 0) {
need_r[i] = n + 1;
}
}
ST nr(n, need_r), nl(n, need_l);
vector<int> mn(n + 1), mx(n + 1);
for (int i = 1; i < n; i++) {
if (need_l[i] == 0) {
mx[i] = 0;
continue;
}
int l = need_l[i], r = i + 1;
while (r - l > 1) {
int mid = (l + r) / 2;
if (nr.get_max(1, 1, n, need_l[i] + 1, mid) > i) {
r = mid;
}
else {
l = mid;
}
}
mx[i] = l;
}
for (int i = 2; i <= n; i++) {
if (need_r[i] == 0) {
mn[i] = n + 1;
continue;
}
int l = i - 1, r = need_r[i];
while (r - l > 1) {
int mid = (l + r) / 2;
if (nl.get_min(1, 1, n, mid, need_r[i] - 1) < i) {
l = mid;
}
else {
r = mid;
}
}
mn[i] = r;
}
ST MN(n, mn), MX(n, mx);
int q;
cin >> q;
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
if (l == r) {
cout << "YES\n";
continue;
}
if (l < r) {
bool ok = true;
for (int j = l; j < r; j++) {
if (l > mx[j]) {
ok = false;
break;
}
}
if (MX.get_min(1, 1, n, l, r - 1) < l) {
cout << "NO\n";
}
else {
cout << "YES\n";
}
}
else {
bool ok = true;
for (int j = l; j > r; j--) {
if (l < mn[j]) {
ok = false;
break;
}
}
if (l < MN.get_max(1, 1, n, r + 1, l)) {
cout << "NO\n";
}
else {
cout << "YES\n";
}
//cout << (ok ? "YES\n" : "NO\n");
}
}
/*int q;
cin >> q;
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
if (l == r) {
cout << "YES\n";
continue;
}
if (l < r) {
bool ok = true;
for (int j = l; j < r; j++) {
if (l > mx[j]) {
ok = false;
break;
}
}
cout << (ok ? "YES\n" : "NO\n");
}
else {
bool ok = true;
for (int j = l; j > r; j--) {
if (l < mn[j]) {
ok = false;
break;
}
}
cout << (ok ? "YES\n" : "NO\n");
}
}*/
}
Compilation message
long_mansion.cpp: In function 'int main()':
long_mansion.cpp:142:12: warning: variable 'ok' set but not used [-Wunused-but-set-variable]
bool ok = true;
^~
long_mansion.cpp:157:12: warning: variable 'ok' set but not used [-Wunused-but-set-variable]
bool ok = true;
^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
12536 KB |
Output is correct |
2 |
Correct |
16 ms |
12664 KB |
Output is correct |
3 |
Correct |
17 ms |
13052 KB |
Output is correct |
4 |
Correct |
16 ms |
12536 KB |
Output is correct |
5 |
Correct |
16 ms |
12536 KB |
Output is correct |
6 |
Correct |
17 ms |
12536 KB |
Output is correct |
7 |
Correct |
18 ms |
12536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
12536 KB |
Output is correct |
2 |
Correct |
16 ms |
12664 KB |
Output is correct |
3 |
Correct |
17 ms |
13052 KB |
Output is correct |
4 |
Correct |
16 ms |
12536 KB |
Output is correct |
5 |
Correct |
16 ms |
12536 KB |
Output is correct |
6 |
Correct |
17 ms |
12536 KB |
Output is correct |
7 |
Correct |
18 ms |
12536 KB |
Output is correct |
8 |
Correct |
247 ms |
14200 KB |
Output is correct |
9 |
Correct |
247 ms |
14072 KB |
Output is correct |
10 |
Correct |
251 ms |
14328 KB |
Output is correct |
11 |
Correct |
259 ms |
14844 KB |
Output is correct |
12 |
Correct |
231 ms |
14072 KB |
Output is correct |
13 |
Correct |
204 ms |
14328 KB |
Output is correct |
14 |
Correct |
201 ms |
14328 KB |
Output is correct |
15 |
Correct |
213 ms |
14328 KB |
Output is correct |
16 |
Correct |
198 ms |
14460 KB |
Output is correct |
17 |
Correct |
201 ms |
14200 KB |
Output is correct |
18 |
Correct |
212 ms |
14328 KB |
Output is correct |
19 |
Correct |
206 ms |
14328 KB |
Output is correct |
20 |
Correct |
205 ms |
14456 KB |
Output is correct |
21 |
Correct |
203 ms |
14456 KB |
Output is correct |
22 |
Correct |
217 ms |
14456 KB |
Output is correct |
23 |
Correct |
200 ms |
14200 KB |
Output is correct |
24 |
Correct |
193 ms |
14200 KB |
Output is correct |
25 |
Correct |
201 ms |
14200 KB |
Output is correct |
26 |
Correct |
194 ms |
14200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
507 ms |
33728 KB |
Output is correct |
2 |
Correct |
483 ms |
33520 KB |
Output is correct |
3 |
Correct |
450 ms |
35184 KB |
Output is correct |
4 |
Correct |
488 ms |
40884 KB |
Output is correct |
5 |
Correct |
482 ms |
40792 KB |
Output is correct |
6 |
Correct |
364 ms |
40048 KB |
Output is correct |
7 |
Correct |
373 ms |
39664 KB |
Output is correct |
8 |
Correct |
370 ms |
39792 KB |
Output is correct |
9 |
Correct |
360 ms |
39792 KB |
Output is correct |
10 |
Correct |
368 ms |
39844 KB |
Output is correct |
11 |
Correct |
357 ms |
39664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
12536 KB |
Output is correct |
2 |
Correct |
16 ms |
12664 KB |
Output is correct |
3 |
Correct |
17 ms |
13052 KB |
Output is correct |
4 |
Correct |
16 ms |
12536 KB |
Output is correct |
5 |
Correct |
16 ms |
12536 KB |
Output is correct |
6 |
Correct |
17 ms |
12536 KB |
Output is correct |
7 |
Correct |
18 ms |
12536 KB |
Output is correct |
8 |
Correct |
247 ms |
14200 KB |
Output is correct |
9 |
Correct |
247 ms |
14072 KB |
Output is correct |
10 |
Correct |
251 ms |
14328 KB |
Output is correct |
11 |
Correct |
259 ms |
14844 KB |
Output is correct |
12 |
Correct |
231 ms |
14072 KB |
Output is correct |
13 |
Correct |
204 ms |
14328 KB |
Output is correct |
14 |
Correct |
201 ms |
14328 KB |
Output is correct |
15 |
Correct |
213 ms |
14328 KB |
Output is correct |
16 |
Correct |
198 ms |
14460 KB |
Output is correct |
17 |
Correct |
201 ms |
14200 KB |
Output is correct |
18 |
Correct |
212 ms |
14328 KB |
Output is correct |
19 |
Correct |
206 ms |
14328 KB |
Output is correct |
20 |
Correct |
205 ms |
14456 KB |
Output is correct |
21 |
Correct |
203 ms |
14456 KB |
Output is correct |
22 |
Correct |
217 ms |
14456 KB |
Output is correct |
23 |
Correct |
200 ms |
14200 KB |
Output is correct |
24 |
Correct |
193 ms |
14200 KB |
Output is correct |
25 |
Correct |
201 ms |
14200 KB |
Output is correct |
26 |
Correct |
194 ms |
14200 KB |
Output is correct |
27 |
Correct |
507 ms |
33728 KB |
Output is correct |
28 |
Correct |
483 ms |
33520 KB |
Output is correct |
29 |
Correct |
450 ms |
35184 KB |
Output is correct |
30 |
Correct |
488 ms |
40884 KB |
Output is correct |
31 |
Correct |
482 ms |
40792 KB |
Output is correct |
32 |
Correct |
364 ms |
40048 KB |
Output is correct |
33 |
Correct |
373 ms |
39664 KB |
Output is correct |
34 |
Correct |
370 ms |
39792 KB |
Output is correct |
35 |
Correct |
360 ms |
39792 KB |
Output is correct |
36 |
Correct |
368 ms |
39844 KB |
Output is correct |
37 |
Correct |
357 ms |
39664 KB |
Output is correct |
38 |
Correct |
2842 ms |
100420 KB |
Output is correct |
39 |
Correct |
2497 ms |
119380 KB |
Output is correct |
40 |
Correct |
1766 ms |
80232 KB |
Output is correct |
41 |
Correct |
803 ms |
119408 KB |
Output is correct |
42 |
Correct |
495 ms |
42992 KB |
Output is correct |
43 |
Correct |
450 ms |
41840 KB |
Output is correct |
44 |
Correct |
655 ms |
64592 KB |
Output is correct |
45 |
Correct |
734 ms |
65412 KB |
Output is correct |
46 |
Correct |
729 ms |
65856 KB |
Output is correct |
47 |
Correct |
537 ms |
43000 KB |
Output is correct |
48 |
Correct |
447 ms |
41220 KB |
Output is correct |
49 |
Correct |
615 ms |
62312 KB |
Output is correct |
50 |
Correct |
663 ms |
63848 KB |
Output is correct |
51 |
Correct |
749 ms |
66432 KB |
Output is correct |
52 |
Correct |
1232 ms |
68588 KB |
Output is correct |
53 |
Correct |
1799 ms |
91748 KB |
Output is correct |
54 |
Correct |
2480 ms |
116808 KB |
Output is correct |
55 |
Correct |
1832 ms |
92644 KB |
Output is correct |