#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>
using namespace std;
// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef vector<int> vi;
// END NO SAD
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<vector<ll>> matrix;
typedef pair<ll, ll> pll;
const int KOOSAGA_SZ = 1 << 19;
pii koosagatree[2 * KOOSAGA_SZ];
int connect[KOOSAGA_SZ];
vector<int> keystorooms[KOOSAGA_SZ];
bool hasinrange(int key, int lhs, int rhs) {
auto it = lower_bound(all(keystorooms[key]), lhs);
return it != keystorooms[key].end() && *it <= rhs;
}
int n;
pii merge(pii a, pii b) {
return pii(min(a.first,b.first), max(a.second,b.second));
}
void upd(int idx, pii v) {
idx += KOOSAGA_SZ;
koosagatree[idx] = merge(koosagatree[idx], v);
while(idx > 1) {
idx /= 2;
koosagatree[idx] = merge(koosagatree[2*idx], koosagatree[2*idx+1]);
}
}
pii qry(int lhs, int rhs) {
pii ret = {1e9, 0};
lhs += KOOSAGA_SZ;
rhs += KOOSAGA_SZ;
while(lhs <= rhs) {
if(lhs == rhs) return merge(ret, koosagatree[lhs]);
if(lhs%2) ret = merge(ret, koosagatree[lhs++]);
if(rhs%2==0) ret = merge(ret, koosagatree[rhs--]);
lhs /= 2;
rhs /= 2;
}
return ret;
}
void realprop() {
for(int i = 1; i <= n; i++) {
while(true) {
pii val = qry(koosagatree[i+KOOSAGA_SZ].first, koosagatree[i+KOOSAGA_SZ].second);
bool dirty = val != koosagatree[i + KOOSAGA_SZ];
if(val.first > 1) {
int need = connect[val.first-1];
if(hasinrange(need, val.first, val.second)) {
dirty = true;
val.first--;
}
}
if(val.second < n) {
int need = connect[val.second];
if(hasinrange(need, val.first, val.second)) {
dirty = true;
val.second++;
}
}
if(!dirty) break;
upd(i, val);
}
}
}
void prop() {
for(int qq = 0; qq < 2; qq++) realprop();
}
void init() {
for(int i = 0; i < KOOSAGA_SZ; i++) koosagatree[i+KOOSAGA_SZ] = {i, i};
for(int i = KOOSAGA_SZ-1; i > 0; i--) {
koosagatree[i] = merge(koosagatree[2*i], koosagatree[2*i+1]);
}
}
void solve() {
cin >> n;
for(int i = 1; i < n; i++) cin >> connect[i];
for(int i = 1; i <= n; i++) {
int k;
cin >> k;
while(k--) {
int key;
cin >> key;
keystorooms[key].push_back(i);
}
}
init();
prop();
int q;
cin >> q;
while(q--) {
int x, y;
cin >> x >> y;
pii ret = koosagatree[x + KOOSAGA_SZ];
if(ret.first <= y && y <= ret.second) cout << "YES\n";
else cout << "NO\n";
}
}
// are there edge cases (N=1?)
// are array sizes proper (scaled by proper constant, for example 2* for koosaga tree)
// integer overflow?
// DS reset properly between test cases
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
20992 KB |
Output is correct |
2 |
Correct |
21 ms |
20992 KB |
Output is correct |
3 |
Correct |
22 ms |
20992 KB |
Output is correct |
4 |
Correct |
20 ms |
20992 KB |
Output is correct |
5 |
Correct |
20 ms |
20992 KB |
Output is correct |
6 |
Correct |
20 ms |
20984 KB |
Output is correct |
7 |
Correct |
19 ms |
20992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
20992 KB |
Output is correct |
2 |
Correct |
21 ms |
20992 KB |
Output is correct |
3 |
Correct |
22 ms |
20992 KB |
Output is correct |
4 |
Correct |
20 ms |
20992 KB |
Output is correct |
5 |
Correct |
20 ms |
20992 KB |
Output is correct |
6 |
Correct |
20 ms |
20984 KB |
Output is correct |
7 |
Correct |
19 ms |
20992 KB |
Output is correct |
8 |
Correct |
131 ms |
22648 KB |
Output is correct |
9 |
Correct |
125 ms |
22636 KB |
Output is correct |
10 |
Correct |
129 ms |
22776 KB |
Output is correct |
11 |
Correct |
138 ms |
22952 KB |
Output is correct |
12 |
Correct |
114 ms |
22904 KB |
Output is correct |
13 |
Correct |
128 ms |
22904 KB |
Output is correct |
14 |
Correct |
127 ms |
22904 KB |
Output is correct |
15 |
Correct |
122 ms |
22908 KB |
Output is correct |
16 |
Correct |
114 ms |
23160 KB |
Output is correct |
17 |
Correct |
124 ms |
22904 KB |
Output is correct |
18 |
Correct |
124 ms |
22904 KB |
Output is correct |
19 |
Correct |
126 ms |
22904 KB |
Output is correct |
20 |
Correct |
119 ms |
23032 KB |
Output is correct |
21 |
Correct |
121 ms |
23160 KB |
Output is correct |
22 |
Correct |
121 ms |
22904 KB |
Output is correct |
23 |
Correct |
123 ms |
22904 KB |
Output is correct |
24 |
Correct |
123 ms |
22776 KB |
Output is correct |
25 |
Correct |
134 ms |
22776 KB |
Output is correct |
26 |
Correct |
126 ms |
22776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
274 ms |
25464 KB |
Output is correct |
2 |
Correct |
273 ms |
25208 KB |
Output is correct |
3 |
Correct |
266 ms |
25440 KB |
Output is correct |
4 |
Correct |
273 ms |
25336 KB |
Output is correct |
5 |
Correct |
279 ms |
25336 KB |
Output is correct |
6 |
Correct |
259 ms |
24440 KB |
Output is correct |
7 |
Correct |
233 ms |
24696 KB |
Output is correct |
8 |
Correct |
226 ms |
24568 KB |
Output is correct |
9 |
Correct |
230 ms |
24696 KB |
Output is correct |
10 |
Correct |
221 ms |
24572 KB |
Output is correct |
11 |
Correct |
226 ms |
24696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
20992 KB |
Output is correct |
2 |
Correct |
21 ms |
20992 KB |
Output is correct |
3 |
Correct |
22 ms |
20992 KB |
Output is correct |
4 |
Correct |
20 ms |
20992 KB |
Output is correct |
5 |
Correct |
20 ms |
20992 KB |
Output is correct |
6 |
Correct |
20 ms |
20984 KB |
Output is correct |
7 |
Correct |
19 ms |
20992 KB |
Output is correct |
8 |
Correct |
131 ms |
22648 KB |
Output is correct |
9 |
Correct |
125 ms |
22636 KB |
Output is correct |
10 |
Correct |
129 ms |
22776 KB |
Output is correct |
11 |
Correct |
138 ms |
22952 KB |
Output is correct |
12 |
Correct |
114 ms |
22904 KB |
Output is correct |
13 |
Correct |
128 ms |
22904 KB |
Output is correct |
14 |
Correct |
127 ms |
22904 KB |
Output is correct |
15 |
Correct |
122 ms |
22908 KB |
Output is correct |
16 |
Correct |
114 ms |
23160 KB |
Output is correct |
17 |
Correct |
124 ms |
22904 KB |
Output is correct |
18 |
Correct |
124 ms |
22904 KB |
Output is correct |
19 |
Correct |
126 ms |
22904 KB |
Output is correct |
20 |
Correct |
119 ms |
23032 KB |
Output is correct |
21 |
Correct |
121 ms |
23160 KB |
Output is correct |
22 |
Correct |
121 ms |
22904 KB |
Output is correct |
23 |
Correct |
123 ms |
22904 KB |
Output is correct |
24 |
Correct |
123 ms |
22776 KB |
Output is correct |
25 |
Correct |
134 ms |
22776 KB |
Output is correct |
26 |
Correct |
126 ms |
22776 KB |
Output is correct |
27 |
Correct |
274 ms |
25464 KB |
Output is correct |
28 |
Correct |
273 ms |
25208 KB |
Output is correct |
29 |
Correct |
266 ms |
25440 KB |
Output is correct |
30 |
Correct |
273 ms |
25336 KB |
Output is correct |
31 |
Correct |
279 ms |
25336 KB |
Output is correct |
32 |
Correct |
259 ms |
24440 KB |
Output is correct |
33 |
Correct |
233 ms |
24696 KB |
Output is correct |
34 |
Correct |
226 ms |
24568 KB |
Output is correct |
35 |
Correct |
230 ms |
24696 KB |
Output is correct |
36 |
Correct |
221 ms |
24572 KB |
Output is correct |
37 |
Correct |
226 ms |
24696 KB |
Output is correct |
38 |
Correct |
333 ms |
27312 KB |
Output is correct |
39 |
Correct |
401 ms |
27640 KB |
Output is correct |
40 |
Correct |
306 ms |
27000 KB |
Output is correct |
41 |
Correct |
619 ms |
27104 KB |
Output is correct |
42 |
Correct |
257 ms |
24952 KB |
Output is correct |
43 |
Correct |
265 ms |
24696 KB |
Output is correct |
44 |
Correct |
400 ms |
27128 KB |
Output is correct |
45 |
Correct |
405 ms |
27512 KB |
Output is correct |
46 |
Correct |
435 ms |
27512 KB |
Output is correct |
47 |
Correct |
298 ms |
25080 KB |
Output is correct |
48 |
Correct |
269 ms |
24824 KB |
Output is correct |
49 |
Correct |
393 ms |
26616 KB |
Output is correct |
50 |
Correct |
500 ms |
27000 KB |
Output is correct |
51 |
Correct |
456 ms |
27816 KB |
Output is correct |
52 |
Correct |
280 ms |
29432 KB |
Output is correct |
53 |
Correct |
371 ms |
32620 KB |
Output is correct |
54 |
Correct |
379 ms |
36472 KB |
Output is correct |
55 |
Correct |
312 ms |
32760 KB |
Output is correct |