char mem[(int)1e8];
size_t cur = 0;
void *operator new(size_t n) {return mem + (cur += n) - n;}
void operator delete(void *) noexcept {}
void operator delete(void *, size_t) noexcept {}
#include <bits/stdc++.h>
void initialize(int N, std::vector<int> A, std::vector<int> B);
int cat(int v), dog(int v), neighbor(int v);
struct segTree {
int n;
using node = std::array<std::array<int, 2>, 2>;
std::vector<node> t;
node merge(node a, node b) {
node c;
for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) c[i][j] = std::min({a[i][0] + b[0][j], a[i][1] + b[1][j]});
return c;
}
segTree() = default;
segTree(int n) {
while (__builtin_popcount(n) != 1) ++n;
this->n = n, t = std::vector<node>(n * 2);
for (auto&f: t) f[0][1] = f[1][0] = 1;
}
void modify(int i, int x, int y) {
i += n, t[i][0][0] += x, t[i][0][1] += x, t[i][1][0] += y, t[i][1][1] += y;
while (i /= 2) t[i] = merge(t[i * 2], t[i * 2 + 1]);
}
std::pair<int, int> get() {
int x = std::min(t[1][0][0], t[1][0][1]), y = std::min(t[1][1][0], t[1][1][1]);
return {std::min(x, y + 1), std::min(x + 1, y)};
}
};
segTree ok[100005];
int pos[100005], p[100005], f[100005], sz[100005], len[100005], tp[100005];
std::vector<int> g[100005];
void cfs(int v = 1, int p = -1) {
for (int i = 0; i < g[v].size(); ++i) if (g[v][i] == p) g[v].erase(g[v].begin() + i);
sz[v] = 1;
for (auto u: g[v]) if (u != p) cfs(u, v), sz[v] += sz[u], ::p[u] = v;
std::sort(g[v].begin(), g[v].end(), [](const int&v, const int&u) { return sz[v] > sz[u]; });
}
void dfs(int v = 1, int s = 1) {
f[v] = s, pos[v] = len[s]++;
for (auto u: g[v]) dfs(u, u == g[v].front() ? s : u);
}
void initialize(int N, std::vector<int> A, std::vector<int> B) {
for (int i = 0; i < A.size(); ++i) g[A[i]].push_back(B[i]), g[B[i]].push_back(A[i]);
cfs(), dfs();
for (int v = 1; v <= N; ++v) if (!pos[v]) ok[v] = len[v];
}
void modify(int v, int x, int y) {
while (v) {
auto [w0, w1] = ok[f[v]].get();
ok[f[v]].modify(pos[v], x, y);
auto [n0, n1] = ok[f[v]].get();
x = n0 - w0, y = n1 - w1, v = p[f[v]];
}
}
int query() {
auto [a, b] = ok[1].get();
return std::min(a, b);
}
int cat(int v) { return tp[v] = 1, modify(v, 1e9, 0), query(); }
int dog(int v) { return tp[v] = 2, modify(v, 0, 1e9), query(); }
int neighbor(int v) { return modify(v, -1e9 * (tp[v] == 1), -1e9 * (tp[v] == 2)), tp[v] = 0, query(); }
Compilation message
catdog.cpp:3:1: error: 'size_t' does not name a type
3 | size_t cur = 0;
| ^~~~~~
catdog.cpp:1:1: note: 'size_t' is defined in header '<cstddef>'; did you forget to '#include <cstddef>'?
+++ |+#include <cstddef>
1 | char mem[(int)1e8];
catdog.cpp:5:7: error: declaration of 'operator new' as non-function
5 | void *operator new(size_t n) {return mem + (cur += n) - n;}
| ^~~~~~~~
catdog.cpp:5:20: error: 'size_t' was not declared in this scope
5 | void *operator new(size_t n) {return mem + (cur += n) - n;}
| ^~~~~~
catdog.cpp:5:20: note: 'size_t' is defined in header '<cstddef>'; did you forget to '#include <cstddef>'?
catdog.cpp:7:30: error: 'size_t' has not been declared
7 | void operator delete(void *, size_t) noexcept {}
| ^~~~~~
catdog.cpp: In function 'void cfs(int, int)':
catdog.cpp:52:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for (int i = 0; i < g[v].size(); ++i) if (g[v][i] == p) g[v].erase(g[v].begin() + i);
| ~~^~~~~~~~~~~~~
catdog.cpp: In function 'void initialize(int, std::vector<int>, std::vector<int>)':
catdog.cpp:64:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for (int i = 0; i < A.size(); ++i) g[A[i]].push_back(B[i]), g[B[i]].push_back(A[i]);
| ~~^~~~~~~~~~