#include <iostream>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#define pii pair<int, int>
#define fst first
#define snd second
#define vi vector<int>
using namespace std;
/*
Idea :
We can transform this problem into a graph traversal problem
Node is defined as (L, R) which is the range currently accessible
The edges have a special property, which can be exploited,
such that we can maintain a data structure where we can query the rightmost cell reachable from a node.
Once we determine the rightmost reachable cell, we can determine the leftmost reachable cell.
*/
int n, q;
int val[500001], goL[500001], goR[500001];
vi pos[500001];
int L[500001], R[500001];
set<pii> s1, s2;
priority_queue<pii> pq;
inline void ins(int x)
{
pii lel = {x, x};
if (s1.size() && prev(s1.end()) -> snd + 1 == x) {lel.fst = prev(s1.end()) -> fst; s1.erase(prev(s1.end()));}
//cout << "insert " << lel.fst << " " << lel.snd << "\n";
s1.insert(lel);
}
inline void del(int x)
{
auto it = s1.upper_bound({x + 1, -1});
if (it != s1.begin())
{
pii lel = *prev(it); s1.erase(prev(it));
if (lel.fst < x)
{
//cout << "insert " << lel.fst << " " << x - 1 << "\n";
s1.insert({lel.fst, x - 1});
}
if (x < lel.snd)
{
//cout << "insert " << x + 1 << " " << lel.snd << "\n";
s1.insert({x + 1, lel.snd});
}
}
}
int main()
{
ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 0; i < n - 1; i++) {cin >> val[i];}
for (int i = 0; i < n; i++)
{
int x; cin >> x;
for (int j = 0; j < x; j++)
{
int k; cin >> k; pos[k].push_back(i);
}
}
for (int i = 0; i < n - 1; i++)
{
auto it = upper_bound(pos[val[i]].begin(), pos[val[i]].end(), i);
if (it != pos[val[i]].begin()) {goR[i] = *prev(it);}
else {goR[i] = -1;}
if (it != pos[val[i]].end()) {goL[i] = *it;}
else {goL[i] = n;}
if (goL[i] < n) {pq.push({goL[i], i}); ins(i);}
//cout << "Bridge " << i << " " << goL[i] << " " << goR[i] << "\n";
}
s2.insert({0, n - 1}); R[n - 1] = n - 1;
for (int i = n - 2; i >= 0; i--)
{
while (pq.size() && pq.top().fst > i)
{
del(pq.top().snd); pq.pop();
}
auto it = s1.upper_bound({goR[i] + 1, -1});
int np = goR[i] + 1;
if (it != s1.begin())
{
if (prev(it) -> fst <= goR[i] && goR[i] <= prev(it) -> snd) np = prev(it) -> snd + 2;
}
while (s2.size() && prev(s2.end()) -> fst >= np) {s2.erase(prev(s2.end()));}
//cout << np << " " << i << "\n";
if (np <= i) s2.insert({np, i});
R[i] = prev(s2.end()) -> snd;
//cout << i << " " << R[i] << "\n";
}
vector<pii> s;
for (int i = 0; i < n; i++)
{
L[i] = i;
auto it = upper_bound(s.begin(), s.end(), make_pair(R[i], n), greater<pii>());
if (it != s.end())
{
if (it == s.begin()) {L[i] = 0;}
else {L[i] = prev(it) -> snd + 1;}
}
if (i + 1 < n)
{
while (s.size() && s.back().fst <= goL[i]) {s.pop_back();}
s.push_back({goL[i], i});
}
//cout << i << " " << L[i] << " " << R[i] << "\n";
}
cin >> q;
while (q--)
{
int S, T; cin >> S >> T; S--; T--;
if (L[S] <= T && T <= R[S]) cout << "YES\n";
else cout << "NO\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
12288 KB |
Output is correct |
2 |
Correct |
15 ms |
12416 KB |
Output is correct |
3 |
Correct |
13 ms |
12416 KB |
Output is correct |
4 |
Correct |
13 ms |
12288 KB |
Output is correct |
5 |
Correct |
11 ms |
12288 KB |
Output is correct |
6 |
Correct |
11 ms |
12288 KB |
Output is correct |
7 |
Correct |
11 ms |
12288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
12288 KB |
Output is correct |
2 |
Correct |
15 ms |
12416 KB |
Output is correct |
3 |
Correct |
13 ms |
12416 KB |
Output is correct |
4 |
Correct |
13 ms |
12288 KB |
Output is correct |
5 |
Correct |
11 ms |
12288 KB |
Output is correct |
6 |
Correct |
11 ms |
12288 KB |
Output is correct |
7 |
Correct |
11 ms |
12288 KB |
Output is correct |
8 |
Correct |
113 ms |
15864 KB |
Output is correct |
9 |
Correct |
120 ms |
15736 KB |
Output is correct |
10 |
Correct |
115 ms |
15964 KB |
Output is correct |
11 |
Correct |
125 ms |
16248 KB |
Output is correct |
12 |
Correct |
110 ms |
15992 KB |
Output is correct |
13 |
Correct |
112 ms |
16100 KB |
Output is correct |
14 |
Correct |
114 ms |
15992 KB |
Output is correct |
15 |
Correct |
118 ms |
16120 KB |
Output is correct |
16 |
Correct |
110 ms |
16248 KB |
Output is correct |
17 |
Correct |
113 ms |
16060 KB |
Output is correct |
18 |
Correct |
111 ms |
15996 KB |
Output is correct |
19 |
Correct |
122 ms |
16156 KB |
Output is correct |
20 |
Correct |
118 ms |
16236 KB |
Output is correct |
21 |
Correct |
118 ms |
16248 KB |
Output is correct |
22 |
Correct |
118 ms |
16128 KB |
Output is correct |
23 |
Correct |
111 ms |
15864 KB |
Output is correct |
24 |
Correct |
112 ms |
15924 KB |
Output is correct |
25 |
Correct |
116 ms |
15864 KB |
Output is correct |
26 |
Correct |
126 ms |
15864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
224 ms |
20244 KB |
Output is correct |
2 |
Correct |
224 ms |
19948 KB |
Output is correct |
3 |
Correct |
217 ms |
19928 KB |
Output is correct |
4 |
Correct |
220 ms |
20048 KB |
Output is correct |
5 |
Correct |
217 ms |
19968 KB |
Output is correct |
6 |
Correct |
203 ms |
19176 KB |
Output is correct |
7 |
Correct |
187 ms |
19472 KB |
Output is correct |
8 |
Correct |
178 ms |
20140 KB |
Output is correct |
9 |
Correct |
188 ms |
20204 KB |
Output is correct |
10 |
Correct |
189 ms |
20144 KB |
Output is correct |
11 |
Correct |
187 ms |
20204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
12288 KB |
Output is correct |
2 |
Correct |
15 ms |
12416 KB |
Output is correct |
3 |
Correct |
13 ms |
12416 KB |
Output is correct |
4 |
Correct |
13 ms |
12288 KB |
Output is correct |
5 |
Correct |
11 ms |
12288 KB |
Output is correct |
6 |
Correct |
11 ms |
12288 KB |
Output is correct |
7 |
Correct |
11 ms |
12288 KB |
Output is correct |
8 |
Correct |
113 ms |
15864 KB |
Output is correct |
9 |
Correct |
120 ms |
15736 KB |
Output is correct |
10 |
Correct |
115 ms |
15964 KB |
Output is correct |
11 |
Correct |
125 ms |
16248 KB |
Output is correct |
12 |
Correct |
110 ms |
15992 KB |
Output is correct |
13 |
Correct |
112 ms |
16100 KB |
Output is correct |
14 |
Correct |
114 ms |
15992 KB |
Output is correct |
15 |
Correct |
118 ms |
16120 KB |
Output is correct |
16 |
Correct |
110 ms |
16248 KB |
Output is correct |
17 |
Correct |
113 ms |
16060 KB |
Output is correct |
18 |
Correct |
111 ms |
15996 KB |
Output is correct |
19 |
Correct |
122 ms |
16156 KB |
Output is correct |
20 |
Correct |
118 ms |
16236 KB |
Output is correct |
21 |
Correct |
118 ms |
16248 KB |
Output is correct |
22 |
Correct |
118 ms |
16128 KB |
Output is correct |
23 |
Correct |
111 ms |
15864 KB |
Output is correct |
24 |
Correct |
112 ms |
15924 KB |
Output is correct |
25 |
Correct |
116 ms |
15864 KB |
Output is correct |
26 |
Correct |
126 ms |
15864 KB |
Output is correct |
27 |
Correct |
224 ms |
20244 KB |
Output is correct |
28 |
Correct |
224 ms |
19948 KB |
Output is correct |
29 |
Correct |
217 ms |
19928 KB |
Output is correct |
30 |
Correct |
220 ms |
20048 KB |
Output is correct |
31 |
Correct |
217 ms |
19968 KB |
Output is correct |
32 |
Correct |
203 ms |
19176 KB |
Output is correct |
33 |
Correct |
187 ms |
19472 KB |
Output is correct |
34 |
Correct |
178 ms |
20140 KB |
Output is correct |
35 |
Correct |
188 ms |
20204 KB |
Output is correct |
36 |
Correct |
189 ms |
20144 KB |
Output is correct |
37 |
Correct |
187 ms |
20204 KB |
Output is correct |
38 |
Correct |
722 ms |
29728 KB |
Output is correct |
39 |
Correct |
698 ms |
32352 KB |
Output is correct |
40 |
Correct |
514 ms |
27012 KB |
Output is correct |
41 |
Correct |
485 ms |
31568 KB |
Output is correct |
42 |
Correct |
264 ms |
20580 KB |
Output is correct |
43 |
Correct |
233 ms |
20236 KB |
Output is correct |
44 |
Correct |
408 ms |
24932 KB |
Output is correct |
45 |
Correct |
371 ms |
25064 KB |
Output is correct |
46 |
Correct |
416 ms |
25344 KB |
Output is correct |
47 |
Correct |
223 ms |
20692 KB |
Output is correct |
48 |
Correct |
201 ms |
20328 KB |
Output is correct |
49 |
Correct |
315 ms |
24436 KB |
Output is correct |
50 |
Correct |
333 ms |
24932 KB |
Output is correct |
51 |
Correct |
341 ms |
25736 KB |
Output is correct |
52 |
Correct |
269 ms |
27380 KB |
Output is correct |
53 |
Correct |
341 ms |
33732 KB |
Output is correct |
54 |
Correct |
392 ms |
39140 KB |
Output is correct |
55 |
Correct |
322 ms |
32740 KB |
Output is correct |