# 题意

## 数据范围

$$n, m, q \le 2 \times 10^5$$

# 代码

#include <bits/stdc++.h>

#define For(i, l, r) for (register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l) for (register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Rep(i, r) for (register int i = (0), i##end = (int)(r); i < i##end; ++i)
#define Set(a, v) memset(a, v, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define debug(x) cout << #x << ": " << (x) << endl

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;

template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, T b) { return b > a ? a = b, 1 : 0; }

int x(0), sgn(1); char ch(getchar());
for (; !isdigit(ch); ch = getchar()) if (ch == '-') sgn = -1;
for (; isdigit(ch); ch = getchar()) x = (x * 10) + (ch ^ 48);
return x * sgn;
}

void File() {
freopen ("1954.in", "r", stdin);
freopen ("1954.out", "w", stdout);
#endif
}

const int N = 2e5 + 1e3;

vector<int> G[N];

ll ans = 0, sum[N];

int n, m, q, dep[N], anc[N][20], Log2[N], sz[N], dfn[N];

void Dfs_Init(int u, int fa = 0) {
static int clk = 0; dfn[u] = ++ clk;
dep[u] = dep[anc[u][0] = fa] + 1;
ans += dep[u]; sz[u] = 1;
for (int v : G[u]) if (v != fa) Dfs_Init(v, u), sz[u] += sz[v];
}

void Get_Sum(int u, int fa = 0) {
sum[u] = sum[fa] + (dep[u] > 1) * sz[u];
for (int v : G[u]) if (v != fa) Get_Sum(v, u);
}

struct Cmp {
inline bool operator () (const int &lhs, const int &rhs) { return dfn[lhs] < dfn[rhs]; }
};

vector<int> add[N], del[N]; multiset<int, Cmp> S;

inline int Lca(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
int gap = dep[x] - dep[y];
For (i, 0, Log2[gap])
if (gap >> i & 1) x = anc[x][i];
if (x == y) return x;
Fordown (i, Log2[dep[x]], 0)
if (anc[x][i] != anc[y][i]) x = anc[x][i], y = anc[y][i];
return anc[x][0];
}

int Find(int x) {
PII res; auto it = S.upper_bound(x);
if (it != S.end()) {
int tmp = Lca(*it, x); chkmax(res, {dep[tmp], tmp});
}
if (it != S.begin()) {
int tmp = Lca(*prev(it), x); chkmax(res, {dep[tmp], tmp});
}
return res.second ? res.second : x;
}

int main () {

File();

For (i, 1, n - 1) {
G[u].push_back(v); G[v].push_back(u);
}

while (q --) {
}

dep[0] = -1; Dfs_Init(1); Get_Sum(1);

For (i, 2, n) Log2[i] = Log2[i >> 1] + 1;
For (j, 1, Log2[n]) For (i, 1, n)
anc[i][j] = anc[anc[i][j - 1]][j - 1];

S.insert(1);
For (i, 1, m) {
for (int x : add[i]) {
if (S.find(x) == S.end())
ans -= sum[x] - sum[Find(x)];
S.insert(x);
}
for (int x : del[i]) {
S.erase(S.find(x));
if (S.find(x) == S.end())
ans += sum[x] - sum[Find(x)];
}
printf ("%lld%c", ans, i == iend ? '\n' : ' ');
}

return 0;

}

posted @ 2019-04-17 21:12  zjp_shadow  阅读(402)  评论(0编辑  收藏  举报