#include "werewolf.h"
#include <bits/stdc++.h>
using namespace std;
struct SegmentTree{
int n;
vector<vector<int>> t;
void init(int _n){
n=_n;
t.assign(4*n+1, {});
}
void build(int k, int l, int r, const vector<int> &a){
if (l==r){
t[k].push_back(a[l]);
return;
}
int mid=(l+r)>>1;
build(k<<1, l, mid, a);
build(k<<1|1, mid+1, r, a);
t[k].resize(t[k<<1].size()+t[k<<1|1].size());
merge(t[k<<1].begin(), t[k<<1].end(), t[k<<1|1].begin(), t[k<<1|1].end(), t[k].begin());
}
bool get(int k, int l, int r, int L, int R, int x, int y){
if (r<L || R<l) return 0;
if (L<=l && r<=R){
return upper_bound(t[k].begin(), t[k].end(), y)!=lower_bound(t[k].begin(), t[k].end(), x);
}
int mid=(l+r)>>1;
return get(k<<1, l, mid, L, R, x, y) || get(k<<1|1, mid+1, r, L, R, x, y);
}
} st;
const int N=2e5+10, LG=18;
struct DSU{
int n;
vector<int> lab;
vector<int> g[N];
int tin[N], tout[N], tdfs;
int up[LG][N];
void init(int _n, int root){
n=_n;
lab.assign(n+1, -1);
memset(tin, 0, sizeof tin);
memset(tout, 0, sizeof tout);
tdfs=-1;
up[0][root]=root;
}
int find_set(int u){
return lab[u]<0?u:lab[u]=find_set(lab[u]);
}
void union_sets(int u, int v){
v=find_set(v);
if (v!=u){
up[0][v]=u;
g[u].push_back(v);
lab[u]+=lab[v];
lab[v]=u;
}
}
void dfs(int u){
for (int k=1; k<LG; ++k) up[k][u]=up[k-1][up[k-1][u]];
tin[u]=++tdfs;
for (int v:g[u]) dfs(v);
tout[u]=tdfs;
}
} dsu1, dsu2;
vector<int> g[N];
vector<int> check_validity(int N, vector<int> X, vector<int> Y,
vector<int> S, vector<int> E,
vector<int> L, vector<int> R) {
int M=X.size();
for (int i=0; i<M; ++i) g[X[i]].push_back(Y[i]), g[Y[i]].push_back(X[i]);
int Q=S.size();
dsu1.init(N, 0);
for (int i=N-1; i>=0; --i){
for (int j:g[i]) if (j>i) dsu1.union_sets(i, j);
}
dsu2.init(N, N-1);
for (int i=0; i<N; ++i){
for (int j:g[i]) if (j<i) dsu2.union_sets(i, j);
}
dsu1.dfs(0);
dsu2.dfs(N-1);
st.init(N);
vector<int> a(N);
for (int i=0; i<N; ++i) a[dsu2.tin[i]]=dsu1.tin[i];
st.build(1, 0, N-1, a);
vector<int> ans(Q);
for (int i=0; i<Q; ++i){
for (int j=LG-1; j>=0; --j) if (dsu1.up[j][S[i]]>=L[i]) S[i]=dsu1.up[j][S[i]];
for (int j=LG-1; j>=0; --j) if (dsu2.up[j][E[i]]<=R[i]) E[i]=dsu2.up[j][E[i]];
ans[i]=st.get(1, 0, N-1, dsu2.tin[E[i]], dsu2.tout[E[i]], dsu1.tin[S[i]], dsu1.tout[S[i]]);
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
17756 KB |
Output is correct |
2 |
Correct |
8 ms |
17756 KB |
Output is correct |
3 |
Correct |
7 ms |
17756 KB |
Output is correct |
4 |
Correct |
8 ms |
17756 KB |
Output is correct |
5 |
Correct |
7 ms |
17756 KB |
Output is correct |
6 |
Correct |
7 ms |
17752 KB |
Output is correct |
7 |
Correct |
8 ms |
17756 KB |
Output is correct |
8 |
Correct |
8 ms |
17856 KB |
Output is correct |
9 |
Correct |
8 ms |
17752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
17756 KB |
Output is correct |
2 |
Correct |
8 ms |
17756 KB |
Output is correct |
3 |
Correct |
7 ms |
17756 KB |
Output is correct |
4 |
Correct |
8 ms |
17756 KB |
Output is correct |
5 |
Correct |
7 ms |
17756 KB |
Output is correct |
6 |
Correct |
7 ms |
17752 KB |
Output is correct |
7 |
Correct |
8 ms |
17756 KB |
Output is correct |
8 |
Correct |
8 ms |
17856 KB |
Output is correct |
9 |
Correct |
8 ms |
17752 KB |
Output is correct |
10 |
Correct |
12 ms |
19292 KB |
Output is correct |
11 |
Correct |
14 ms |
19164 KB |
Output is correct |
12 |
Correct |
11 ms |
19292 KB |
Output is correct |
13 |
Correct |
12 ms |
19488 KB |
Output is correct |
14 |
Correct |
11 ms |
19420 KB |
Output is correct |
15 |
Correct |
12 ms |
19420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
516 ms |
123968 KB |
Output is correct |
2 |
Correct |
426 ms |
125780 KB |
Output is correct |
3 |
Correct |
440 ms |
124580 KB |
Output is correct |
4 |
Correct |
442 ms |
123988 KB |
Output is correct |
5 |
Correct |
428 ms |
123984 KB |
Output is correct |
6 |
Correct |
559 ms |
123992 KB |
Output is correct |
7 |
Correct |
544 ms |
123728 KB |
Output is correct |
8 |
Correct |
457 ms |
125792 KB |
Output is correct |
9 |
Correct |
413 ms |
124804 KB |
Output is correct |
10 |
Correct |
350 ms |
123936 KB |
Output is correct |
11 |
Correct |
401 ms |
123808 KB |
Output is correct |
12 |
Correct |
444 ms |
123988 KB |
Output is correct |
13 |
Correct |
570 ms |
130732 KB |
Output is correct |
14 |
Correct |
581 ms |
130600 KB |
Output is correct |
15 |
Correct |
533 ms |
130896 KB |
Output is correct |
16 |
Correct |
532 ms |
130644 KB |
Output is correct |
17 |
Correct |
497 ms |
123728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
17756 KB |
Output is correct |
2 |
Correct |
8 ms |
17756 KB |
Output is correct |
3 |
Correct |
7 ms |
17756 KB |
Output is correct |
4 |
Correct |
8 ms |
17756 KB |
Output is correct |
5 |
Correct |
7 ms |
17756 KB |
Output is correct |
6 |
Correct |
7 ms |
17752 KB |
Output is correct |
7 |
Correct |
8 ms |
17756 KB |
Output is correct |
8 |
Correct |
8 ms |
17856 KB |
Output is correct |
9 |
Correct |
8 ms |
17752 KB |
Output is correct |
10 |
Correct |
12 ms |
19292 KB |
Output is correct |
11 |
Correct |
14 ms |
19164 KB |
Output is correct |
12 |
Correct |
11 ms |
19292 KB |
Output is correct |
13 |
Correct |
12 ms |
19488 KB |
Output is correct |
14 |
Correct |
11 ms |
19420 KB |
Output is correct |
15 |
Correct |
12 ms |
19420 KB |
Output is correct |
16 |
Correct |
516 ms |
123968 KB |
Output is correct |
17 |
Correct |
426 ms |
125780 KB |
Output is correct |
18 |
Correct |
440 ms |
124580 KB |
Output is correct |
19 |
Correct |
442 ms |
123988 KB |
Output is correct |
20 |
Correct |
428 ms |
123984 KB |
Output is correct |
21 |
Correct |
559 ms |
123992 KB |
Output is correct |
22 |
Correct |
544 ms |
123728 KB |
Output is correct |
23 |
Correct |
457 ms |
125792 KB |
Output is correct |
24 |
Correct |
413 ms |
124804 KB |
Output is correct |
25 |
Correct |
350 ms |
123936 KB |
Output is correct |
26 |
Correct |
401 ms |
123808 KB |
Output is correct |
27 |
Correct |
444 ms |
123988 KB |
Output is correct |
28 |
Correct |
570 ms |
130732 KB |
Output is correct |
29 |
Correct |
581 ms |
130600 KB |
Output is correct |
30 |
Correct |
533 ms |
130896 KB |
Output is correct |
31 |
Correct |
532 ms |
130644 KB |
Output is correct |
32 |
Correct |
497 ms |
123728 KB |
Output is correct |
33 |
Correct |
673 ms |
123988 KB |
Output is correct |
34 |
Correct |
199 ms |
49236 KB |
Output is correct |
35 |
Correct |
659 ms |
125784 KB |
Output is correct |
36 |
Correct |
611 ms |
124236 KB |
Output is correct |
37 |
Correct |
632 ms |
125308 KB |
Output is correct |
38 |
Correct |
642 ms |
124664 KB |
Output is correct |
39 |
Correct |
765 ms |
130604 KB |
Output is correct |
40 |
Correct |
656 ms |
133820 KB |
Output is correct |
41 |
Correct |
484 ms |
124916 KB |
Output is correct |
42 |
Correct |
376 ms |
124404 KB |
Output is correct |
43 |
Correct |
543 ms |
130360 KB |
Output is correct |
44 |
Correct |
523 ms |
125172 KB |
Output is correct |
45 |
Correct |
473 ms |
131068 KB |
Output is correct |
46 |
Correct |
446 ms |
130388 KB |
Output is correct |
47 |
Correct |
501 ms |
130760 KB |
Output is correct |
48 |
Correct |
489 ms |
130644 KB |
Output is correct |
49 |
Correct |
523 ms |
130764 KB |
Output is correct |
50 |
Correct |
501 ms |
130604 KB |
Output is correct |
51 |
Correct |
493 ms |
133716 KB |
Output is correct |
52 |
Correct |
538 ms |
133576 KB |
Output is correct |