BZOJ 1103: [POI2007]大都市meg(dfs序,树状数组)

本来还想链剖的,结果才发现能直接树状数组的= =

记录遍历到达点与退出点的时间,然后一开始每个到达时间+1,退出时间-1,置为公路就-1,+1,询问直接点1到该点到达时间求和就行了- -

CODE:

#include<cstdio>

#include<iostream>

#include<cstring>

#include<algorithm>

#include<vector>

using namespace std;

#define maxn 500010

#define maxm 250010

int f[maxn+10],up[maxm],down[maxm],clo;

vector<int > q[maxm];

#define lowbit(x) (x&(-x))

void add(int x,int y) {

for (x;x<=maxn;x+=lowbit(x)) f[x]+=y;

}

int sum(int x) {

int ans=0;

for (;x;x-=lowbit(x)) ans+=f[x];

return ans;

}

void dfs(int x) {

up[x]=++clo;

for (int i=0;i<q[x].size();i++) dfs(q[x][i]);

down[x]=++clo;

return ;

}

int main(){

int n,m;

scanf("%d",&n);

for (int i=1;i<n;i++) {

int x,y;

scanf("%d%d",&x,&y);

if (x>y) swap(x,y);

q[x].push_back(y);

}

dfs(1);

for (int i=2;i<=n;i++) add(up[i],1),add(down[i],-1);

scanf("%d",&m);

for (int i=1;i<=n+m-1;i++) {

char s[2];int x,y;

scanf("%s",s);

switch (s[0]){

case 'A':

scanf("%d%d",&x,&y);

if (x>y) swap(x,y);

add(up[y],-1);

add(down[y],1);

break;

case 'W':

scanf("%d",&x);

printf("%d\n",sum(up[x]));

break;

}

}

return 0;

}


posted @ 2014-11-18 16:43  New_Godess  阅读(126)  评论(0编辑  收藏  举报