#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;
int need_l[N], need_r[N];
int get_max_r(int l, int r) {
int res = 0;
for (int i = l; i <= r; i++) {
res = max(res, need_r[i]);
}
return res;
}
int get_min_l(int l, int r) {
int res = INF;
for (int i = l; i <= r; i++) {
res = min(res, need_l[i]);
}
return res;
}
int c[N], mx[N], mn[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;
for (int i = 1; i < n; i++) {
for (auto it : have[i]) {
prv[it] = i;
}
need_l[i] = prv[c[i]];
}
prv = {};
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;
}
}
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 (get_max_r(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 (get_min_l(mid, need_r[i] - 1) < i) {
l = mid;
}
else {
r = mid;
}
}
mn[i] = r;
}
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");
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
12280 KB |
Output is correct |
2 |
Correct |
16 ms |
12408 KB |
Output is correct |
3 |
Correct |
18 ms |
12408 KB |
Output is correct |
4 |
Correct |
16 ms |
12408 KB |
Output is correct |
5 |
Correct |
19 ms |
12280 KB |
Output is correct |
6 |
Correct |
16 ms |
12408 KB |
Output is correct |
7 |
Correct |
31 ms |
12408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
12280 KB |
Output is correct |
2 |
Correct |
16 ms |
12408 KB |
Output is correct |
3 |
Correct |
18 ms |
12408 KB |
Output is correct |
4 |
Correct |
16 ms |
12408 KB |
Output is correct |
5 |
Correct |
19 ms |
12280 KB |
Output is correct |
6 |
Correct |
16 ms |
12408 KB |
Output is correct |
7 |
Correct |
31 ms |
12408 KB |
Output is correct |
8 |
Correct |
154 ms |
18168 KB |
Output is correct |
9 |
Correct |
136 ms |
18040 KB |
Output is correct |
10 |
Correct |
245 ms |
18580 KB |
Output is correct |
11 |
Correct |
439 ms |
18936 KB |
Output is correct |
12 |
Correct |
177 ms |
17784 KB |
Output is correct |
13 |
Correct |
142 ms |
18424 KB |
Output is correct |
14 |
Correct |
148 ms |
18424 KB |
Output is correct |
15 |
Correct |
242 ms |
18552 KB |
Output is correct |
16 |
Correct |
451 ms |
18216 KB |
Output is correct |
17 |
Correct |
131 ms |
18296 KB |
Output is correct |
18 |
Correct |
148 ms |
18424 KB |
Output is correct |
19 |
Correct |
166 ms |
18424 KB |
Output is correct |
20 |
Correct |
330 ms |
18424 KB |
Output is correct |
21 |
Correct |
446 ms |
18168 KB |
Output is correct |
22 |
Correct |
274 ms |
18296 KB |
Output is correct |
23 |
Correct |
200 ms |
18296 KB |
Output is correct |
24 |
Correct |
203 ms |
18168 KB |
Output is correct |
25 |
Correct |
204 ms |
18552 KB |
Output is correct |
26 |
Correct |
165 ms |
18168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1166 ms |
26356 KB |
Output is correct |
2 |
Correct |
2264 ms |
26240 KB |
Output is correct |
3 |
Execution timed out |
3049 ms |
23928 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
12280 KB |
Output is correct |
2 |
Correct |
16 ms |
12408 KB |
Output is correct |
3 |
Correct |
18 ms |
12408 KB |
Output is correct |
4 |
Correct |
16 ms |
12408 KB |
Output is correct |
5 |
Correct |
19 ms |
12280 KB |
Output is correct |
6 |
Correct |
16 ms |
12408 KB |
Output is correct |
7 |
Correct |
31 ms |
12408 KB |
Output is correct |
8 |
Correct |
154 ms |
18168 KB |
Output is correct |
9 |
Correct |
136 ms |
18040 KB |
Output is correct |
10 |
Correct |
245 ms |
18580 KB |
Output is correct |
11 |
Correct |
439 ms |
18936 KB |
Output is correct |
12 |
Correct |
177 ms |
17784 KB |
Output is correct |
13 |
Correct |
142 ms |
18424 KB |
Output is correct |
14 |
Correct |
148 ms |
18424 KB |
Output is correct |
15 |
Correct |
242 ms |
18552 KB |
Output is correct |
16 |
Correct |
451 ms |
18216 KB |
Output is correct |
17 |
Correct |
131 ms |
18296 KB |
Output is correct |
18 |
Correct |
148 ms |
18424 KB |
Output is correct |
19 |
Correct |
166 ms |
18424 KB |
Output is correct |
20 |
Correct |
330 ms |
18424 KB |
Output is correct |
21 |
Correct |
446 ms |
18168 KB |
Output is correct |
22 |
Correct |
274 ms |
18296 KB |
Output is correct |
23 |
Correct |
200 ms |
18296 KB |
Output is correct |
24 |
Correct |
203 ms |
18168 KB |
Output is correct |
25 |
Correct |
204 ms |
18552 KB |
Output is correct |
26 |
Correct |
165 ms |
18168 KB |
Output is correct |
27 |
Correct |
1166 ms |
26356 KB |
Output is correct |
28 |
Correct |
2264 ms |
26240 KB |
Output is correct |
29 |
Execution timed out |
3049 ms |
23928 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |