#define _CRT_SECURE_NO_WARNINGS
#define fast ios::sync_with_stdio(false); cin.tie(0)
#define foru(i, k, n) for (int i = k; i < n; i++)
#define ford(i, k, n) for (int i = k; i >= n; i--)
#define pb push_back
#define ff first
#define ss second
#include <iostream>
#include <vector>
#include <algorithm>
#include <bitset>
#include <queue>
#include <unordered_set>
#include <set>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int sz = 5e5 + 5;
int n, m, c[sz], nc[sz];
vector<int> a[sz], na[sz], adj[sz];
int lfinal[sz], rfinal[sz], closestL[sz], closestR[sz], lmn[sz], rmx[sz];
short connectedTo[sz];
vector<pii> comps;
vector<int> top;
set<int> st[sz];
void input() {
cin >> n;
foru(i, 0, n - 1) {
cin >> c[i]; c[i]--;
}
foru(i, 0, n) {
int tmp, xx;
cin >> tmp;
while (tmp--) {
cin >> xx;
a[i].pb(--xx);
}
}
}
void computeConnectedTo() {
foru(i, 0, n) {
bool found = 0;
if (i != 0) {
for (int x : a[i]) {
if (x == c[i - 1]) {
connectedTo[i] = -1;
found = 1;
break;
}
}
if (found)continue;
}
if (i != n - 1) {
for (int x : a[i]) {
if (x == c[i]) {
connectedTo[i] = 1;
found = 1;
break;
}
}
if (found)continue;
}
}
}
void computeComps() {
int prvStart = 0;
foru(i, 0, n) {
if (i == n - 1 || connectedTo[i] != 1 || connectedTo[i + 1] != -1) {
comps.pb({ prvStart,i });
prvStart = i + 1;
}
}
m = comps.size();
unordered_set<int> mem;
foru(i, 0, m) {
mem.clear();
foru(j, comps[i].ff, comps[i].ss + 1) {
for (int x : a[j])mem.insert(x);
}
for (int x : mem) {
na[i].pb(x);
st[x].insert(i);
}
if (connectedTo[comps[i].ff] == -1)adj[i - 1].pb(i);
if (connectedTo[comps[i].ss] == 1)adj[i + 1].pb(i);
if (i != m - 1)nc[i] = c[comps[i].ss];
lfinal[i] = rfinal[i] = i;
}
}
void topoSort() {
vector<int> indeg(m, 0);
foru(i, 0, m) {
for (int x : adj[i])indeg[x]++;
}
queue<int> q;
foru(i, 0, m) {
if (indeg[i] == 0)q.push(i);
}
while (!q.empty()) {
int x = q.front(); q.pop();
top.pb(x);
for (int y : adj[x]) {
indeg[y]--;
if (indeg[y] == 0)q.push(y);
}
}
}
void computeClosest() {
for (int i = 0; i < n; i++) {
st[i].insert(-1), st[i].insert(m + 1);
}
for (int i = 0; i < m - 1; i++) {
auto it = st[nc[i]].upper_bound(i);
closestR[i] = (*it);
--it;
closestL[i] = (*it);
}
}
void computeRanges() {
int src;
foru(i, 0, m) {
src = top[i];
if (connectedTo[comps[src].ff] == 0 && connectedTo[comps[src].ss] == 0)continue;
else if (connectedTo[comps[src].ss] == 1) {
if (lfinal[src + 1] <= src) {
lfinal[src] = lfinal[src + 1];
rfinal[src] = rfinal[src + 1];
continue;
}
lfinal[src] = min(lfinal[src], lfinal[src + 1]);
rfinal[src] = max(rfinal[src], rfinal[src + 1]);
}
else if (connectedTo[comps[src].ff] == -1) {
if (rfinal[src - 1] >= src) {
lfinal[src] = lfinal[src - 1];
rfinal[src] = rfinal[src - 1];
continue;
}
lfinal[src] = min(lfinal[src], lfinal[src - 1]);
rfinal[src] = max(rfinal[src], rfinal[src - 1]);
}
while (true) {
if (rfinal[src] != m - 1 && closestL[rfinal[src]] >= lfinal[src]) rfinal[src]++;
else if (lfinal[src] != 0 && closestR[lfinal[src] - 1] <= rfinal[src]) lfinal[src]--;
else break;
}
}
}
void computeFinal() {
foru(i, 0, m) {
foru(j, comps[i].ff, comps[i].ss + 1) {
lmn[j] = comps[lfinal[i]].ff;
rmx[j] = comps[rfinal[i]].ss;
}
}
}
int main() {
fast;
input();
computeConnectedTo();
computeComps();
topoSort();
computeClosest();
computeRanges();
computeFinal();
int q, x, y;
cin >> q;
while (q--) {
cin >> x >> y;
x--; y--;
if (y >= lmn[x] && y <= rmx[x])cout << "YES\n";
else cout << "NO\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
59884 KB |
Output is correct |
2 |
Correct |
43 ms |
60012 KB |
Output is correct |
3 |
Correct |
56 ms |
60524 KB |
Output is correct |
4 |
Correct |
46 ms |
59884 KB |
Output is correct |
5 |
Correct |
41 ms |
59884 KB |
Output is correct |
6 |
Correct |
40 ms |
59884 KB |
Output is correct |
7 |
Correct |
39 ms |
59756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
59884 KB |
Output is correct |
2 |
Correct |
43 ms |
60012 KB |
Output is correct |
3 |
Correct |
56 ms |
60524 KB |
Output is correct |
4 |
Correct |
46 ms |
59884 KB |
Output is correct |
5 |
Correct |
41 ms |
59884 KB |
Output is correct |
6 |
Correct |
40 ms |
59884 KB |
Output is correct |
7 |
Correct |
39 ms |
59756 KB |
Output is correct |
8 |
Correct |
162 ms |
65772 KB |
Output is correct |
9 |
Correct |
149 ms |
65644 KB |
Output is correct |
10 |
Correct |
157 ms |
66176 KB |
Output is correct |
11 |
Correct |
165 ms |
66924 KB |
Output is correct |
12 |
Correct |
139 ms |
65132 KB |
Output is correct |
13 |
Correct |
147 ms |
66028 KB |
Output is correct |
14 |
Correct |
168 ms |
65844 KB |
Output is correct |
15 |
Correct |
148 ms |
65900 KB |
Output is correct |
16 |
Correct |
142 ms |
65772 KB |
Output is correct |
17 |
Correct |
146 ms |
65772 KB |
Output is correct |
18 |
Correct |
149 ms |
65856 KB |
Output is correct |
19 |
Correct |
148 ms |
65900 KB |
Output is correct |
20 |
Correct |
162 ms |
65900 KB |
Output is correct |
21 |
Correct |
153 ms |
65900 KB |
Output is correct |
22 |
Correct |
146 ms |
65772 KB |
Output is correct |
23 |
Correct |
152 ms |
65516 KB |
Output is correct |
24 |
Correct |
149 ms |
65516 KB |
Output is correct |
25 |
Correct |
164 ms |
65644 KB |
Output is correct |
26 |
Correct |
155 ms |
65520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1163 ms |
104040 KB |
Output is correct |
2 |
Correct |
1746 ms |
109728 KB |
Output is correct |
3 |
Correct |
2563 ms |
107616 KB |
Output is correct |
4 |
Correct |
1351 ms |
110584 KB |
Output is correct |
5 |
Correct |
1450 ms |
110400 KB |
Output is correct |
6 |
Correct |
997 ms |
97376 KB |
Output is correct |
7 |
Correct |
821 ms |
98536 KB |
Output is correct |
8 |
Correct |
667 ms |
98912 KB |
Output is correct |
9 |
Correct |
600 ms |
99296 KB |
Output is correct |
10 |
Correct |
419 ms |
99424 KB |
Output is correct |
11 |
Correct |
448 ms |
99352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
59884 KB |
Output is correct |
2 |
Correct |
43 ms |
60012 KB |
Output is correct |
3 |
Correct |
56 ms |
60524 KB |
Output is correct |
4 |
Correct |
46 ms |
59884 KB |
Output is correct |
5 |
Correct |
41 ms |
59884 KB |
Output is correct |
6 |
Correct |
40 ms |
59884 KB |
Output is correct |
7 |
Correct |
39 ms |
59756 KB |
Output is correct |
8 |
Correct |
162 ms |
65772 KB |
Output is correct |
9 |
Correct |
149 ms |
65644 KB |
Output is correct |
10 |
Correct |
157 ms |
66176 KB |
Output is correct |
11 |
Correct |
165 ms |
66924 KB |
Output is correct |
12 |
Correct |
139 ms |
65132 KB |
Output is correct |
13 |
Correct |
147 ms |
66028 KB |
Output is correct |
14 |
Correct |
168 ms |
65844 KB |
Output is correct |
15 |
Correct |
148 ms |
65900 KB |
Output is correct |
16 |
Correct |
142 ms |
65772 KB |
Output is correct |
17 |
Correct |
146 ms |
65772 KB |
Output is correct |
18 |
Correct |
149 ms |
65856 KB |
Output is correct |
19 |
Correct |
148 ms |
65900 KB |
Output is correct |
20 |
Correct |
162 ms |
65900 KB |
Output is correct |
21 |
Correct |
153 ms |
65900 KB |
Output is correct |
22 |
Correct |
146 ms |
65772 KB |
Output is correct |
23 |
Correct |
152 ms |
65516 KB |
Output is correct |
24 |
Correct |
149 ms |
65516 KB |
Output is correct |
25 |
Correct |
164 ms |
65644 KB |
Output is correct |
26 |
Correct |
155 ms |
65520 KB |
Output is correct |
27 |
Correct |
1163 ms |
104040 KB |
Output is correct |
28 |
Correct |
1746 ms |
109728 KB |
Output is correct |
29 |
Correct |
2563 ms |
107616 KB |
Output is correct |
30 |
Correct |
1351 ms |
110584 KB |
Output is correct |
31 |
Correct |
1450 ms |
110400 KB |
Output is correct |
32 |
Correct |
997 ms |
97376 KB |
Output is correct |
33 |
Correct |
821 ms |
98536 KB |
Output is correct |
34 |
Correct |
667 ms |
98912 KB |
Output is correct |
35 |
Correct |
600 ms |
99296 KB |
Output is correct |
36 |
Correct |
419 ms |
99424 KB |
Output is correct |
37 |
Correct |
448 ms |
99352 KB |
Output is correct |
38 |
Correct |
1116 ms |
175956 KB |
Output is correct |
39 |
Correct |
986 ms |
196436 KB |
Output is correct |
40 |
Correct |
918 ms |
155428 KB |
Output is correct |
41 |
Execution timed out |
3086 ms |
182868 KB |
Time limit exceeded |
42 |
Halted |
0 ms |
0 KB |
- |