#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++) {
pii val = qry(koosagatree[i+KOOSAGA_SZ].first, koosagatree[i+KOOSAGA_SZ].second);
if(val.first > 1) {
int need = connect[val.first-1];
if(hasinrange(need, val.first, val.second)) {
val.first--;
}
}
if(val.second < n) {
int need = connect[val.second];
if(hasinrange(need, val.first, val.second)) {
val.second++;
}
}
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 |
Incorrect |
21 ms |
20992 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
21 ms |
20992 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
235 ms |
32820 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
21 ms |
20992 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |