inline void solve() {
cin >> q >> C;
rep (i, 1, n) G[i].clear(), dep[i] = 0;
rep (i, 1, q) rt[i] = col[i] = 0;
n = 1; tot = 0;
rep (i, 1, q) {
cin >> op >> x >> c;
if (op == 0) {
cin >> d, n++;
G[x].pb(mp(n, d)), G[n].pb(mp(x, d));
x = n;
}
Q[i] = mp(x, c);
}
T.build(); build();
dfs1(1, 0); init();
T.upd(rt[C], 1, 1); upd(1, C, t[rt[C]]); col[1] = C;
rep (i, 1, q) {
x = Q[i].fi, c = Q[i].se;
if (col[x]) T.upd(rt[col[x]], x, 0), upd(1, col[x], t[rt[col[x]]]);
col[x] = c;
T.upd(rt[c], x, 1), upd(1, c, t[rt[c]]);
cout << s[1].ans << "\n";
}
}
为什么这段代码的第 3 行 rep 中的 q 改为 n 就会 MLE。
这个题会每次加入一个新节点,n 代表是上一轮的总结点数,题目保证 ∑q=5×105。
对了,我的代码除了这一个 vector 没有其他的动态空间,静态空间大约在 390 Mib 左右。