#include "werewolf.h"
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
struct dsu {
vector<int> p;
dsu() = default;
dsu(int n) {
p.resize(n);
iota(p.begin(), p.end(), 0);
}
int get(int a) {
return a == p[a] ? a : p[a] = get(p[a]);
}
bool mrg(int a, int b) {
a = get(a);
b = get(b);
if (a == b) {
return false;
}
p[b] = a;
return true;
}
bool same(int a, int b) {
return get(a) == get(b);
}
};
struct SegTree {
int sz = 1;
vector<vector<int>> t;
SegTree() = default;
SegTree(const vector<int> &a) {
int n = (int) a.size();
sz = 1;
while (sz < n) sz <<= 1;
t.assign(sz << 1, {});
for (int i = 0; i < n; ++i) {
t[i + sz] = {a[i]};
}
for (int i = sz - 1; i > 0; --i) {
t[i].resize(t[i << 1].size() + t[i << 1 | 1].size());
merge(t[i << 1].begin(), t[i << 1].end(), t[i << 1 | 1].begin(), t[i << 1 | 1].end(), t[i].begin());
}
}
int get(int l, int r, int val, int x, int lx, int rx) {
if (l >= rx || lx >= r) return inf;
if (l <= lx && rx <= r) {
auto it = lower_bound(t[x].begin(), t[x].end(), val);
if (it == t[x].end()) {
return inf;
} else {
return *it;
}
}
int m = (lx + rx) >> 1;
return min(get(l, r, val, x << 1, lx, m), get(l, r, val, x << 1 | 1, m, rx));
}
};
namespace Werewolf {
const int N = 600000, logn = 20;
int jump[logn][N], weight[N], tin[N], tout[N], T = 0;
vector<int> g[N], ord;
int n, m;
void init(vector<array<int, 3>> e, int _n) {
n = _n;
m = (int)e.size();
dsu d(n + m);
int last = n;
assert(*max_element(tin, tin + N) == 0);
for (auto [w, a, b] : e) {
if (d.same(a, b)) {
g[last].push_back(d.get(a));
d.mrg(last, a);
} else {
g[last].push_back(d.get(a));
g[last].push_back(d.get(b));
d.mrg(last, a);
d.mrg(last, b);
}
weight[last - n] = w;
last += 1;
}
function<void(int)> dfs = [&](int v) {
tin[v] = T;
if (v < n) {
ord.push_back(v);
T++;
}
for (int to : g[v]) {
jump[0][to] = v;
dfs(to);
}
tout[v] = T;
};
memset(tin, -1, sizeof(tin));
for (int i = last - 1; i > -1; --i) {
if (tin[i] == -1) {
jump[0][i] = i;
dfs(i);
}
}
for (int j = 1; j < logn; ++j) {
for (int i = 0; i < n + m; ++i) {
jump[j][i] = jump[j - 1][jump[j - 1][i]];
}
}
}
pair<int, int> get(int v, int w) {
for (int j = logn - 1; j > -1; --j) {
if (weight[jump[j][v] - n] <= w) {
v = jump[j][v];
}
}
return {tin[v], tout[v]};
}
};
namespace Human {
const int N = 600000, logn = 20;
int jump[logn][N], weight[N], tin[N], tout[N], T = 0;
vector<int> g[N], ord;
int n, m;
void init(vector<array<int, 3>> e, int _n) {
n = _n;
m = (int)e.size();
dsu d(n + m);
int last = n;
assert(*max_element(tin, tin + N) == 0);
for (auto [w, a, b] : e) {
if (d.same(a, b)) {
g[last].push_back(d.get(a));
d.mrg(last, a);
} else {
g[last].push_back(d.get(a));
g[last].push_back(d.get(b));
d.mrg(last, a);
d.mrg(last, b);
}
weight[last - n] = w;
last += 1;
}
function<void(int)> dfs = [&](int v) {
tin[v] = T;
if (v < n) {
ord.push_back(v);
T++;
}
for (int to : g[v]) {
jump[0][to] = v;
dfs(to);
}
tout[v] = T;
};
memset(tin, -1, sizeof(tin));
for (int i = last - 1; i > -1; --i) {
if (tin[i] == -1) {
jump[0][i] = i;
dfs(i);
}
}
for (int j = 1; j < logn; ++j) {
for (int i = 0; i < n + m; ++i) {
jump[j][i] = jump[j - 1][jump[j - 1][i]];
}
}
}
pair<int, int> get(int v, int w) {
for (int j = logn - 1; j > -1; --j) {
if (weight[jump[j][v] - n] >= w) {
v = jump[j][v];
}
}
return {tin[v], tout[v]};
}
} //human
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 = (int)X.size(), q = (int)e.size();
vector<array<int, 3>> eWerewolf(m), eHuman(m);
for (int i = 0; i < m; ++i) {
if (X[i] > Y[i]) {
swap(X[i], Y[i]);
}
eWerewolf[i] = {Y[i], X[i], Y[i]};
eHuman[i] = {X[i], X[i], Y[i]};
}
sort(eHuman.begin(), eHuman.end(), greater());
sort(eWerewolf.begin(), eWerewolf.end());
Werewolf::init(eWerewolf, n);
Human::init(eHuman, n);
vector<int> a(n);
for (int i = 0; i < n; ++i) {
a[i] = Human::tin[Werewolf::ord[i]];
}
SegTree st(a);
vector<int> ans(q);
for (int i = 0; i < q; ++i) {
auto [hL, hR] = Human::get(s[i], L[i]);
auto [wL, wR] = Werewolf::get(e[i], R[i]);
int x = st.get(wL, wR, hL, 1, 0, st.sz);
ans[i] = (x < hR);
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
33572 KB |
Output is correct |
2 |
Correct |
26 ms |
33488 KB |
Output is correct |
3 |
Correct |
22 ms |
33600 KB |
Output is correct |
4 |
Correct |
23 ms |
33436 KB |
Output is correct |
5 |
Correct |
23 ms |
33492 KB |
Output is correct |
6 |
Correct |
21 ms |
33532 KB |
Output is correct |
7 |
Correct |
20 ms |
33612 KB |
Output is correct |
8 |
Correct |
25 ms |
33484 KB |
Output is correct |
9 |
Correct |
22 ms |
33516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
33572 KB |
Output is correct |
2 |
Correct |
26 ms |
33488 KB |
Output is correct |
3 |
Correct |
22 ms |
33600 KB |
Output is correct |
4 |
Correct |
23 ms |
33436 KB |
Output is correct |
5 |
Correct |
23 ms |
33492 KB |
Output is correct |
6 |
Correct |
21 ms |
33532 KB |
Output is correct |
7 |
Correct |
20 ms |
33612 KB |
Output is correct |
8 |
Correct |
25 ms |
33484 KB |
Output is correct |
9 |
Correct |
22 ms |
33516 KB |
Output is correct |
10 |
Correct |
33 ms |
35660 KB |
Output is correct |
11 |
Correct |
33 ms |
35536 KB |
Output is correct |
12 |
Correct |
32 ms |
35488 KB |
Output is correct |
13 |
Correct |
32 ms |
35792 KB |
Output is correct |
14 |
Correct |
28 ms |
35712 KB |
Output is correct |
15 |
Correct |
36 ms |
36468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
925 ms |
168392 KB |
Output is correct |
2 |
Correct |
1354 ms |
177088 KB |
Output is correct |
3 |
Correct |
1331 ms |
173048 KB |
Output is correct |
4 |
Correct |
1248 ms |
171236 KB |
Output is correct |
5 |
Correct |
1207 ms |
171144 KB |
Output is correct |
6 |
Correct |
1110 ms |
170940 KB |
Output is correct |
7 |
Correct |
796 ms |
170812 KB |
Output is correct |
8 |
Correct |
1367 ms |
177056 KB |
Output is correct |
9 |
Correct |
961 ms |
173136 KB |
Output is correct |
10 |
Correct |
520 ms |
171252 KB |
Output is correct |
11 |
Correct |
600 ms |
171140 KB |
Output is correct |
12 |
Correct |
805 ms |
170936 KB |
Output is correct |
13 |
Correct |
1513 ms |
177092 KB |
Output is correct |
14 |
Correct |
1351 ms |
177852 KB |
Output is correct |
15 |
Correct |
1187 ms |
177864 KB |
Output is correct |
16 |
Correct |
1324 ms |
177888 KB |
Output is correct |
17 |
Correct |
827 ms |
171588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
33572 KB |
Output is correct |
2 |
Correct |
26 ms |
33488 KB |
Output is correct |
3 |
Correct |
22 ms |
33600 KB |
Output is correct |
4 |
Correct |
23 ms |
33436 KB |
Output is correct |
5 |
Correct |
23 ms |
33492 KB |
Output is correct |
6 |
Correct |
21 ms |
33532 KB |
Output is correct |
7 |
Correct |
20 ms |
33612 KB |
Output is correct |
8 |
Correct |
25 ms |
33484 KB |
Output is correct |
9 |
Correct |
22 ms |
33516 KB |
Output is correct |
10 |
Correct |
33 ms |
35660 KB |
Output is correct |
11 |
Correct |
33 ms |
35536 KB |
Output is correct |
12 |
Correct |
32 ms |
35488 KB |
Output is correct |
13 |
Correct |
32 ms |
35792 KB |
Output is correct |
14 |
Correct |
28 ms |
35712 KB |
Output is correct |
15 |
Correct |
36 ms |
36468 KB |
Output is correct |
16 |
Correct |
925 ms |
168392 KB |
Output is correct |
17 |
Correct |
1354 ms |
177088 KB |
Output is correct |
18 |
Correct |
1331 ms |
173048 KB |
Output is correct |
19 |
Correct |
1248 ms |
171236 KB |
Output is correct |
20 |
Correct |
1207 ms |
171144 KB |
Output is correct |
21 |
Correct |
1110 ms |
170940 KB |
Output is correct |
22 |
Correct |
796 ms |
170812 KB |
Output is correct |
23 |
Correct |
1367 ms |
177056 KB |
Output is correct |
24 |
Correct |
961 ms |
173136 KB |
Output is correct |
25 |
Correct |
520 ms |
171252 KB |
Output is correct |
26 |
Correct |
600 ms |
171140 KB |
Output is correct |
27 |
Correct |
805 ms |
170936 KB |
Output is correct |
28 |
Correct |
1513 ms |
177092 KB |
Output is correct |
29 |
Correct |
1351 ms |
177852 KB |
Output is correct |
30 |
Correct |
1187 ms |
177864 KB |
Output is correct |
31 |
Correct |
1324 ms |
177888 KB |
Output is correct |
32 |
Correct |
827 ms |
171588 KB |
Output is correct |
33 |
Correct |
1278 ms |
173184 KB |
Output is correct |
34 |
Correct |
1145 ms |
185352 KB |
Output is correct |
35 |
Correct |
1492 ms |
183456 KB |
Output is correct |
36 |
Correct |
1008 ms |
174156 KB |
Output is correct |
37 |
Correct |
1425 ms |
179360 KB |
Output is correct |
38 |
Correct |
1121 ms |
176628 KB |
Output is correct |
39 |
Correct |
1165 ms |
184452 KB |
Output is correct |
40 |
Correct |
1211 ms |
233424 KB |
Output is correct |
41 |
Correct |
1096 ms |
177004 KB |
Output is correct |
42 |
Correct |
561 ms |
174152 KB |
Output is correct |
43 |
Correct |
1666 ms |
211920 KB |
Output is correct |
44 |
Correct |
1298 ms |
179320 KB |
Output is correct |
45 |
Correct |
963 ms |
187488 KB |
Output is correct |
46 |
Correct |
977 ms |
184452 KB |
Output is correct |
47 |
Correct |
1132 ms |
179404 KB |
Output is correct |
48 |
Correct |
1234 ms |
178000 KB |
Output is correct |
49 |
Correct |
1127 ms |
179912 KB |
Output is correct |
50 |
Correct |
1206 ms |
178536 KB |
Output is correct |
51 |
Correct |
1125 ms |
233404 KB |
Output is correct |
52 |
Correct |
1082 ms |
233364 KB |
Output is correct |