首页 > 代码库 > 树标号
树标号
Apple Tree http://poj.org/problem?id=3321
1 #include<cstdio> 2 #include<cstring> 3 #define mt(a,b) memset(a,b,sizeof(a)) 4 #define lrrt int L,int R,int rt 5 #define iall 1,n,1 6 #define imid int mid=(L+R)>>1 7 #define lson L,mid,rt<<1 8 #define rson mid+1,R,rt<<1|1 9 using namespace std;10 const int M=100010;11 int tree[M<<2];12 void pushup(int rt){13 tree[rt]=tree[rt<<1]+tree[rt<<1|1];14 }15 void build(lrrt){16 if(L==R){17 tree[rt]=1;18 return ;19 }20 imid;21 build(lson);22 build(rson);23 pushup(rt);24 }25 void update(int x,lrrt){26 if(L==R){27 tree[rt]^=1;28 return ;29 }30 imid;31 if(mid>=x) update(x,lson);32 else update(x,rson);33 pushup(rt);34 }35 int query(int x,int y,lrrt){36 if(x<=L&&R<=y) return tree[rt];37 imid;38 int ans=0;39 if(mid>=x) ans+=query(x,y,lson);40 if(mid<y) ans+=query(x,y,rson);41 return ans;42 }43 struct G{44 struct E{45 int u,v,next;46 }e[M<<1];47 int le,head[M];48 void init(){49 le=0;50 mt(head,-1);51 }52 void add(int u,int v){53 e[le].u=u;54 e[le].v=v;55 e[le].next=head[u];56 head[u]=le++;57 }58 }g;59 struct Node{60 int l,r;61 }node[M];62 int Index;63 void dfs(int u,int fa){64 node[u].l=++Index;65 for(int i=g.head[u];~i;i=g.e[i].next){66 int v=g.e[i].v;67 if(v!=fa){68 dfs(v,u);69 }70 }71 node[u].r=Index;72 }73 int main(){74 int n,m;75 while(~scanf("%d",&n)){76 g.init();77 for(int i=0,u,v;i<n-1;i++){78 scanf("%d%d",&u,&v);79 g.add(u,v);80 g.add(v,u);81 }82 build(iall);83 Index=0;84 dfs(1,-1);85 scanf("%d",&m);86 while(m--){87 char op[4];88 int x;89 scanf("%s%d",op,&x);90 if(op[0]==‘C‘){91 update(node[x].l,iall);92 }93 else{94 printf("%d\n",query(node[x].l,node[x].r,iall));95 }96 }97 }98 return 0;99 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。