#include "werewolf.h"
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <vector>
#include <functional>
using namespace std;
struct DSU
{
int N;
vector<int> A;
DSU(int _N = 0): N(_N), A(N, -1) {};
int get(int u) { return (A[u] < 0 ? u : A[u] = get(A[u])); }
void set(int v, int u) { A[get(v)] = u; }
};
struct BIT
{
int N;
vector<int> A;
BIT(int _N = 0): N(_N + 5), A(N, 0) {};
void inc(int i)
{
for (; i < N; i += i & (-i)) ++A[i];
}
int get(int l, int r)
{
int ans = 0;
for (--l; l > 0; l -= l & (-l)) ans -= A[l];
for (; r > 0; r -= r & (-r)) ans += A[r];
return ans;
}
};
struct Tree : vector<vector<int>>
{
int LG, N;
vector<vector<int>> anc;
Tree(int _N): N(_N) { assign(N, vector<int>()); }
void build()
{
LG = __lg(N) + 1;
anc.assign(LG, vector<int>(N, -1));
for (int u = 0; u < N; ++u) for (int v : at(u)) anc[0][v] = u;
for (int j = 1; j < LG; ++j) for (int u = 0; u < N; ++u) {
anc[j][u] = (~anc[j - 1][u] ? anc[j - 1][anc[j - 1][u]] : -1);
}
}
void findTour(int u, int &curTime, vector<int> &tin, vector<int> &tout)
{
tin[u] = ++curTime;
for (int v : at(u)) findTour(v, curTime, tin, tout);
tout[u] = curTime;
}
void solve(int u, BIT &bit, vector<vector<pair<int, int>>> &queries, vector<int> &ans, vector<int> &tin, vector<int> &tout)
{
for (auto &q : queries[u]) ans[q.second] = -bit.get(tin[q.first], tout[q.first]);
bit.inc(tin[u]);
for (int v : at(u)) solve(v, bit, queries, ans, tin, tout);
for (auto &q : queries[u]) ans[q.second] += bit.get(tin[q.first], tout[q.first]);
}
};
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) S.size();
vector<vector<int>> adj(N);
for (int i = 0; i < M; ++i) adj[X[i]].push_back(i), adj[Y[i]].push_back(i);
Tree startTree(N), finishTree(N);
DSU dsu;
dsu = DSU(N);
for (int u = N - 1; u >= 0; --u) {
for (int i : adj[u]) {
int v = u ^ X[i] ^ Y[i];
if (v > u) {
if (dsu.get(v) == u) continue;
// cout << "sta " << u << " -> " << dsu.get(v) << endl;
startTree[u].push_back(dsu.get(v));
dsu.set(v, u);
}
}
}
dsu = DSU(N);
for (int u = 0; u < N; ++u) {
for (int i : adj[u]) {
int v = u ^ X[i] ^ Y[i];
if (v < u) {
if (dsu.get(v) == u) continue;
// cout << "fin " << u << " -> " << dsu.get(v) << endl;
finishTree[u].push_back(dsu.get(v));
dsu.set(v, u);
}
}
}
finishTree.build(), startTree.build();
vector<int> tin(N), tout(N);
int curTime = 0;
startTree.findTour(0, curTime, tin, tout);
vector<vector<pair<int, int>>> queries(N);
for (int q = 0; q < Q; ++q) {
// cout << q << " = " << S[q] << ' ' << E[q] << endl;
for (int j = __lg(N); j >= 0; --j) {
if (~startTree.anc[j][S[q]] && startTree.anc[j][S[q]] >= L[q]) S[q] = startTree.anc[j][S[q]];
if (~finishTree.anc[j][E[q]] && finishTree.anc[j][E[q]] <= R[q]) E[q] = finishTree.anc[j][E[q]];
}
assert(S[q] >= L[q] && E[q] <= R[q]);
// cout << q << " = " << S[q] << ' ' << E[q] << endl;
queries[E[q]].emplace_back(S[q], q);
}
BIT bit(N);
vector<int> ans(Q, 0);
finishTree.solve(N - 1, bit, queries, ans, tin, tout);
// for (auto &x : ans) cout << "ans " << x << endl;
for (auto &x : ans) x = !!x;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
7 ms |
1552 KB |
Output is correct |
11 |
Correct |
7 ms |
1536 KB |
Output is correct |
12 |
Correct |
6 ms |
1408 KB |
Output is correct |
13 |
Correct |
8 ms |
1664 KB |
Output is correct |
14 |
Correct |
7 ms |
1664 KB |
Output is correct |
15 |
Correct |
9 ms |
1664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
709 ms |
87244 KB |
Output is correct |
2 |
Correct |
809 ms |
90728 KB |
Output is correct |
3 |
Correct |
764 ms |
87624 KB |
Output is correct |
4 |
Correct |
687 ms |
86504 KB |
Output is correct |
5 |
Correct |
689 ms |
86408 KB |
Output is correct |
6 |
Correct |
734 ms |
86760 KB |
Output is correct |
7 |
Correct |
700 ms |
86120 KB |
Output is correct |
8 |
Correct |
799 ms |
90848 KB |
Output is correct |
9 |
Correct |
595 ms |
87784 KB |
Output is correct |
10 |
Correct |
496 ms |
86124 KB |
Output is correct |
11 |
Correct |
523 ms |
86416 KB |
Output is correct |
12 |
Correct |
539 ms |
86248 KB |
Output is correct |
13 |
Correct |
878 ms |
97000 KB |
Output is correct |
14 |
Correct |
956 ms |
96836 KB |
Output is correct |
15 |
Correct |
869 ms |
97000 KB |
Output is correct |
16 |
Correct |
883 ms |
96996 KB |
Output is correct |
17 |
Correct |
721 ms |
86464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
7 ms |
1552 KB |
Output is correct |
11 |
Correct |
7 ms |
1536 KB |
Output is correct |
12 |
Correct |
6 ms |
1408 KB |
Output is correct |
13 |
Correct |
8 ms |
1664 KB |
Output is correct |
14 |
Correct |
7 ms |
1664 KB |
Output is correct |
15 |
Correct |
9 ms |
1664 KB |
Output is correct |
16 |
Correct |
709 ms |
87244 KB |
Output is correct |
17 |
Correct |
809 ms |
90728 KB |
Output is correct |
18 |
Correct |
764 ms |
87624 KB |
Output is correct |
19 |
Correct |
687 ms |
86504 KB |
Output is correct |
20 |
Correct |
689 ms |
86408 KB |
Output is correct |
21 |
Correct |
734 ms |
86760 KB |
Output is correct |
22 |
Correct |
700 ms |
86120 KB |
Output is correct |
23 |
Correct |
799 ms |
90848 KB |
Output is correct |
24 |
Correct |
595 ms |
87784 KB |
Output is correct |
25 |
Correct |
496 ms |
86124 KB |
Output is correct |
26 |
Correct |
523 ms |
86416 KB |
Output is correct |
27 |
Correct |
539 ms |
86248 KB |
Output is correct |
28 |
Correct |
878 ms |
97000 KB |
Output is correct |
29 |
Correct |
956 ms |
96836 KB |
Output is correct |
30 |
Correct |
869 ms |
97000 KB |
Output is correct |
31 |
Correct |
883 ms |
96996 KB |
Output is correct |
32 |
Correct |
721 ms |
86464 KB |
Output is correct |
33 |
Correct |
852 ms |
87924 KB |
Output is correct |
34 |
Correct |
387 ms |
33928 KB |
Output is correct |
35 |
Correct |
861 ms |
90984 KB |
Output is correct |
36 |
Correct |
787 ms |
88296 KB |
Output is correct |
37 |
Correct |
878 ms |
90100 KB |
Output is correct |
38 |
Correct |
886 ms |
88808 KB |
Output is correct |
39 |
Correct |
938 ms |
99728 KB |
Output is correct |
40 |
Correct |
976 ms |
97592 KB |
Output is correct |
41 |
Correct |
632 ms |
88296 KB |
Output is correct |
42 |
Correct |
540 ms |
86760 KB |
Output is correct |
43 |
Correct |
895 ms |
96360 KB |
Output is correct |
44 |
Correct |
744 ms |
89192 KB |
Output is correct |
45 |
Correct |
748 ms |
100712 KB |
Output is correct |
46 |
Correct |
726 ms |
100328 KB |
Output is correct |
47 |
Correct |
849 ms |
97028 KB |
Output is correct |
48 |
Correct |
853 ms |
96872 KB |
Output is correct |
49 |
Correct |
929 ms |
97128 KB |
Output is correct |
50 |
Correct |
943 ms |
97000 KB |
Output is correct |
51 |
Correct |
774 ms |
96872 KB |
Output is correct |
52 |
Correct |
770 ms |
96872 KB |
Output is correct |