#include "werewolf.h"
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pll;
#define all(x) (x).begin(), (x).end()
#define X first
#define Y second
#define sep ' '
#define debug(x) cerr << #x << ": " << x << endl;
const int MAXN = 1e6 + 10;
const int LOG = 20;
int n, m, q;
namespace DSU_L {
int C[MAXN], F[MAXN], par[MAXN][LOG], sz, T[MAXN], dfs_time, tin[MAXN], tout[MAXN];
vector<int> adj[MAXN];
inline void init() {
sz = n - 1;
T[0] = -1;
for (int i = 0; i < n; i++) {
F[i] = C[i] = i;
T[i] = MAXN;
}
}
inline int find(int v) {
return C[v] == v ? v : C[v] = find(C[v]);
}
inline bool unite(int u, int v, int l) {
u = find(u), v = find(v);
if (u == v) return false;
int fu = F[u], fv = F[v];
sz++;
par[fu][0] = sz;
par[fv][0] = sz;
C[v] = u;
T[sz] = l;
F[u] = sz;
adj[sz].push_back(fu);
adj[sz].push_back(fv);
return true;
}
void dfs(int v) {
tin[v] = ++dfs_time;
for (int u : adj[v])
dfs(u);
tout[v] = dfs_time;
}
inline void init_tree() {
dfs(sz);
par[sz][0] = sz;
for (int i = 1; i < LOG; i++)
for (int v = 0; v <= sz; v++)
par[v][i] = par[par[v][i - 1]][i - 1];
}
inline int max_node(int v, int l) {
for (int i = LOG - 1; i >= 0; i--)
if (T[par[v][i]] >= l)
v = par[v][i];
return v;
}
inline pll get_seg(int v, int l) {
v = max_node(v, l);
return {tin[v], tout[v]};
}
}
namespace DSU_R {
int par[MAXN];
vector<int> C[MAXN];
set<int> st[MAXN];
inline void init() {
for (int i = 0; i < n; i++) {
st[i].insert(DSU_L::tin[i]);
par[i] = i;
C[i].push_back(i);
}
}
inline bool unite(int u, int v) {
u = par[u], v = par[v];
if (u == v) return false;
if (C[u].size() < C[v].size()) swap(u, v);
for (int e : C[v]) {
C[u].push_back(e);
par[e] = u;
}
for (int e : st[v])
st[u].insert(e);
C[v].clear();
st[v].clear();
return true;
}
inline bool get(int v, int l, int r) {
v = par[v];
auto it = st[v].lower_bound(l);
return it != st[v].end() && *it <= r;
}
}
vector<int> ans, adj[MAXN], QR[MAXN];
vector<int> check_validity(int N_, vector<int> X_, vector<int> Y_,
vector<int> S, vector<int> E,
vector<int> L, vector<int> R) {
n = N_;
m = X_.size();
q = S.size();
for (int i = 0; i < m; i++) {
adj[X_[i]].push_back(Y_[i]);
adj[Y_[i]].push_back(X_[i]);
}
DSU_L::init();
for (int v = n - 1; v >= 0; v--) {
for (int u : adj[v]) {
if (u < v) continue;
DSU_L::unite(u, v, v);
}
}
DSU_L::init_tree();
DSU_R::init();
for (int i = 0; i < q; i++)
QR[R[i]].push_back(i);
ans.resize(q);
for (int r = 0; r < n; r++) {
for (int u : adj[r]) {
if (u > r) continue;
DSU_R::unite(r, u);
}
for (int i : QR[r]) {
auto [tl, tr] = DSU_L::get_seg(S[i], L[i]);
ans[i] = DSU_R::get(E[i], tl, tr);
}
}
return ans;
}
// TODO: fill ans
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
141260 KB |
Output is correct |
2 |
Correct |
60 ms |
141164 KB |
Output is correct |
3 |
Correct |
62 ms |
141256 KB |
Output is correct |
4 |
Correct |
62 ms |
141152 KB |
Output is correct |
5 |
Correct |
67 ms |
141260 KB |
Output is correct |
6 |
Correct |
63 ms |
141192 KB |
Output is correct |
7 |
Correct |
62 ms |
141260 KB |
Output is correct |
8 |
Correct |
72 ms |
141240 KB |
Output is correct |
9 |
Correct |
61 ms |
141260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
141260 KB |
Output is correct |
2 |
Correct |
60 ms |
141164 KB |
Output is correct |
3 |
Correct |
62 ms |
141256 KB |
Output is correct |
4 |
Correct |
62 ms |
141152 KB |
Output is correct |
5 |
Correct |
67 ms |
141260 KB |
Output is correct |
6 |
Correct |
63 ms |
141192 KB |
Output is correct |
7 |
Correct |
62 ms |
141260 KB |
Output is correct |
8 |
Correct |
72 ms |
141240 KB |
Output is correct |
9 |
Correct |
61 ms |
141260 KB |
Output is correct |
10 |
Correct |
70 ms |
142492 KB |
Output is correct |
11 |
Correct |
65 ms |
142512 KB |
Output is correct |
12 |
Correct |
66 ms |
142524 KB |
Output is correct |
13 |
Correct |
68 ms |
142496 KB |
Output is correct |
14 |
Correct |
71 ms |
142460 KB |
Output is correct |
15 |
Correct |
67 ms |
142564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
881 ms |
232000 KB |
Output is correct |
2 |
Correct |
583 ms |
232200 KB |
Output is correct |
3 |
Correct |
565 ms |
232552 KB |
Output is correct |
4 |
Correct |
668 ms |
233476 KB |
Output is correct |
5 |
Correct |
586 ms |
233760 KB |
Output is correct |
6 |
Correct |
724 ms |
234908 KB |
Output is correct |
7 |
Correct |
787 ms |
238860 KB |
Output is correct |
8 |
Correct |
553 ms |
232088 KB |
Output is correct |
9 |
Correct |
461 ms |
233224 KB |
Output is correct |
10 |
Correct |
498 ms |
231308 KB |
Output is correct |
11 |
Correct |
485 ms |
231676 KB |
Output is correct |
12 |
Correct |
621 ms |
236044 KB |
Output is correct |
13 |
Correct |
653 ms |
238828 KB |
Output is correct |
14 |
Correct |
631 ms |
238812 KB |
Output is correct |
15 |
Correct |
620 ms |
238784 KB |
Output is correct |
16 |
Correct |
601 ms |
238724 KB |
Output is correct |
17 |
Correct |
880 ms |
238760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
141260 KB |
Output is correct |
2 |
Correct |
60 ms |
141164 KB |
Output is correct |
3 |
Correct |
62 ms |
141256 KB |
Output is correct |
4 |
Correct |
62 ms |
141152 KB |
Output is correct |
5 |
Correct |
67 ms |
141260 KB |
Output is correct |
6 |
Correct |
63 ms |
141192 KB |
Output is correct |
7 |
Correct |
62 ms |
141260 KB |
Output is correct |
8 |
Correct |
72 ms |
141240 KB |
Output is correct |
9 |
Correct |
61 ms |
141260 KB |
Output is correct |
10 |
Correct |
70 ms |
142492 KB |
Output is correct |
11 |
Correct |
65 ms |
142512 KB |
Output is correct |
12 |
Correct |
66 ms |
142524 KB |
Output is correct |
13 |
Correct |
68 ms |
142496 KB |
Output is correct |
14 |
Correct |
71 ms |
142460 KB |
Output is correct |
15 |
Correct |
67 ms |
142564 KB |
Output is correct |
16 |
Correct |
881 ms |
232000 KB |
Output is correct |
17 |
Correct |
583 ms |
232200 KB |
Output is correct |
18 |
Correct |
565 ms |
232552 KB |
Output is correct |
19 |
Correct |
668 ms |
233476 KB |
Output is correct |
20 |
Correct |
586 ms |
233760 KB |
Output is correct |
21 |
Correct |
724 ms |
234908 KB |
Output is correct |
22 |
Correct |
787 ms |
238860 KB |
Output is correct |
23 |
Correct |
553 ms |
232088 KB |
Output is correct |
24 |
Correct |
461 ms |
233224 KB |
Output is correct |
25 |
Correct |
498 ms |
231308 KB |
Output is correct |
26 |
Correct |
485 ms |
231676 KB |
Output is correct |
27 |
Correct |
621 ms |
236044 KB |
Output is correct |
28 |
Correct |
653 ms |
238828 KB |
Output is correct |
29 |
Correct |
631 ms |
238812 KB |
Output is correct |
30 |
Correct |
620 ms |
238784 KB |
Output is correct |
31 |
Correct |
601 ms |
238724 KB |
Output is correct |
32 |
Correct |
880 ms |
238760 KB |
Output is correct |
33 |
Correct |
963 ms |
234188 KB |
Output is correct |
34 |
Correct |
247 ms |
174356 KB |
Output is correct |
35 |
Correct |
1004 ms |
234860 KB |
Output is correct |
36 |
Correct |
893 ms |
234164 KB |
Output is correct |
37 |
Correct |
853 ms |
234352 KB |
Output is correct |
38 |
Correct |
853 ms |
234096 KB |
Output is correct |
39 |
Correct |
801 ms |
231572 KB |
Output is correct |
40 |
Correct |
802 ms |
242932 KB |
Output is correct |
41 |
Correct |
790 ms |
234224 KB |
Output is correct |
42 |
Correct |
728 ms |
235232 KB |
Output is correct |
43 |
Correct |
958 ms |
237972 KB |
Output is correct |
44 |
Correct |
846 ms |
234424 KB |
Output is correct |
45 |
Correct |
613 ms |
233304 KB |
Output is correct |
46 |
Correct |
658 ms |
232368 KB |
Output is correct |
47 |
Correct |
704 ms |
238952 KB |
Output is correct |
48 |
Correct |
668 ms |
238704 KB |
Output is correct |
49 |
Correct |
636 ms |
238944 KB |
Output is correct |
50 |
Correct |
664 ms |
238692 KB |
Output is correct |
51 |
Correct |
744 ms |
243476 KB |
Output is correct |
52 |
Correct |
702 ms |
243388 KB |
Output is correct |