#include "werewolf.h"
#include <bits/stdc++.h>
using namespace std;
#define TRACE(x) cout << #x << " :: " << x << endl;
#define _ << " " <<
#define FOR(i,a,b) for (int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for (int i=(a);i>=(b);--i)
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
const int mxN = 2e5+5;
const int lgN = 17;
const int mxM = 4e5+5;
const int mxQ = 2e5+5;
int N, M, Q;
struct Edge { int X, Y, W; } edge[mxM];
struct SegmentTree {
vector<int> d[8*mxN];
int n;
SegmentTree(int n): n(n) {}
void update(int x, int v, int s, int e, int p) {
if (s == e) {
d[p].push_back(v);
} else {
int m = (s+e)/2;
if (x <= m) update(x,v,s,m,p*2);
else update(x,v,m+1,e,p*2+1);
}
}
inline void update(int x, int v) { update(x,v,0,n-1,1); }
void build(int s, int e, int p) {
if (s != e) {
int m = (s+e)/2;
build(s,m,p*2);
build(m+1,e,p*2+1);
vector<int>& a = d[2*p], b = d[2*p+1];
for (int i = 0, j = 0; i < SZ(a) || j < SZ(b); ) {
if (i < SZ(a) && (j == SZ(b) || a[i] < b[j])) d[p].push_back(a[i++]);
else d[p].push_back(b[j++]);
}
}
}
inline void build() { build(0,n-1,1); }
int query(int x, int y, int u, int v, int s, int e, int p) {
if (s == x && e == y) {
return lower_bound(ALL(d[p]),v+1) - lower_bound(ALL(d[p]),u);
} else {
int m = (s+e)/2;
if (y <= m) return query(x,y,u,v,s,m,p*2);
if (x > m) return query(x,y,u,v,m+1,e,p*2+1);
return query(x,m,u,v,s,m,p*2) + query(m+1,y,u,v,m+1,e,p*2+1);
}
}
inline int query(int x, int y, int u, int v) { return query(x,y,u,v,0,n-1,1); }
} *ST;
struct KruskalTree {
static const int mxV = 2*mxN;
int pa[mxV], v[mxV];
vector<int> al[mxV];
int cnt, fa[mxV][lgN+1], pre[mxV], pst[mxV], dfsT;
KruskalTree(int n) {
FOR(i,0,n-1){
pa[i] = i;
v[i] = -1;
}
cnt = n;
memset(fa,-1,sizeof fa);
}
int finds(int i) { return (pa[i] == i) ? i : (pa[i] = finds(pa[i])); }
bool unions(int i, int j, int w) {
int x = finds(i), y = finds(j);
if (x != y) {
pa[cnt] = cnt;
v[cnt] = w;
al[cnt].push_back(x);
al[cnt].push_back(y);
fa[x][0] = cnt;
fa[y][0] = cnt;
pa[x] = pa[y] = cnt;
++cnt;
return true;
} return false;
}
void dfs(int u) {
pre[u] = dfsT++;
FOR(k,1,lgN){
if (fa[u][k-1] == -1) fa[u][k] = -1;
else fa[u][k] = fa[fa[u][k-1]][k-1];
}
for (int v : al[u]) { dfs(v); }
pst[u] = dfsT-1;
//TRACE(u _ v[u] _ pre[u] _ pst[u]);
}
void init() {
dfsT = 0;
dfs(cnt-1);
}
int findFaGE(int u, int val) {
RFOR(k,lgN,0){
if (fa[u][k] != -1 && v[fa[u][k]] >= val) u = fa[u][k];
}
return u;
}
} *KT1, *KT2;
std::vector<int> check_validity(int _N, std::vector<int> X, std::vector<int> Y,
std::vector<int> S, std::vector<int> E,
std::vector<int> L, std::vector<int> R) {
N = _N, M = SZ(X), Q = SZ(S);
FOR(i,0,M-1){
edge[i] = (Edge){ X[i], Y[i], min(X[i],Y[i]) };
}
sort(edge,edge+M,[](Edge a, Edge b){
if (a.W == b.W) return (a.X == b.X ? a.Y < b.Y : a.X < b.X);
return a.W > b.W;
});
KT1 = new KruskalTree(N);
FOR(i,0,M-1){
KT1->unions(edge[i].X,edge[i].Y,edge[i].W);
}
KT1->init();
FOR(i,0,M-1) edge[i].W = max(edge[i].X,edge[i].Y);
sort(edge,edge+M,[](Edge a, Edge b){
if (a.W == b.W) return (a.X == b.X ? a.Y < b.Y : a.X < b.X);
return a.W < b.W;
});
KT2 = new KruskalTree(N);
FOR(i,0,M-1){
KT2->unions(edge[i].X,edge[i].Y,-edge[i].W);
}
KT2->init();
ST = new SegmentTree(2*N);
FOR(i,0,N-1){
//TRACE(i _ KT1->pre[i] _ KT2->pre[i]);
ST->update(KT1->pre[i],KT2->pre[i]);
}
ST->build();
vector<int> ans(Q);
FOR(i,0,Q-1){
int sp = KT1->findFaGE(S[i],L[i]);
int ep = KT2->findFaGE(E[i],-R[i]);
//TRACE(sp _ KT1->pre[sp] _ KT1->pst[sp] _ ep _ KT2->pre[ep] _ KT2->pst[ep]);
ans[i] = ST->query(KT1->pre[sp],KT1->pst[sp],KT2->pre[ep],KT2->pst[ep]) > 0;
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
113144 KB |
Output is correct |
2 |
Correct |
59 ms |
113140 KB |
Output is correct |
3 |
Correct |
58 ms |
113148 KB |
Output is correct |
4 |
Correct |
59 ms |
113144 KB |
Output is correct |
5 |
Correct |
62 ms |
113144 KB |
Output is correct |
6 |
Correct |
60 ms |
113144 KB |
Output is correct |
7 |
Correct |
60 ms |
113144 KB |
Output is correct |
8 |
Correct |
60 ms |
113148 KB |
Output is correct |
9 |
Correct |
58 ms |
113144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
113144 KB |
Output is correct |
2 |
Correct |
59 ms |
113140 KB |
Output is correct |
3 |
Correct |
58 ms |
113148 KB |
Output is correct |
4 |
Correct |
59 ms |
113144 KB |
Output is correct |
5 |
Correct |
62 ms |
113144 KB |
Output is correct |
6 |
Correct |
60 ms |
113144 KB |
Output is correct |
7 |
Correct |
60 ms |
113144 KB |
Output is correct |
8 |
Correct |
60 ms |
113148 KB |
Output is correct |
9 |
Correct |
58 ms |
113144 KB |
Output is correct |
10 |
Correct |
68 ms |
114296 KB |
Output is correct |
11 |
Correct |
69 ms |
114296 KB |
Output is correct |
12 |
Correct |
68 ms |
114220 KB |
Output is correct |
13 |
Correct |
69 ms |
114296 KB |
Output is correct |
14 |
Correct |
67 ms |
114296 KB |
Output is correct |
15 |
Correct |
71 ms |
114296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1004 ms |
192148 KB |
Output is correct |
2 |
Correct |
1610 ms |
194352 KB |
Output is correct |
3 |
Correct |
1415 ms |
193004 KB |
Output is correct |
4 |
Correct |
1089 ms |
192228 KB |
Output is correct |
5 |
Correct |
1127 ms |
192220 KB |
Output is correct |
6 |
Correct |
1031 ms |
191976 KB |
Output is correct |
7 |
Correct |
994 ms |
192268 KB |
Output is correct |
8 |
Correct |
1590 ms |
194044 KB |
Output is correct |
9 |
Correct |
1096 ms |
192796 KB |
Output is correct |
10 |
Correct |
864 ms |
191772 KB |
Output is correct |
11 |
Correct |
874 ms |
191836 KB |
Output is correct |
12 |
Correct |
800 ms |
192096 KB |
Output is correct |
13 |
Correct |
1584 ms |
194960 KB |
Output is correct |
14 |
Correct |
1582 ms |
194996 KB |
Output is correct |
15 |
Correct |
1589 ms |
195188 KB |
Output is correct |
16 |
Correct |
1644 ms |
194904 KB |
Output is correct |
17 |
Correct |
1004 ms |
192340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
113144 KB |
Output is correct |
2 |
Correct |
59 ms |
113140 KB |
Output is correct |
3 |
Correct |
58 ms |
113148 KB |
Output is correct |
4 |
Correct |
59 ms |
113144 KB |
Output is correct |
5 |
Correct |
62 ms |
113144 KB |
Output is correct |
6 |
Correct |
60 ms |
113144 KB |
Output is correct |
7 |
Correct |
60 ms |
113144 KB |
Output is correct |
8 |
Correct |
60 ms |
113148 KB |
Output is correct |
9 |
Correct |
58 ms |
113144 KB |
Output is correct |
10 |
Correct |
68 ms |
114296 KB |
Output is correct |
11 |
Correct |
69 ms |
114296 KB |
Output is correct |
12 |
Correct |
68 ms |
114220 KB |
Output is correct |
13 |
Correct |
69 ms |
114296 KB |
Output is correct |
14 |
Correct |
67 ms |
114296 KB |
Output is correct |
15 |
Correct |
71 ms |
114296 KB |
Output is correct |
16 |
Correct |
1004 ms |
192148 KB |
Output is correct |
17 |
Correct |
1610 ms |
194352 KB |
Output is correct |
18 |
Correct |
1415 ms |
193004 KB |
Output is correct |
19 |
Correct |
1089 ms |
192228 KB |
Output is correct |
20 |
Correct |
1127 ms |
192220 KB |
Output is correct |
21 |
Correct |
1031 ms |
191976 KB |
Output is correct |
22 |
Correct |
994 ms |
192268 KB |
Output is correct |
23 |
Correct |
1590 ms |
194044 KB |
Output is correct |
24 |
Correct |
1096 ms |
192796 KB |
Output is correct |
25 |
Correct |
864 ms |
191772 KB |
Output is correct |
26 |
Correct |
874 ms |
191836 KB |
Output is correct |
27 |
Correct |
800 ms |
192096 KB |
Output is correct |
28 |
Correct |
1584 ms |
194960 KB |
Output is correct |
29 |
Correct |
1582 ms |
194996 KB |
Output is correct |
30 |
Correct |
1589 ms |
195188 KB |
Output is correct |
31 |
Correct |
1644 ms |
194904 KB |
Output is correct |
32 |
Correct |
1004 ms |
192340 KB |
Output is correct |
33 |
Correct |
1576 ms |
192276 KB |
Output is correct |
34 |
Correct |
577 ms |
143276 KB |
Output is correct |
35 |
Correct |
1927 ms |
195468 KB |
Output is correct |
36 |
Correct |
1415 ms |
192308 KB |
Output is correct |
37 |
Correct |
1817 ms |
194196 KB |
Output is correct |
38 |
Correct |
1545 ms |
192756 KB |
Output is correct |
39 |
Correct |
1239 ms |
197020 KB |
Output is correct |
40 |
Correct |
1469 ms |
203032 KB |
Output is correct |
41 |
Correct |
1496 ms |
193644 KB |
Output is correct |
42 |
Correct |
1444 ms |
192216 KB |
Output is correct |
43 |
Correct |
2090 ms |
200144 KB |
Output is correct |
44 |
Correct |
1689 ms |
194300 KB |
Output is correct |
45 |
Correct |
1379 ms |
197432 KB |
Output is correct |
46 |
Correct |
1806 ms |
196964 KB |
Output is correct |
47 |
Correct |
1643 ms |
195460 KB |
Output is correct |
48 |
Correct |
1698 ms |
195056 KB |
Output is correct |
49 |
Correct |
1568 ms |
195472 KB |
Output is correct |
50 |
Correct |
1625 ms |
195168 KB |
Output is correct |
51 |
Correct |
1290 ms |
203368 KB |
Output is correct |
52 |
Correct |
1275 ms |
203348 KB |
Output is correct |