Title Description
Solution
The exam was not carefully thought, the results only took the violent points ...
is actually a DFS sequence + tree-like array.
Let's turn it into a sequence with DFS and record their \ (siz\).
So every time we have one edge, we have an effect on the subtree.
It's good to maintain it in a tree array.
Code
#include <bits/stdc++.h>using namespace Std;const int maxn=250008;struct sj{int to; int next;} A[maxn*2];int head[maxn],size;int n,m;void add1 (int x,int y) {a[++size].to=y; A[SIZE].NEXT=HEAD[X]; Head[x]=size;} int id[maxn],tot;int siz[maxn],ans[maxn];void DFS (int x) {id[x]=++tot; Siz[x]=1; for (int i=head[x];i;i=a[i].next) {int tt=a[i].to; if (!siz[tt]) {ans[tt]=ans[x]+1; DFS (TT); SIZ[X]+=SIZ[TT]; }}}int c[maxn];int lowbit (int x) {return x& (-X);} void Add (int x,int z) {for (int i=x;i<=n;i+=lowbit (i)) c[i]+=z;} int check (int x) {int sum=0; for (int i=x;i>=1;i-=lowbit (i)) sum+=c[i]; return sum;} int main () {scanf ("%d", &n); for (int i=1;i<n;i++) {int x, Y, scanf ("%d%d", &x,&y), Add1 (x, y); Add1 (y,x);} DFS (1); for (int i=1;i<=n;i++) Add (id[i],0); scanf ("%d", &m); for (int i=1;i<=m+n-1;i++) {int X,y;char ch; cin>>ch; if (ch== ' A ') {scanf ("%d%d", &x,&y); if (Id[x]<id[y]) swap (Y,X); Add (id[x],1); Add (id[x]+siz[x],-1); } if (ch== ' W ') scanf ("%d", &x), Cout<<ans[x]-check (Id[x]) <<endl; }}
[Poi2007]meg-megalopolis (tree array, DFS order)