#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr ll INFLL = numeric_limits<ll>::max() / 2;
template<class T>
istream &operator>>(istream &is, vector<T> &a) {
for (auto &i : a) {
is >> i;
}
return is;
}
#ifdef __APPLE__
#include "../../debug.h"
#else
#define debug(...) 42
#endif
struct SegTree { // min, max, first >
struct Node {
int mn, mx;
Node() {}
Node(int x) {
mn = x;
mx = x;
}
};
Node pull(const Node &a, const Node &b) {
Node ans;
ans.mn = min(a.mn, b.mn);
ans.mx = max(a.mx, b.mx);
return ans;
}
vector<Node> tree;
int n;
void build(int v, int l, int r, const vector<int> &a) {
if (l + 1 == r) {
tree[v] = a[l];
return;
}
int m = (l + r) / 2;
build(2 * v + 1, l, m, a);
build(2 * v + 2, m, r, a);
tree[v] = pull(tree[2 * v + 1], tree[2 * v + 2]);
}
SegTree(vector<int> a) {
n = a.size();
tree.resize(4 * n);
build(0, 0, n, a);
}
int get_first_bol(int v, int l, int r, int li, int ri, int x) {
if (li >= r || l >= ri || tree[v].mx <= x) {
return n;
}
if (l + 1 == r) {
return l;
}
int m = (l + r) / 2;
int la = get_first_bol(2 * v + 1, l, m, li, ri, x);
if (la != n) {
return la;
}
return get_first_bol(2 * v + 2, m, r, li, ri, x);
}
int get_first_bol(int li, int ri, int x) {
return get_first_bol(0, 0, n, li, ri, x);
}
int get_min(int v, int l, int r, int li, int ri) {
if (li >= r || l >= ri) {
return INF;
}
if (li <= l && r <= ri) {
return tree[v].mn;
}
int m = (l + r) / 2;
return min(get_min(2 * v + 1, l, m, li, ri), get_min(2 * v + 2, m, r, li, ri));
}
int get_min(int li, int ri) {
return get_min(0, 0, n, li, ri);
}
};
int main() {
cin.tie(0)->sync_with_stdio(0);
#ifdef __APPLE__
freopen("input.txt", "r", stdin);
#endif
int n;
cin >> n;
vector<int> c(n - 1);
cin >> c;
for (int &i : c) {
--i;
}
vector<vector<int>> a(n);
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
a[i].resize(x);
cin >> a[i];
for (int &j : a[i]) {
--j;
}
}
int q;
cin >> q;
vector<array<int, 3>> lr, rl;
for (int i = 0; i < q; ++i) {
int l, r;
cin >> l >> r;
--l;
--r;
if (l <= r) {
lr.push_back({l, r, i});
} else {
rl.push_back({n - l - 1, n - r - 1, i});
}
}
vector<bool> ans(q, 1);
auto solve = [&](vector<array<int, 3>> qq) {
vector<vector<int>> ind(n);
for (int i = 0; i < n; ++i) {
for (int j : a[i]) {
ind[j].push_back(i);
}
}
vector<int> prv(n, -1), nxt(n, n); // prv[i] of c[i] and nxt[i] of c[i]
for (int i = 0; i < n - 1; ++i) {
const auto &v = ind[c[i]];
auto it = upper_bound(v.begin(), v.end(), i) - v.begin();
if (it != 0) {
prv[i] = v[it - 1];
}
}
for (int i = 0; i < n - 1; ++i) {
const auto &v = ind[c[i]];
auto it = upper_bound(v.begin(), v.end(), i) - v.begin();
if (it != (int) v.size()) {
nxt[i] = v[it];
}
}
SegTree do1(nxt);
vector<int> kek(n, n); // dlya kazdogo x first j na otr prv[x], chto nxt[j] > x
for (int x = 0; x < n; ++x) {
if (prv[x] == -1) {
kek[x] = -1;
continue;
}
kek[x] = do1.get_first_bol(prv[x], n, x);
// for (int j = prv[x]; j < n; ++j) {
// if (nxt[j] > x) {
// kek[x] = j;
// break;
// }
// }
}
SegTree do2(kek);
for (auto [l, r, i] : qq) {
debug(l + 1, r + 1, i);
if (do2.get_min(l, r) < l) {
ans[i] = 0;
}
// for (int x = l; x < r; ++x) {
// if (kek[x] < l) {
// ans[i] = 0;
// break;
// }
// }
}
};
solve(lr);
reverse(c.begin(), c.end());
reverse(a.begin(), a.end());
solve(rl);
for (auto i : ans) {
if (i) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
}
Compilation message
long_mansion.cpp: In lambda function:
long_mansion.cpp:19:20: warning: statement has no effect [-Wunused-value]
19 | #define debug(...) 42
| ^~
long_mansion.cpp:171:13: note: in expansion of macro 'debug'
171 | debug(l + 1, r + 1, i);
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
756 KB |
Output is correct |
2 |
Correct |
5 ms |
948 KB |
Output is correct |
3 |
Correct |
7 ms |
1236 KB |
Output is correct |
4 |
Correct |
4 ms |
712 KB |
Output is correct |
5 |
Correct |
5 ms |
816 KB |
Output is correct |
6 |
Correct |
4 ms |
716 KB |
Output is correct |
7 |
Correct |
4 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
756 KB |
Output is correct |
2 |
Correct |
5 ms |
948 KB |
Output is correct |
3 |
Correct |
7 ms |
1236 KB |
Output is correct |
4 |
Correct |
4 ms |
712 KB |
Output is correct |
5 |
Correct |
5 ms |
816 KB |
Output is correct |
6 |
Correct |
4 ms |
716 KB |
Output is correct |
7 |
Correct |
4 ms |
724 KB |
Output is correct |
8 |
Correct |
171 ms |
15416 KB |
Output is correct |
9 |
Correct |
167 ms |
15368 KB |
Output is correct |
10 |
Correct |
177 ms |
15904 KB |
Output is correct |
11 |
Correct |
192 ms |
16472 KB |
Output is correct |
12 |
Correct |
154 ms |
14856 KB |
Output is correct |
13 |
Correct |
135 ms |
13892 KB |
Output is correct |
14 |
Correct |
138 ms |
15616 KB |
Output is correct |
15 |
Correct |
137 ms |
15524 KB |
Output is correct |
16 |
Correct |
137 ms |
15352 KB |
Output is correct |
17 |
Correct |
136 ms |
13888 KB |
Output is correct |
18 |
Correct |
137 ms |
15560 KB |
Output is correct |
19 |
Correct |
142 ms |
13908 KB |
Output is correct |
20 |
Correct |
141 ms |
13728 KB |
Output is correct |
21 |
Correct |
138 ms |
15396 KB |
Output is correct |
22 |
Correct |
144 ms |
16720 KB |
Output is correct |
23 |
Correct |
134 ms |
15284 KB |
Output is correct |
24 |
Correct |
134 ms |
15316 KB |
Output is correct |
25 |
Correct |
133 ms |
15348 KB |
Output is correct |
26 |
Correct |
157 ms |
15428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
320 ms |
33052 KB |
Output is correct |
2 |
Correct |
319 ms |
32524 KB |
Output is correct |
3 |
Correct |
318 ms |
32752 KB |
Output is correct |
4 |
Correct |
314 ms |
32932 KB |
Output is correct |
5 |
Correct |
321 ms |
33032 KB |
Output is correct |
6 |
Correct |
258 ms |
30764 KB |
Output is correct |
7 |
Correct |
275 ms |
30520 KB |
Output is correct |
8 |
Correct |
262 ms |
30336 KB |
Output is correct |
9 |
Correct |
267 ms |
30448 KB |
Output is correct |
10 |
Correct |
261 ms |
30324 KB |
Output is correct |
11 |
Correct |
262 ms |
30344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
756 KB |
Output is correct |
2 |
Correct |
5 ms |
948 KB |
Output is correct |
3 |
Correct |
7 ms |
1236 KB |
Output is correct |
4 |
Correct |
4 ms |
712 KB |
Output is correct |
5 |
Correct |
5 ms |
816 KB |
Output is correct |
6 |
Correct |
4 ms |
716 KB |
Output is correct |
7 |
Correct |
4 ms |
724 KB |
Output is correct |
8 |
Correct |
171 ms |
15416 KB |
Output is correct |
9 |
Correct |
167 ms |
15368 KB |
Output is correct |
10 |
Correct |
177 ms |
15904 KB |
Output is correct |
11 |
Correct |
192 ms |
16472 KB |
Output is correct |
12 |
Correct |
154 ms |
14856 KB |
Output is correct |
13 |
Correct |
135 ms |
13892 KB |
Output is correct |
14 |
Correct |
138 ms |
15616 KB |
Output is correct |
15 |
Correct |
137 ms |
15524 KB |
Output is correct |
16 |
Correct |
137 ms |
15352 KB |
Output is correct |
17 |
Correct |
136 ms |
13888 KB |
Output is correct |
18 |
Correct |
137 ms |
15560 KB |
Output is correct |
19 |
Correct |
142 ms |
13908 KB |
Output is correct |
20 |
Correct |
141 ms |
13728 KB |
Output is correct |
21 |
Correct |
138 ms |
15396 KB |
Output is correct |
22 |
Correct |
144 ms |
16720 KB |
Output is correct |
23 |
Correct |
134 ms |
15284 KB |
Output is correct |
24 |
Correct |
134 ms |
15316 KB |
Output is correct |
25 |
Correct |
133 ms |
15348 KB |
Output is correct |
26 |
Correct |
157 ms |
15428 KB |
Output is correct |
27 |
Correct |
320 ms |
33052 KB |
Output is correct |
28 |
Correct |
319 ms |
32524 KB |
Output is correct |
29 |
Correct |
318 ms |
32752 KB |
Output is correct |
30 |
Correct |
314 ms |
32932 KB |
Output is correct |
31 |
Correct |
321 ms |
33032 KB |
Output is correct |
32 |
Correct |
258 ms |
30764 KB |
Output is correct |
33 |
Correct |
275 ms |
30520 KB |
Output is correct |
34 |
Correct |
262 ms |
30336 KB |
Output is correct |
35 |
Correct |
267 ms |
30448 KB |
Output is correct |
36 |
Correct |
261 ms |
30324 KB |
Output is correct |
37 |
Correct |
262 ms |
30344 KB |
Output is correct |
38 |
Correct |
603 ms |
79512 KB |
Output is correct |
39 |
Correct |
652 ms |
89004 KB |
Output is correct |
40 |
Correct |
526 ms |
68784 KB |
Output is correct |
41 |
Correct |
631 ms |
87108 KB |
Output is correct |
42 |
Correct |
246 ms |
32180 KB |
Output is correct |
43 |
Correct |
261 ms |
32208 KB |
Output is correct |
44 |
Correct |
379 ms |
50484 KB |
Output is correct |
45 |
Correct |
380 ms |
50904 KB |
Output is correct |
46 |
Correct |
418 ms |
51208 KB |
Output is correct |
47 |
Correct |
280 ms |
32600 KB |
Output is correct |
48 |
Correct |
274 ms |
31836 KB |
Output is correct |
49 |
Correct |
391 ms |
50332 KB |
Output is correct |
50 |
Correct |
424 ms |
50632 KB |
Output is correct |
51 |
Correct |
425 ms |
51764 KB |
Output is correct |
52 |
Correct |
306 ms |
52124 KB |
Output is correct |
53 |
Correct |
406 ms |
74708 KB |
Output is correct |
54 |
Correct |
468 ms |
89524 KB |
Output is correct |
55 |
Correct |
392 ms |
74924 KB |
Output is correct |