poj3321-dfs序&树状数组&dfs序模板题-Apple Tree
来源:互联网 发布:python视频教程 廖雪峰 编辑:程序博客网 时间:2024/06/05 02:46
开始用getsum()-getsum()当作单点查询竟然tle了。。
于是乎 我用了一个计数器。
和上一道题一摸一样。真的是模板题啊 亲们qwq
就对着照了上一道题改了一下下
一棵树上每个点上挂一个苹果
下列操作:① 查询以x为根的子树上有多少个苹果(裸的我tm)
② 对苹果的数量进行取反(如果有变成没有,没有变成有)
#include <iostream>#include <stdio.h>#include <string.h>using namespace std;/*体现题意的地方,就在于在树状数组中的结果。*/const int maxn=1e5+100;struct Node{ int to,next;}node[maxn*2];int head[maxn];int sta[maxn];int end1[maxn];int ans[maxn];int sum[maxn];int che[maxn];int len;void addedge(int a,int b){ node[len].to=b; node[len].next=head[a]; head[a]=len++;}int tim;void init(){ tim=0; len=0; memset(head,-1,sizeof(head)); memset(sta,0,sizeof(sta)); memset(sum,0,sizeof(sum)); memset(che,0,sizeof(che));}void dfs(int s,int pre){ sta[s]=++tim; for(int i=head[s];i!=-1;i=node[i].next){ int to=node[i].to; if(to==pre) continue; dfs(to,s); } end1[s]=tim;}int lowbit(int a){ return a&(-a);}void add(int s,int va){ for(int i=s;i<maxn;i+=lowbit(i)) sum[i]+=va;}int getsum(int s){ int all=0; while(s>0){ all+=sum[s]; s-=lowbit(s); } return all;}int main(){ int m,n,a,b; while(~scanf("%d",&m)){ if(!m)break; init(); for(int i=0;i<m-1;i++){ scanf("%d%d",&a,&b); addedge(a,b); addedge(b,a); } dfs(1,-1); scanf("%d",&n); char ch; for(int i=1;i<=m;i++){ add(sta[i],1); } int nu; for(int i=1;i<=n;i++){ scanf(" %c",&ch); if(ch=='Q') { scanf("%d",&nu); int ans=getsum(end1[nu])-getsum(sta[nu]-1); printf("%d\n",ans); } else{ scanf("%d",&nu); if(che[nu]%2==0){ che[nu]++; add(sta[nu],-1); } else{ che[nu]++; add(sta[nu],1); } } } printf("\n"); } return 0;}
阅读全文
0 0
- POJ3321 Apple Tree(DFS序 + 树状数组)
- poj3321-Apple Tree-dfs序+树状数组
- Poj3321 Apple Tree【DFS序】【树状数组】
- poj3321-dfs序&树状数组&dfs序模板题-Apple Tree
- 【树状数组--dfs序转化】poj3321 Apple Tree
- poj3321 Apple Tree(DFS+树状数组)
- 【POJ3321】Apple Tree-DFS+树状数组维护
- poj3321 Apple Tree(树状数组+dfs)
- POJ3321 dfs序+树状数组
- POJ3321(dfs序+树状数组)
- POJ3321——Apple Tree(DFS+树状数组)
- POJ3321 Apple Tree(树状数组 + dfs + 线性表)
- 【poj3321】Apple Tree——树状数组&DFS
- (POJ3321)Apple Tree <dfs序列 树状数组>
- POJ3321-Apple Tree(dfs序+线段树)
- POJ3321[苹果树]--DFS序+树状数组
- poj3321 dfs+树状数组
- POJ3321/Apple tree/(DFS序+线段树)
- Hibernate一对多关系映射(双向映射)
- first step
- PostgreSQL 数据类型
- 设计模式之禅
- 1019. 数字黑洞 (20)
- poj3321-dfs序&树状数组&dfs序模板题-Apple Tree
- 标签的基本语法
- ubuntu qq
- 机器学习基石-09-1-Linear Regression Problem
- window10 centos7双系统,win启动项消失的问题
- 2017ccpc赛后总结(杭州站)
- Maven+MyBaits使用总结
- 算法设计周记(九)--二分查找
- 用宏来选择不同的代码段