#include <bits/stdc++.h>
#include "catdog.h"
using namespace std;
using ll = long long;
using pl = pair<ll, ll>;
#define vt vector
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define F0R(i, b) FOR (i, 0, b)
#define endl '\n'
#define debug(x) do{auto _x = x; cerr << #x << " = " << _x << endl;} while(0)
const ll INF = 1e9;
pl clamp(pl a) {
return {min(a.f, a.s + 1), min(a.f + 1, a.s)};
}
pl operator+(const pl& a, const pl& b) {
return {a.f + b.f, a.s + b.s};
}
pl operator-(const pl& a, const pl& b) {
return {a.f - b.f, a.s - b.s};
}
struct Node {
pl actual, cat, same, dog; // actual sum, extra cat, same, extra dog
void build() {
same = clamp(actual);
cat = clamp({actual.f + 1, actual.s});
dog = clamp({actual.f, actual.s + 1});
}
inline pl cmb(pl a) const {
if (a.f == a.s) return {a.f + same.f, a.s + same.s};
else if (a.f > a.s) return {a.s + cat.f, a.s + cat.s};
else return {a.f + dog.f, a.f + dog.s};
}
Node operator+(const Node& b) const {
return {
actual + b.actual,
b.cmb(cat),
b.cmb(same),
b.cmb(dog)
};
}
void operator+=(pl b) {
actual.f += b.f;
actual.s += b.s;
}
void operator-=(pl b) {
actual.f -= b.f;
actual.s -= b.s;
}
void print() {
cerr << '{' << actual.f << ", " << actual.s << ": " << cat.f << ", " << cat.s << ", " << same.f << ", " << same.s << ", " << dog.f << ", " << dog.s << "} \n";
}
};
const Node NID = {
{0, 0},
{1, 0},
{0, 0},
{0, 1}
};
// cursed reverse segtree
struct SegTree {
int n;
vt<Node> seg;
void init(int _n) {
for (n = 1; n < _n; n *= 2);
seg.resize(2 * n, NID);
}
Node query(int l, int r) {
Node lhs = NID, rhs = NID;
for (l += n, r += n + 1; l < r; l /= 2, r /= 2) {
if (l & 1) lhs = seg[l++] + lhs;
if (r & 1) rhs = rhs + seg[--r];
}
return rhs + lhs;
}
void pull(int i) {
i += n;
while (i > 1) i /= 2, seg[i] = seg[2 * i + 1] + seg[2 * i];
}
void upd(int i, pl val) {
seg[i += n] += val;
seg[i].build();
pull(i - n);
}
Node& operator[](int i) {
return seg[i + n];
}
};
int n;
vt<int> state;
struct HLD {
int n, t;
vt<vt<int>> adj;
vt<int> pos, root, sz, depth, par, leaf;
vt<pl> agg;
SegTree seg;
void init(int _n) {
n = _n;
adj.resize(n);
agg.resize(n, {-1, -1});
pos = root = sz = depth = par = leaf = vt<int>(n);
seg.init(n);
}
void ae(int u, int v) {
adj[u].pb(v);
adj[v].pb(u);
}
int dfs_sz(int u) {
sz[u] = 1;
for (int& v : adj[u]) {
depth[v] = depth[u] + 1;
par[v] = u;
adj[v].erase(find(all(adj[v]), u));
sz[u] += dfs_sz(v);
if (sz[v] > sz[adj[u][0]]) swap(v, adj[u][0]);
}
return sz[u];
}
int dfs_hld(int u, int rt) {
root[u] = rt;
pos[u] = t++;
int res = -1;
for (int v : adj[u]) {
if (v == adj[u][0]) res = dfs_hld(v, rt);
else dfs_hld(v, v);
}
if (res == -1) leaf[u] = u;
else leaf[u] = res;
return leaf[u];
}
void gen(int r = 0) {
t = 0;
par[r] = -1;
dfs_sz(r);
dfs_hld(r, r);
F0R (u, n) {
if (root[u] == u) agg[u] = {0, 0};
}
}
void prop(int u) {
while (true) {
u = leaf[u]; // go to bottom of heavy path
// calc updates subtree aggregate
int rt = root[u];
Node res = seg.query(pos[rt], pos[u]);
// update aggregate and its parent
int p = par[rt];
if (p >= 0) seg[pos[p]] -= agg[rt];
agg[rt] = res.same;
if (p == -1) return;
seg[pos[p]] += res.same;
seg[pos[p]].build();
seg.pull(pos[p]);
u = p; // go to parent
}
}
ll update(int u, pl v) { // do stuff to update u
seg.upd(pos[u], v);
prop(u);
return min(agg[0].f, agg[0].s);
}
void print() {
F0R (u, n) {
cerr << u << ": ";
seg[pos[u]].print();
}
cerr << "aggregates\n";
F0R (u, n) {
cerr << u << ": " << agg[u].f << " " << agg[u].s << endl;
}
}
} hld;
/*
let dp[u] = {min cost to make u's subtree cat safe, min cost to make u's subtree dog safe}
the difference between them is at most 1
keep track of all the cases (extra cat, same, extra dog)
each node stores the sum of everything EXCEPT its heavy path
heavy path roots store its subtree aggregate
when performing an update, modify the current node
walk up from the BASE of each heavy path up to the root, updating subtree aggregates and their parents
*/
void initialize(int N, vt<int> A, vt<int> B) {
n = N;
state.resize(n);
hld.init(n);
F0R (i, n - 1) {
int u = A[i] - 1, v = B[i] - 1;
hld.ae(u, v);
}
hld.gen();
}
int cat(int v) {
v--;
state[v] = 1;
return hld.update(v, {INF, 0});
}
int dog(int v) {
v--;
state[v] = 2;
return hld.update(v, {0, INF});
}
int neighbor(int v) {
v--;
pl sus;
if (state[v] == 1) sus = {-INF, 0};
else sus = {0, -INF};
state[v] = 0;
return hld.update(v, sus);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
500 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
688 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
360 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
500 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
688 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
360 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
2 ms |
500 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
864 KB |
Output is correct |
29 |
Correct |
1 ms |
864 KB |
Output is correct |
30 |
Correct |
1 ms |
352 KB |
Output is correct |
31 |
Correct |
1 ms |
608 KB |
Output is correct |
32 |
Correct |
1 ms |
436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
500 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
688 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
360 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
2 ms |
500 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
864 KB |
Output is correct |
29 |
Correct |
1 ms |
864 KB |
Output is correct |
30 |
Correct |
1 ms |
352 KB |
Output is correct |
31 |
Correct |
1 ms |
608 KB |
Output is correct |
32 |
Correct |
1 ms |
436 KB |
Output is correct |
33 |
Correct |
128 ms |
16292 KB |
Output is correct |
34 |
Correct |
50 ms |
16944 KB |
Output is correct |
35 |
Correct |
106 ms |
14496 KB |
Output is correct |
36 |
Correct |
201 ms |
29804 KB |
Output is correct |
37 |
Correct |
12 ms |
8280 KB |
Output is correct |
38 |
Correct |
212 ms |
31044 KB |
Output is correct |
39 |
Correct |
220 ms |
31040 KB |
Output is correct |
40 |
Correct |
209 ms |
31044 KB |
Output is correct |
41 |
Correct |
216 ms |
31056 KB |
Output is correct |
42 |
Correct |
263 ms |
31220 KB |
Output is correct |
43 |
Correct |
204 ms |
31040 KB |
Output is correct |
44 |
Correct |
236 ms |
31212 KB |
Output is correct |
45 |
Correct |
198 ms |
31072 KB |
Output is correct |
46 |
Correct |
227 ms |
31088 KB |
Output is correct |
47 |
Correct |
211 ms |
31140 KB |
Output is correct |
48 |
Correct |
68 ms |
27108 KB |
Output is correct |
49 |
Correct |
78 ms |
29644 KB |
Output is correct |
50 |
Correct |
25 ms |
7260 KB |
Output is correct |
51 |
Correct |
32 ms |
13624 KB |
Output is correct |
52 |
Correct |
16 ms |
7004 KB |
Output is correct |
53 |
Correct |
97 ms |
29776 KB |
Output is correct |
54 |
Correct |
64 ms |
14416 KB |
Output is correct |
55 |
Correct |
181 ms |
27048 KB |
Output is correct |
56 |
Correct |
108 ms |
15584 KB |
Output is correct |
57 |
Correct |
139 ms |
29264 KB |
Output is correct |
58 |
Correct |
22 ms |
14040 KB |
Output is correct |
59 |
Correct |
33 ms |
9052 KB |
Output is correct |
60 |
Correct |
65 ms |
28432 KB |
Output is correct |
61 |
Correct |
71 ms |
28896 KB |
Output is correct |
62 |
Correct |
45 ms |
26584 KB |
Output is correct |
63 |
Correct |
38 ms |
28220 KB |
Output is correct |
64 |
Correct |
41 ms |
35152 KB |
Output is correct |
65 |
Correct |
62 ms |
56876 KB |
Output is correct |
66 |
Correct |
32 ms |
12636 KB |
Output is correct |
67 |
Correct |
63 ms |
45132 KB |
Output is correct |
68 |
Correct |
97 ms |
55232 KB |
Output is correct |
69 |
Correct |
17 ms |
4796 KB |
Output is correct |
70 |
Correct |
4 ms |
860 KB |
Output is correct |
71 |
Correct |
39 ms |
28564 KB |
Output is correct |
72 |
Correct |
62 ms |
54096 KB |
Output is correct |
73 |
Correct |
134 ms |
69744 KB |
Output is correct |
74 |
Correct |
112 ms |
51580 KB |
Output is correct |
75 |
Correct |
102 ms |
68944 KB |
Output is correct |
76 |
Correct |
109 ms |
62556 KB |
Output is correct |
77 |
Correct |
128 ms |
53532 KB |
Output is correct |