第二个点和第八个点 TLE 了,数据都是 100000 顶满的。
#include <bits/stdc++.h>
#define low(i) ((i)&(-(i)))
using namespace std;
char nc(){
static char buf[100000], *p1=buf, *p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
int read(){
int x = 0, f = 1;
char c = nc();
while(!isdigit(c)) {if(c=='-')f=-1; c=nc();}
while(isdigit(c)) {x=x*10+c-48; c=nc();}
return x*f;
}
int n, a, b, tmp;
int dfn[100010], d, siz[100010];
int head[100010], nxt[100010], ver[100010], tot=1;
int bit[100010];
void dfs(int u){
dfn[u] = ++d;
siz[u] = 1;
for(int i=head[u]; i; i=nxt[i]){
int v = ver[i];
if(dfn[v]) continue;
dfs(v);
siz[u] += siz[v];
}
}
void addedge(int u, int v){
ver[tot] = v;
nxt[tot] = head[u];
head[u] = tot++;
}
void add(int p, int val){
for(; p<=n; p+=low(p))
bit[p] += val;
}
int query(int p){
int ans = 0;
for(; p; p-=low(p))
ans += bit[p];
return ans;
}
int main(){
n = read();
for(int i=1; i<n; i++){
a = read(), b = read();
addedge(a, b);
addedge(b, a);
}
dfs(1);
// for(int i=1; i<=n; i++){
// printf("%d %d\n", dfn[i], siz[i]);
// }
for(int i=1; i<=n; i++){
tmp = read();
printf("%d\n", query(dfn[tmp]));
add(dfn[tmp]+1, 1);
add(dfn[tmp]+siz[tmp], -1);
// for(int i=1; i<=n; i++)
// printf("%d ", query(i));
// printf("%d\n");
}
return 0;
}