#include "werewolf.h"
#include <bits/stdc++.h>
using namespace std;
namespace {
template <typename Fun>
struct YCombinator {
template <typename T>
YCombinator(T &&_fun) : fun(forward<T>(_fun)) {}
template <typename... Args>
decltype(auto) operator()(Args &&...args) {
return fun(ref(*this), forward<Args>(args)...);
}
private:
Fun fun;
};
template <typename T>
YCombinator(T &&) -> YCombinator<decay_t<T>>;
template <typename S, typename Op = plus<S>>
struct FenwickTree {
explicit FenwickTree(int _n = 0, S _e = S(), Op _op = Op()) : n(_n), e(_e), op(_op), tree(n + 1, e) {}
S query(int p) const {
S x = e;
for (; p; p -= p & -p) {
x = op(tree[p], x);
}
return x;
}
S query() const {
return query(n);
}
void modify(int p, S x) {
for (p++; p <= n; p += p & -p) {
tree[p] = op(tree[p], x);
}
}
int size() const {
return n;
}
private:
int n;
S e;
Op op;
vector<S> tree;
};
} // namespace
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 n = N;
int m = (int)X.size();
int q = (int)S.size();
vector<int> a, b;
vector<array<int, 2>> sub_a(q), sub_b(q);
vector<vector<int>> qrys_l(n), qrys_r(n);
for (int i = 0; i < q; i++) {
qrys_l[L[i]].push_back(i);
qrys_r[R[i]].push_back(i);
}
{
vector<vector<int>> adj(n);
for (int i = 0; i < m; i++) {
int u = X[i];
int v = Y[i];
if (u > v) {
swap(u, v);
}
adj[u].push_back(v);
}
vector<vector<int>> krt(2 * n);
vector<int> node(q);
vector<int> root(2 * n);
iota(root.begin(), root.end(), 0);
YCombinator get_root = [&](auto self, int u) -> int {
return root[u] == u ? u : root[u] = self(root[u]);
};
for (int u = n - 1; u >= 0; u--) {
int x = 2 * n - (u + 1);
krt[x].push_back(u);
root[u] = x;
vector<int> ch;
for (int v : adj[u]) {
ch.push_back(get_root(v));
}
sort(ch.begin(), ch.end());
ch.erase(unique(ch.begin(), ch.end()), ch.end());
for (int y : ch) {
krt[x].push_back(y);
root[y] = x;
}
for (int i : qrys_l[u]) {
node[i] = get_root(S[i]);
}
}
vector<int> tin(2 * n), tout(2 * n);
{
int timer = 0;
YCombinator([&](auto self, int x) -> void {
tin[x] = timer;
if (x < n) {
a.push_back(x);
timer++;
}
for (int y : krt[x]) {
self(y);
}
tout[x] = timer;
})(2 * n - 1);
}
for (int i = 0; i < q; i++) {
sub_a[i] = {tin[node[i]], tout[node[i]]};
}
}
{
vector<vector<int>> adj(n);
for (int i = 0; i < m; i++) {
int u = X[i];
int v = Y[i];
if (u < v) {
swap(u, v);
}
adj[u].push_back(v);
}
vector<vector<int>> krt(2 * n);
vector<int> node(q);
vector<int> root(2 * n);
iota(root.begin(), root.end(), 0);
YCombinator get_root = [&](auto self, int u) -> int {
return root[u] == u ? u : root[u] = self(root[u]);
};
for (int u = 0; u < N; u++) {
int x = n + u;
krt[x].push_back(u);
root[u] = x;
vector<int> ch;
for (int v : adj[u]) {
ch.push_back(get_root(v));
}
sort(ch.begin(), ch.end());
ch.erase(unique(ch.begin(), ch.end()), ch.end());
for (int y : ch) {
krt[x].push_back(y);
root[y] = x;
}
for (int i : qrys_r[u]) {
node[i] = get_root(E[i]);
}
}
vector<int> tin(2 * n), tout(2 * n);
{
int timer = 0;
YCombinator([&](auto self, int x) -> void {
tin[x] = timer;
if (x < n) {
b.push_back(x);
timer++;
}
for (int y : krt[x]) {
self(y);
}
tout[x] = timer;
})(2 * n - 1);
}
for (int i = 0; i < q; i++) {
sub_b[i] = {tin[node[i]], tout[node[i]]};
}
}
vector<int> ans(q);
{
vector<int> idx(n);
for (int i = 0; i < n; i++) {
idx[a[i]] = i;
}
vector<array<vector<int>, 2>> todo(n);
for (int i = 0; i < q; i++) {
todo[sub_b[i][0]][0].push_back(i);
todo[sub_b[i][1] - 1][1].push_back(i);
}
FenwickTree<int> fenw(n);
for (int i = 0; i < n; i++) {
for (int j : todo[i][0]) {
ans[j] -= fenw.query(sub_a[j][1]) - fenw.query(sub_a[j][0]);
}
fenw.modify(idx[b[i]], 1);
for (int j : todo[i][1]) {
ans[j] += fenw.query(sub_a[j][1]) - fenw.query(sub_a[j][0]);
}
}
}
for (int i = 0; i < q; i++) {
if (ans[i] > 0) {
ans[i] = 1;
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
4 ms |
1368 KB |
Output is correct |
11 |
Correct |
4 ms |
1224 KB |
Output is correct |
12 |
Correct |
4 ms |
1360 KB |
Output is correct |
13 |
Correct |
4 ms |
1360 KB |
Output is correct |
14 |
Correct |
4 ms |
1484 KB |
Output is correct |
15 |
Correct |
4 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
371 ms |
60976 KB |
Output is correct |
2 |
Correct |
357 ms |
63536 KB |
Output is correct |
3 |
Correct |
335 ms |
62048 KB |
Output is correct |
4 |
Correct |
314 ms |
61216 KB |
Output is correct |
5 |
Correct |
329 ms |
61528 KB |
Output is correct |
6 |
Correct |
328 ms |
61024 KB |
Output is correct |
7 |
Correct |
298 ms |
58936 KB |
Output is correct |
8 |
Correct |
309 ms |
63584 KB |
Output is correct |
9 |
Correct |
271 ms |
60764 KB |
Output is correct |
10 |
Correct |
275 ms |
60008 KB |
Output is correct |
11 |
Correct |
273 ms |
60204 KB |
Output is correct |
12 |
Correct |
334 ms |
60768 KB |
Output is correct |
13 |
Correct |
322 ms |
66664 KB |
Output is correct |
14 |
Correct |
311 ms |
66664 KB |
Output is correct |
15 |
Correct |
286 ms |
66604 KB |
Output is correct |
16 |
Correct |
326 ms |
66628 KB |
Output is correct |
17 |
Correct |
285 ms |
58716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
4 ms |
1368 KB |
Output is correct |
11 |
Correct |
4 ms |
1224 KB |
Output is correct |
12 |
Correct |
4 ms |
1360 KB |
Output is correct |
13 |
Correct |
4 ms |
1360 KB |
Output is correct |
14 |
Correct |
4 ms |
1484 KB |
Output is correct |
15 |
Correct |
4 ms |
1372 KB |
Output is correct |
16 |
Correct |
371 ms |
60976 KB |
Output is correct |
17 |
Correct |
357 ms |
63536 KB |
Output is correct |
18 |
Correct |
335 ms |
62048 KB |
Output is correct |
19 |
Correct |
314 ms |
61216 KB |
Output is correct |
20 |
Correct |
329 ms |
61528 KB |
Output is correct |
21 |
Correct |
328 ms |
61024 KB |
Output is correct |
22 |
Correct |
298 ms |
58936 KB |
Output is correct |
23 |
Correct |
309 ms |
63584 KB |
Output is correct |
24 |
Correct |
271 ms |
60764 KB |
Output is correct |
25 |
Correct |
275 ms |
60008 KB |
Output is correct |
26 |
Correct |
273 ms |
60204 KB |
Output is correct |
27 |
Correct |
334 ms |
60768 KB |
Output is correct |
28 |
Correct |
322 ms |
66664 KB |
Output is correct |
29 |
Correct |
311 ms |
66664 KB |
Output is correct |
30 |
Correct |
286 ms |
66604 KB |
Output is correct |
31 |
Correct |
326 ms |
66628 KB |
Output is correct |
32 |
Correct |
285 ms |
58716 KB |
Output is correct |
33 |
Correct |
368 ms |
61572 KB |
Output is correct |
34 |
Correct |
153 ms |
22808 KB |
Output is correct |
35 |
Correct |
377 ms |
63336 KB |
Output is correct |
36 |
Correct |
335 ms |
61524 KB |
Output is correct |
37 |
Correct |
400 ms |
62880 KB |
Output is correct |
38 |
Correct |
344 ms |
62048 KB |
Output is correct |
39 |
Correct |
360 ms |
69992 KB |
Output is correct |
40 |
Correct |
342 ms |
64328 KB |
Output is correct |
41 |
Correct |
351 ms |
62304 KB |
Output is correct |
42 |
Correct |
286 ms |
60484 KB |
Output is correct |
43 |
Correct |
379 ms |
66364 KB |
Output is correct |
44 |
Correct |
322 ms |
62820 KB |
Output is correct |
45 |
Correct |
304 ms |
68988 KB |
Output is correct |
46 |
Correct |
279 ms |
68972 KB |
Output is correct |
47 |
Correct |
314 ms |
66660 KB |
Output is correct |
48 |
Correct |
329 ms |
66664 KB |
Output is correct |
49 |
Correct |
345 ms |
66720 KB |
Output is correct |
50 |
Correct |
307 ms |
66600 KB |
Output is correct |
51 |
Correct |
349 ms |
62844 KB |
Output is correct |
52 |
Correct |
347 ms |
62564 KB |
Output is correct |