#include "werewolf.h"
#include<bits/stdc++.h>
using namespace std;
int N;
vector<vector<int>> adj;
vector<int> D;
vector<pair<int, int>> ch;
void init(int N) {
D = vector<int>(N); iota(begin(D), end(D), 0);
ch.resize(N);
}
int parent(int x) {
return (D[x] == x ? x : D[x] = parent(D[x]));
}
void unite(int x, int y) {
x = parent(x); y = parent(y);
if(x == y) return;
int s = (int)D.size();
D[x] = D[y] = s;
D.push_back(s);
ch.emplace_back(x, y);
}
void dfs(int S, int& tim3) {
if(S < N) {
ch[S] = {tim3, tim3};
++tim3;
return;
}
int x, y;
tie(x, y) = ch[S];
dfs(x, tim3); dfs(y, tim3);
ch[S].first = min(ch[x].first, ch[y].first);
ch[S].second = max(ch[x].second, ch[y].second);
}
void merg3(vector<int>& es, const vector<int>& a, const vector<int>& b) {
es.resize((int)a.size() + (int)b.size());
int i = 0, j = 0;
while(true) {
if(i < (int)a.size() && j < (int)b.size()) {
if(a[i] < b[j]) {
es[i + j] = a[i];
++i;
} else {
es[i + j] = b[j];
++j;
}
} else if(i < (int)a.size()) {
es[i + j] = a[i];
++i;
} else if(j < (int)b.size()) {
es[i + j] = b[j];
++j;
} else break;
}
}
vector<vector<int>> st;
int base;
void build(vector<pair<int, int>>& pos) {
sort(begin(pos), end(pos));
base = 1;
while(base < N) {
base *= 2;
}
st.resize(base * 2);
for(int i = 0; i < N; ++i) {
st[i + base].push_back(pos[i].second);
}
for(int i = base - 1; i >= 1; --i) {
merg3(st[i], st[2 * i], st[2 * i + 1]);
}
}
bool chck(int s, int l, int r) {
auto it = lower_bound(begin(st[s]), end(st[s]), l);
return (it != end(st[s]) && (*it) <= r);
}
bool query(int xl, int xr, int yl, int yr) {
xl += base;
xr += base;
while(xl <= xr) {
if(xl & 1) {
if(chck(xl, yl, yr)) return 1;
++xl;
}
if(!(xr & 1)) {
if(chck(xr, yl, yr)) return 1;
--xr;
}
xl /= 2;
xr /= 2;
}
return 0;
}
vector<int> check_validity(int _N, vector<int> X, vector<int> Y, vector<int> S, vector<int> E, vector<int> L, vector<int> R) {
N = _N;
adj.resize(N);
int M = (int)X.size();
for(int i = 0; i < M; ++i) {
adj[X[i]].push_back(Y[i]);
adj[Y[i]].push_back(X[i]);
}
int Q = (int)L.size();
vector<pair<int, int>> RL(Q), RR(Q), pos(N);
{
vector<int> sind(Q);
iota(begin(sind), end(sind), 0);
sort(begin(sind), end(sind), [&](const int& i, const int& j) { return L[i] > L[j]; });
init(N);
vector<int> par(Q);
int i = N - 1;
for(int j = 0; j < Q; ++j) {
for(; i >= L[sind[j]]; --i) {
for(auto& u : adj[i]) {
if(u >= i) unite(u, i);
}
}
par[sind[j]] = parent(S[sind[j]]);
}
for(; i >= 0; --i) {
for(auto& u : adj[i]) {
if(u >= i) unite(u, i);
}
}
int tim3 = 0; dfs((int)D.size() - 1, tim3);
for(int i = 0; i < Q; ++i) {
RL[i] = ch[par[i]];
}
for(int i = 0; i < N; ++i) {
pos[i].first = ch[i].first;
}
}
{
vector<int> sind(Q);
iota(begin(sind), end(sind), 0);
sort(begin(sind), end(sind), [&](const int& i, const int& j) { return R[i] < R[j]; });
init(N);
vector<int> par(Q);
int i = 0;
for(int j = 0; j < Q; ++j) {
for(; i <= R[sind[j]]; ++i) {
for(auto& u : adj[i]) {
if(u <= i) unite(u, i);
}
}
par[sind[j]] = parent(E[sind[j]]);
}
for(; i < N; ++i) {
for(auto& u : adj[i]) {
if(u <= i) unite(u, i);
}
}
int tim3 = 0; dfs((int)D.size() - 1, tim3);
for(int i = 0; i < Q; ++i) {
RR[i] = ch[par[i]];
}
for(int i = 0; i < N; ++i) {
pos[i].second = ch[i].first;
}
}
vector<int> ans(Q);
build(pos);
for(int i = 0; i < Q; ++i) {
ans[i] = query(RL[i].first, RL[i].second, RR[i].first, RR[i].second);
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
300 KB |
Output is correct |
6 |
Correct |
1 ms |
296 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
300 KB |
Output is correct |
6 |
Correct |
1 ms |
296 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
300 KB |
Output is correct |
10 |
Correct |
4 ms |
1364 KB |
Output is correct |
11 |
Correct |
5 ms |
1336 KB |
Output is correct |
12 |
Correct |
4 ms |
1364 KB |
Output is correct |
13 |
Correct |
4 ms |
1460 KB |
Output is correct |
14 |
Correct |
4 ms |
1500 KB |
Output is correct |
15 |
Correct |
5 ms |
1396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
344 ms |
74312 KB |
Output is correct |
2 |
Correct |
279 ms |
80476 KB |
Output is correct |
3 |
Correct |
278 ms |
77588 KB |
Output is correct |
4 |
Correct |
288 ms |
76160 KB |
Output is correct |
5 |
Correct |
310 ms |
76088 KB |
Output is correct |
6 |
Correct |
333 ms |
75868 KB |
Output is correct |
7 |
Correct |
289 ms |
75776 KB |
Output is correct |
8 |
Correct |
278 ms |
80456 KB |
Output is correct |
9 |
Correct |
242 ms |
77448 KB |
Output is correct |
10 |
Correct |
302 ms |
76044 KB |
Output is correct |
11 |
Correct |
304 ms |
76040 KB |
Output is correct |
12 |
Correct |
273 ms |
75816 KB |
Output is correct |
13 |
Correct |
321 ms |
80552 KB |
Output is correct |
14 |
Correct |
326 ms |
80536 KB |
Output is correct |
15 |
Correct |
356 ms |
80552 KB |
Output is correct |
16 |
Correct |
343 ms |
80496 KB |
Output is correct |
17 |
Correct |
322 ms |
75724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
300 KB |
Output is correct |
6 |
Correct |
1 ms |
296 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
300 KB |
Output is correct |
10 |
Correct |
4 ms |
1364 KB |
Output is correct |
11 |
Correct |
5 ms |
1336 KB |
Output is correct |
12 |
Correct |
4 ms |
1364 KB |
Output is correct |
13 |
Correct |
4 ms |
1460 KB |
Output is correct |
14 |
Correct |
4 ms |
1500 KB |
Output is correct |
15 |
Correct |
5 ms |
1396 KB |
Output is correct |
16 |
Correct |
344 ms |
74312 KB |
Output is correct |
17 |
Correct |
279 ms |
80476 KB |
Output is correct |
18 |
Correct |
278 ms |
77588 KB |
Output is correct |
19 |
Correct |
288 ms |
76160 KB |
Output is correct |
20 |
Correct |
310 ms |
76088 KB |
Output is correct |
21 |
Correct |
333 ms |
75868 KB |
Output is correct |
22 |
Correct |
289 ms |
75776 KB |
Output is correct |
23 |
Correct |
278 ms |
80456 KB |
Output is correct |
24 |
Correct |
242 ms |
77448 KB |
Output is correct |
25 |
Correct |
302 ms |
76044 KB |
Output is correct |
26 |
Correct |
304 ms |
76040 KB |
Output is correct |
27 |
Correct |
273 ms |
75816 KB |
Output is correct |
28 |
Correct |
321 ms |
80552 KB |
Output is correct |
29 |
Correct |
326 ms |
80536 KB |
Output is correct |
30 |
Correct |
356 ms |
80552 KB |
Output is correct |
31 |
Correct |
343 ms |
80496 KB |
Output is correct |
32 |
Correct |
322 ms |
75724 KB |
Output is correct |
33 |
Correct |
452 ms |
77196 KB |
Output is correct |
34 |
Correct |
186 ms |
35628 KB |
Output is correct |
35 |
Correct |
452 ms |
80600 KB |
Output is correct |
36 |
Correct |
389 ms |
76680 KB |
Output is correct |
37 |
Correct |
422 ms |
79836 KB |
Output is correct |
38 |
Correct |
418 ms |
77248 KB |
Output is correct |
39 |
Correct |
317 ms |
85408 KB |
Output is correct |
40 |
Correct |
401 ms |
85532 KB |
Output is correct |
41 |
Correct |
408 ms |
79156 KB |
Output is correct |
42 |
Correct |
344 ms |
76608 KB |
Output is correct |
43 |
Correct |
383 ms |
84180 KB |
Output is correct |
44 |
Correct |
385 ms |
79580 KB |
Output is correct |
45 |
Correct |
328 ms |
85800 KB |
Output is correct |
46 |
Correct |
284 ms |
85588 KB |
Output is correct |
47 |
Correct |
343 ms |
80844 KB |
Output is correct |
48 |
Correct |
317 ms |
80540 KB |
Output is correct |
49 |
Correct |
321 ms |
80716 KB |
Output is correct |
50 |
Correct |
343 ms |
80536 KB |
Output is correct |
51 |
Correct |
414 ms |
86360 KB |
Output is correct |
52 |
Correct |
385 ms |
86356 KB |
Output is correct |