POJ-3321-Apple Tree
来源:互联网 发布:ubuntu 杀死所有进程 编辑:程序博客网 时间:2024/05/21 07:55
这个题要求对苹果树进行更新与查找工作,先用DFS找出时间戳,然后树状数组进行更新操作即可
代码:
#include<cstdio>#include<cstring>#include<iostream>using namespace std;const int maxn=1e6+100;struct node{ int l; int r;}t[maxn];int n,m,time,c[maxn*3],a[maxn],head[maxn],pnt[maxn],nxt[maxn],e;void addedge(int u,int v){ pnt[e]=v;nxt[e]=head[u];head[u]=e++;}void DFS(int u){ t[u].l=time; for(int i=head[u];i!=-1;i=nxt[i])DFS(pnt[i]); t[u].r=time++;}int lowbit(int x){ return x&(-x);}void change(int x){ if(a[x])for(int i=x;i<time;i+=lowbit(i)) c[i]++; elsefor(int i=x;i<time;i+=lowbit(i)) c[i]--;}int sum(int x){ int ans=0; for(int i=x;i>0;ans+=c[i],i-=lowbit(i)); return ans;}int main(){ while(scanf("%d",&n)!=EOF) {memset(head,-1,sizeof(head));e=0;time=1;for(int i=0;i<n-1;i++){ int u,v; scanf("%d%d",&u,&v); addedge(u,v);}DFS(1);scanf("%d",&m);for(int i=1;i<=n;i++){ a[i]=1; change(i);}while(m--){ char op[3]; int ita; scanf("%s%d",op,&ita); if(op[0]=='Q')printf("%d\n",sum(t[ita].r)-sum(t[ita].l-1)); else {a[t[ita].r]^=1;change(t[ita].r); }} } return 0;}
- POJ 3321 Apple Tree
- POJ 3321 Apple Tree
- poj 3321 Apple Tree
- POJ 3321 Apple Tree
- Poj 3321 Apple Tree
- poj 3321 apple tree
- poj 3321 Apple Tree
- poj 3321 Apple Tree
- POJ 3321 Apple Tree
- POJ--3321--Apple Tree
- POJ-3321-Apple Tree
- poj 3321 Apple Tree
- POJ 3321 Apple Tree
- POJ 3321 Apple Tree
- POJ 3321 Apple Tree
- POJ 3321 Apple Tree
- POJ:3321 Apple Tree
- POJ 3321 Apple Tree
- 10408 - Farey sequences - UVa
- Cisco路由器的硬件组成
- Convert Sorted List to Binary Search Tree
- Switch case is in protected scope
- intel dpdk api timer 模块详解和测试
- POJ-3321-Apple Tree
- 关于flex4绘画板中加入文字的一些感悟
- javascript中escape,encodeURI,encodeURIComponent三个函数的区别
- Interleaving String
- USB子系统学习之基础篇一(相关术语及设备)
- C语言的基本数据类型总结
- 小数高精度乘法
- Android中GC_FOR_MALLOC的含义
- poj 1658 Eva's Problem(水题)