#include "werewolf.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
#define mt make_tuple
using iii = tuple<int, int, int>;
int hpos, wpos, to[200005], link[200005], sz[200005], rep[200005], ancH[600005][25], ancW[600005][25], depH[600005], depW[600005], stH[600005], edH[600005], stW[600005], edW[600005];
vector<int> hadj[600005], wadj[600005];
iii human[400005], wolf[400005];
int find(int x) {
if (x == link[x]) return x;
return link[x] = find(link[x]);
}
void unite(int a, int b) {
a = find(a);
b = find(b);
if (a == b) return;
if (sz[b] > sz[a]) swap(a, b);
sz[a] += sz[b];
link[b] = a;
}
void dfsH(int n, int e = -1) {
ancH[n][0] = e;
for (int i = 1; i <= 20; i++)
if (ancH[n][i - 1] != -1)
ancH[n][i] = ancH[ancH[n][i - 1]][i - 1];
int minst = 1e9, maxed = -1e9;
for (auto u : hadj[n])
if (u != e) {
depH[u] = depH[n] + 1;
dfsH(u, n);
minst = min(minst, stH[u]);
maxed = max(maxed, edH[u]);
}
stH[n] = minst;
edH[n] = maxed;
if (hadj[n].empty())
stH[n] = edH[n] = hpos++;
}
void dfsW(int n, int e = -1) {
ancW[n][0] = e;
for (int i = 1; i <= 20; i++)
if (ancW[n][i - 1] != -1)
ancW[n][i] = ancW[ancW[n][i - 1]][i - 1];
int minst = 1e9, maxed = -1e9;
for (auto u : wadj[n])
if (u != e) {
depW[u] = depW[n] + 1;
dfsW(u, n);
minst = min(minst, stW[u]);
maxed = max(maxed, edW[u]);
}
stW[n] = minst;
edW[n] = maxed;
if (wadj[n].empty())
stW[n] = edW[n] = wpos++;
}
struct node {
node *left, *right;
int S, E;
vector<int> val;
node(int _s, int _e) : S(_s), E(_e) {
if (S == E) {
val.pb(to[S]);
return;
}
int M = (S + E) >> 1;
left = new node(S, M);
right = new node(M + 1, E);
merge(left->val.begin(), left->val.end(), right->val.begin(), right->val.end(), back_inserter(val));
}
bool qry(int l, int r, int l2, int r2) {
if (l > E || r < S) return 0;
if (l <= S && E <= r) {
auto it = lower_bound(val.begin(), val.end(), l2);
if (it == val.end() || *it > r2) return 0;
return 1;
}
return left->qry(l, r, l2, r2) || right->qry(l, r, l2, r2);
}
} *root;
vector<int> check_validity(int N, vector<int> X, vector<int> Y, vector<int> S, vector<int> E, vector<int> L, vector<int> R) {
memset(ancH, -1, sizeof ancH);
memset(ancW, -1, sizeof ancW);
int M = X.size();
for (int i = 0; i < N; i++) {
link[i] = rep[i] = i;
sz[i] = 1;
}
for (int i = 0; i < M; i++) {
human[i] = mt(min(X[i], Y[i]), X[i], Y[i]);
wolf[i] = mt(max(X[i], Y[i]), X[i], Y[i]);
}
sort(human, human + X.size(), greater<iii>());
sort(wolf, wolf + X.size());
for (int i = 0; i < M; i++) {
auto [w, u, v] = human[i];
int a = rep[find(u)], b = rep[find(v)];
if (a != b) {
hadj[N + i].pb(a);
hadj[N + i].pb(b);
} else {
hadj[N + i].pb(a);
}
unite(u, v);
rep[find(u)] = N + i;
}
for (int i = 0; i < N; i++) {
link[i] = rep[i] = i;
sz[i] = 1;
}
for (int i = 0; i < M; i++) {
auto [w, u, v] = wolf[i];
int a = rep[find(u)], b = rep[find(v)];
if (a != b) {
wadj[N + i].pb(a);
wadj[N + i].pb(b);
} else {
wadj[N + i].pb(a);
}
unite(u, v);
rep[find(u)] = N + i;
}
dfsH(N + M - 1);
dfsW(N + M - 1);
for (int i = 0; i < N; i++)
to[stH[i]] = stW[i];
root = new node(0, N - 1);
int Q = S.size();
vector<int> A;
for (int i = 0; i < Q; i++) {
for (int j = 20; j >= 0; j--)
if (ancH[S[i]][j] != -1 && g0(human[ancH[S[i]][j] - N]) >= L[i]) S[i] = ancH[S[i]][j];
for (int j = 20; j >= 0; j--)
if (ancW[E[i]][j] != -1 && g0(wolf[ancW[E[i]][j] - N]) <= R[i]) E[i] = ancW[E[i]][j];
A.pb(root->qry(stH[S[i]], edH[S[i]], stW[E[i]], edW[E[i]]));
}
return A;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
145980 KB |
Output is correct |
2 |
Correct |
50 ms |
145888 KB |
Output is correct |
3 |
Correct |
58 ms |
145984 KB |
Output is correct |
4 |
Correct |
50 ms |
145868 KB |
Output is correct |
5 |
Correct |
50 ms |
145992 KB |
Output is correct |
6 |
Correct |
53 ms |
145984 KB |
Output is correct |
7 |
Correct |
57 ms |
145888 KB |
Output is correct |
8 |
Correct |
49 ms |
145980 KB |
Output is correct |
9 |
Correct |
50 ms |
145948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
145980 KB |
Output is correct |
2 |
Correct |
50 ms |
145888 KB |
Output is correct |
3 |
Correct |
58 ms |
145984 KB |
Output is correct |
4 |
Correct |
50 ms |
145868 KB |
Output is correct |
5 |
Correct |
50 ms |
145992 KB |
Output is correct |
6 |
Correct |
53 ms |
145984 KB |
Output is correct |
7 |
Correct |
57 ms |
145888 KB |
Output is correct |
8 |
Correct |
49 ms |
145980 KB |
Output is correct |
9 |
Correct |
50 ms |
145948 KB |
Output is correct |
10 |
Correct |
58 ms |
147560 KB |
Output is correct |
11 |
Correct |
56 ms |
147396 KB |
Output is correct |
12 |
Correct |
56 ms |
147404 KB |
Output is correct |
13 |
Correct |
57 ms |
147708 KB |
Output is correct |
14 |
Correct |
56 ms |
147616 KB |
Output is correct |
15 |
Correct |
57 ms |
147884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
866 ms |
246932 KB |
Output is correct |
2 |
Correct |
804 ms |
257992 KB |
Output is correct |
3 |
Correct |
658 ms |
250912 KB |
Output is correct |
4 |
Correct |
665 ms |
247772 KB |
Output is correct |
5 |
Correct |
712 ms |
247512 KB |
Output is correct |
6 |
Correct |
799 ms |
247240 KB |
Output is correct |
7 |
Correct |
724 ms |
247144 KB |
Output is correct |
8 |
Correct |
825 ms |
257876 KB |
Output is correct |
9 |
Correct |
503 ms |
250836 KB |
Output is correct |
10 |
Correct |
536 ms |
247656 KB |
Output is correct |
11 |
Correct |
542 ms |
247516 KB |
Output is correct |
12 |
Correct |
531 ms |
247372 KB |
Output is correct |
13 |
Correct |
1149 ms |
257956 KB |
Output is correct |
14 |
Correct |
1135 ms |
257976 KB |
Output is correct |
15 |
Correct |
1246 ms |
258016 KB |
Output is correct |
16 |
Correct |
1242 ms |
257952 KB |
Output is correct |
17 |
Correct |
784 ms |
246948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
145980 KB |
Output is correct |
2 |
Correct |
50 ms |
145888 KB |
Output is correct |
3 |
Correct |
58 ms |
145984 KB |
Output is correct |
4 |
Correct |
50 ms |
145868 KB |
Output is correct |
5 |
Correct |
50 ms |
145992 KB |
Output is correct |
6 |
Correct |
53 ms |
145984 KB |
Output is correct |
7 |
Correct |
57 ms |
145888 KB |
Output is correct |
8 |
Correct |
49 ms |
145980 KB |
Output is correct |
9 |
Correct |
50 ms |
145948 KB |
Output is correct |
10 |
Correct |
58 ms |
147560 KB |
Output is correct |
11 |
Correct |
56 ms |
147396 KB |
Output is correct |
12 |
Correct |
56 ms |
147404 KB |
Output is correct |
13 |
Correct |
57 ms |
147708 KB |
Output is correct |
14 |
Correct |
56 ms |
147616 KB |
Output is correct |
15 |
Correct |
57 ms |
147884 KB |
Output is correct |
16 |
Correct |
866 ms |
246932 KB |
Output is correct |
17 |
Correct |
804 ms |
257992 KB |
Output is correct |
18 |
Correct |
658 ms |
250912 KB |
Output is correct |
19 |
Correct |
665 ms |
247772 KB |
Output is correct |
20 |
Correct |
712 ms |
247512 KB |
Output is correct |
21 |
Correct |
799 ms |
247240 KB |
Output is correct |
22 |
Correct |
724 ms |
247144 KB |
Output is correct |
23 |
Correct |
825 ms |
257876 KB |
Output is correct |
24 |
Correct |
503 ms |
250836 KB |
Output is correct |
25 |
Correct |
536 ms |
247656 KB |
Output is correct |
26 |
Correct |
542 ms |
247516 KB |
Output is correct |
27 |
Correct |
531 ms |
247372 KB |
Output is correct |
28 |
Correct |
1149 ms |
257956 KB |
Output is correct |
29 |
Correct |
1135 ms |
257976 KB |
Output is correct |
30 |
Correct |
1246 ms |
258016 KB |
Output is correct |
31 |
Correct |
1242 ms |
257952 KB |
Output is correct |
32 |
Correct |
784 ms |
246948 KB |
Output is correct |
33 |
Correct |
1006 ms |
249748 KB |
Output is correct |
34 |
Correct |
965 ms |
259208 KB |
Output is correct |
35 |
Correct |
1247 ms |
260964 KB |
Output is correct |
36 |
Correct |
1069 ms |
249740 KB |
Output is correct |
37 |
Correct |
1232 ms |
257308 KB |
Output is correct |
38 |
Correct |
1060 ms |
252404 KB |
Output is correct |
39 |
Correct |
935 ms |
269124 KB |
Output is correct |
40 |
Correct |
1403 ms |
287928 KB |
Output is correct |
41 |
Correct |
878 ms |
254728 KB |
Output is correct |
42 |
Correct |
723 ms |
249780 KB |
Output is correct |
43 |
Correct |
1187 ms |
283764 KB |
Output is correct |
44 |
Correct |
1090 ms |
257248 KB |
Output is correct |
45 |
Correct |
708 ms |
271644 KB |
Output is correct |
46 |
Correct |
688 ms |
269088 KB |
Output is correct |
47 |
Correct |
1297 ms |
259024 KB |
Output is correct |
48 |
Correct |
1319 ms |
258044 KB |
Output is correct |
49 |
Correct |
1371 ms |
259000 KB |
Output is correct |
50 |
Correct |
1240 ms |
258008 KB |
Output is correct |
51 |
Correct |
1091 ms |
286880 KB |
Output is correct |
52 |
Correct |
1084 ms |
286612 KB |
Output is correct |