#include <bits/stdc++.h>
using namespace std;
#define ios ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define mid ((l + r) >> 1)
#define lc (id << 1)
#define rc (lc | 1)
#define bug(str, x) cerr << str << " : " << x << '\n'
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 5e5 + 10;
const int inf = 1e9 + 10;
const int mod = 1e9 + 7;
int n, c[maxn], l[maxn], r[maxn], bound[2][maxn];
int seg[maxn << 2];
vector<int> K[maxn], ind[maxn];
set<int> S;
void add(int id, int l, int r, int p, int x){
if(p < l or p >= r)
return;
if(l + 1 == r){
seg[id] = x;
return;
}
add(lc, l, mid, p, x);
add(rc, mid, r, p, x);
seg[id] = min(seg[lc], seg[rc]);
}
int get(int id, int l, int r, int ql, int qr, int x){
if(qr <= l or r <= ql)
return -1;
if(l >= ql and r <= qr and seg[id] > x)
return -1;
if(l + 1 == r)
return l;
int R = get(rc, mid, r, ql, qr, x);
if(~R)
return R;
return get(lc, l, mid, ql, qr, x);
}
void solve(int d){
for(int i = 0; i < n - 1; i ++){
int idx = upper_bound(all(ind[c[i]]), i) - ind[c[i]].begin();
if(idx == ind[c[i]].size())
r[i] = n;
else
r[i] = ind[c[i]][idx];
idx --;
if(~idx)
l[i] = ind[c[i]][idx];
else
l[i] = idx;
add(1, 0, n, i, l[i]);
}
for(int i = 0; i < n; i ++)
S.insert(i);
for(int i = n - 2; ~i; i --){
int ind = get(1, 0, n, i + 1, r[i], i);
if(~ind){
while(true){
auto it = S.upper_bound(i);
if(it == S.end() or *it > ind)
break;
bound[d][*it] = i;
S.erase(it);
}
}
}
for(int u : S)
bound[d][u] = -1;
//for(int i = 0; i < n; i ++)
// cout << d << ' ' << bound[d][i] << '\n';
}
int main(){
ios;
cin >> n;
for(int i = 0; i < n - 1; i ++)
cin >> c[i];
for(int i = 0; i < n; i ++){
int b;
cin >> b;
for(int j = 0; j < b; j ++){
int a;
cin >> a;
K[i].pb(a);
ind[a].pb(i);
}
}
solve(0);
reverse(c, c + n - 1);
for(int i = 0; i <= n; i ++)
ind[i].clear();
for(int i = n - 1; ~i; i --)
for(int u : K[i])
ind[u].pb(n - 1 - i);
for(int i = 0; i <= 4 * n; i ++)
seg[i] = 0;
S.clear();
solve(1);
int q;
cin >> q;
while(q--){
int x, y;
cin >> x >> y;
x --, y --;
if(x < y)
cout << (n - 2 - bound[1][n - 1 - x] >= y ? "YES" : "NO") << '\n';
else
cout << (bound[0][x] < y ? "YES" : "NO") << '\n';
}
}
Compilation message
long_mansion.cpp: In function 'void solve(int)':
long_mansion.cpp:54:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | if(idx == ind[c[i]].size())
| ~~~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
24132 KB |
Output is correct |
2 |
Correct |
17 ms |
24268 KB |
Output is correct |
3 |
Correct |
19 ms |
24448 KB |
Output is correct |
4 |
Correct |
16 ms |
24152 KB |
Output is correct |
5 |
Correct |
15 ms |
24132 KB |
Output is correct |
6 |
Correct |
15 ms |
24204 KB |
Output is correct |
7 |
Correct |
16 ms |
24124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
24132 KB |
Output is correct |
2 |
Correct |
17 ms |
24268 KB |
Output is correct |
3 |
Correct |
19 ms |
24448 KB |
Output is correct |
4 |
Correct |
16 ms |
24152 KB |
Output is correct |
5 |
Correct |
15 ms |
24132 KB |
Output is correct |
6 |
Correct |
15 ms |
24204 KB |
Output is correct |
7 |
Correct |
16 ms |
24124 KB |
Output is correct |
8 |
Correct |
109 ms |
30092 KB |
Output is correct |
9 |
Correct |
108 ms |
29892 KB |
Output is correct |
10 |
Correct |
104 ms |
30456 KB |
Output is correct |
11 |
Correct |
106 ms |
30840 KB |
Output is correct |
12 |
Correct |
97 ms |
29540 KB |
Output is correct |
13 |
Correct |
103 ms |
30200 KB |
Output is correct |
14 |
Correct |
106 ms |
30140 KB |
Output is correct |
15 |
Correct |
100 ms |
30068 KB |
Output is correct |
16 |
Correct |
104 ms |
29932 KB |
Output is correct |
17 |
Correct |
107 ms |
30108 KB |
Output is correct |
18 |
Correct |
103 ms |
30100 KB |
Output is correct |
19 |
Correct |
108 ms |
30112 KB |
Output is correct |
20 |
Correct |
102 ms |
30084 KB |
Output is correct |
21 |
Correct |
104 ms |
30056 KB |
Output is correct |
22 |
Correct |
107 ms |
30064 KB |
Output is correct |
23 |
Correct |
122 ms |
30000 KB |
Output is correct |
24 |
Correct |
105 ms |
29892 KB |
Output is correct |
25 |
Correct |
106 ms |
29936 KB |
Output is correct |
26 |
Correct |
103 ms |
29876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
293 ms |
46788 KB |
Output is correct |
2 |
Correct |
305 ms |
46360 KB |
Output is correct |
3 |
Correct |
273 ms |
45964 KB |
Output is correct |
4 |
Correct |
281 ms |
46728 KB |
Output is correct |
5 |
Correct |
279 ms |
46780 KB |
Output is correct |
6 |
Correct |
251 ms |
44340 KB |
Output is correct |
7 |
Correct |
237 ms |
44228 KB |
Output is correct |
8 |
Correct |
240 ms |
44096 KB |
Output is correct |
9 |
Correct |
236 ms |
44228 KB |
Output is correct |
10 |
Correct |
237 ms |
44084 KB |
Output is correct |
11 |
Correct |
248 ms |
44008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
24132 KB |
Output is correct |
2 |
Correct |
17 ms |
24268 KB |
Output is correct |
3 |
Correct |
19 ms |
24448 KB |
Output is correct |
4 |
Correct |
16 ms |
24152 KB |
Output is correct |
5 |
Correct |
15 ms |
24132 KB |
Output is correct |
6 |
Correct |
15 ms |
24204 KB |
Output is correct |
7 |
Correct |
16 ms |
24124 KB |
Output is correct |
8 |
Correct |
109 ms |
30092 KB |
Output is correct |
9 |
Correct |
108 ms |
29892 KB |
Output is correct |
10 |
Correct |
104 ms |
30456 KB |
Output is correct |
11 |
Correct |
106 ms |
30840 KB |
Output is correct |
12 |
Correct |
97 ms |
29540 KB |
Output is correct |
13 |
Correct |
103 ms |
30200 KB |
Output is correct |
14 |
Correct |
106 ms |
30140 KB |
Output is correct |
15 |
Correct |
100 ms |
30068 KB |
Output is correct |
16 |
Correct |
104 ms |
29932 KB |
Output is correct |
17 |
Correct |
107 ms |
30108 KB |
Output is correct |
18 |
Correct |
103 ms |
30100 KB |
Output is correct |
19 |
Correct |
108 ms |
30112 KB |
Output is correct |
20 |
Correct |
102 ms |
30084 KB |
Output is correct |
21 |
Correct |
104 ms |
30056 KB |
Output is correct |
22 |
Correct |
107 ms |
30064 KB |
Output is correct |
23 |
Correct |
122 ms |
30000 KB |
Output is correct |
24 |
Correct |
105 ms |
29892 KB |
Output is correct |
25 |
Correct |
106 ms |
29936 KB |
Output is correct |
26 |
Correct |
103 ms |
29876 KB |
Output is correct |
27 |
Correct |
293 ms |
46788 KB |
Output is correct |
28 |
Correct |
305 ms |
46360 KB |
Output is correct |
29 |
Correct |
273 ms |
45964 KB |
Output is correct |
30 |
Correct |
281 ms |
46728 KB |
Output is correct |
31 |
Correct |
279 ms |
46780 KB |
Output is correct |
32 |
Correct |
251 ms |
44340 KB |
Output is correct |
33 |
Correct |
237 ms |
44228 KB |
Output is correct |
34 |
Correct |
240 ms |
44096 KB |
Output is correct |
35 |
Correct |
236 ms |
44228 KB |
Output is correct |
36 |
Correct |
237 ms |
44084 KB |
Output is correct |
37 |
Correct |
248 ms |
44008 KB |
Output is correct |
38 |
Correct |
879 ms |
85416 KB |
Output is correct |
39 |
Correct |
974 ms |
96144 KB |
Output is correct |
40 |
Correct |
654 ms |
72792 KB |
Output is correct |
41 |
Correct |
879 ms |
93996 KB |
Output is correct |
42 |
Correct |
270 ms |
46028 KB |
Output is correct |
43 |
Correct |
263 ms |
45700 KB |
Output is correct |
44 |
Correct |
480 ms |
61764 KB |
Output is correct |
45 |
Correct |
466 ms |
61972 KB |
Output is correct |
46 |
Correct |
480 ms |
62404 KB |
Output is correct |
47 |
Correct |
271 ms |
46224 KB |
Output is correct |
48 |
Correct |
253 ms |
45688 KB |
Output is correct |
49 |
Correct |
445 ms |
60948 KB |
Output is correct |
50 |
Correct |
458 ms |
61500 KB |
Output is correct |
51 |
Correct |
496 ms |
62884 KB |
Output is correct |
52 |
Correct |
427 ms |
63252 KB |
Output is correct |
53 |
Correct |
608 ms |
79068 KB |
Output is correct |
54 |
Correct |
776 ms |
95556 KB |
Output is correct |
55 |
Correct |
608 ms |
79380 KB |
Output is correct |