http://www.lydsy.com/JudgeOnline/problem.php?id=1103
Test instructions: An N-node tree (1<=n<=250000), M-Bar (1<=M<=250000-1), with a weight of 1, has a n+m-1 operation: Operation W u: Ask u to the root of the weight and; operate a U V, subtract the weight of the Edge (U, v) by 1
#include <bits/stdc++.h>using namespace Std;const int N=250005;int N, ihead [N], cnt;struct dat {int next, to;} e[n<<1];void Add (int u, int v) {e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v;e[++cnt].next=ihead[v]; ihead[ v]=cnt; E[cnt].to=u;} int dep[n], tot, ff[n], ll[n];void dfs (int x, int fa) {ff[x]=++tot;for (int i=ihead[x]; i; i=e[i].next) if (E[I].TO!=FA) DEP [E[i].to]=dep[x]+1, DFS (e[i].to, x); Ll[x]=tot;} int c[n];void upd (int x, int s) {for (; x<=n; x+=x&-x) c[x]+=s;} int sum (int x) {int ret=0; for (; x; x-=x&-x) ret+=c[x]; return ret;} int main () {scanf ("%d", &n), for (int i=0; i<n-1; ++i) {int u, v; scanf ("%d%d", &u, &v); Add (U, v);} DFS (1, 0); int m; scanf ("%d", &m), for (int k=0; k<m+n-1; ++k) {int u, V;char s[2];scanf ("%s%d", S, &u), if (s[0]== ' W ') {printf ("%d\ n ", sum (Ff[u]) +dep[u]); }else {scanf ("%d", &v), if (Dep[u]<dep[v]) swap (U, v), UPD (Ff[u],-1), UPD (ll[u]+1, 1);}} return 0;}
The DFS sequence should be ...
Obviously, every time you change an edge, it affects all subtrees of a node with a greater depth, and asking is asking 1-the right value of the current point and the line.
So let's maintain the DFS sequence and prefix and
"Bzoj" 1103: [POI2007] Metropolitan Meg