poj 3321 Ultra-QuickSort (树状数组)
来源:互联网 发布:北仑博菱电器工资算法 编辑:程序博客网 时间:2024/05/26 07:28
Description
There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple tree.
The tree has N forks which are connected by branches. Kaka numbers the forks by 1 to N and the root is always numbered by 1. Apples will grow on the forks and two apple won't grow on the same fork. kaka wants to know how many apples are there in a sub-tree, for his study of the produce ability of the apple tree.
The trouble is that a new apple may grow on an empty fork some time and kaka may pick an apple from the tree for his dessert. Can you help kaka?
Input
The first line contains an integer N (N ≤ 100,000) , which is the number of the forks in the tree.
The following N - 1 lines each contain two integers u and v, which means fork u and fork v are connected by a branch.
The next line contains an integer M (M ≤ 100,000).
The following M lines each contain a message which is either
"C x" which means the existence of the apple on fork x has been changed. i.e. if there is an apple on the fork, then Kaka pick it; otherwise a new apple has grown on the empty fork.
or
"Q x" which means an inquiry for the number of apples in the sub-tree above the fork x, including the apple (if exists) on the fork x
Note the tree is full of apples at the beginning
Output
Sample Input
31 21 33Q 1C 2Q 1
Sample Output
32
Source
POJ Monthly--2007.08.05, Huang, Jinsong
题意: 有一个苹果树,有两种操作,Q是询问所有子树包括结点一共有多少个苹果,C是如果结点存在苹果就吃掉,否则长出一个苹果。
分析: 额。。其实就是修改一下结点的编号,有个小坑,这个题居然卡 vector ~~所以要用邻接表做
AC代码:
#include<stdio.h>#include<string.h>#include<algorithm>#include<vector>using namespace std;const int maxn=1e5+20;int left[maxn],right[maxn];int sum[maxn];int head[maxn];bool s[maxn];int cnt;int num;struct tree{int u,v,next;tree(){}tree(int uu,int vv,int nn){u=uu,v=vv,next=nn;}}edge[maxn];int lowbit(int x){return x&(-x);}void init(int n){cnt=1;num=0;memset(sum,0,sizeof(sum));memset(s,false,sizeof(s));memset(head,-1,sizeof(head));}void addedge(int a,int b){edge[num].u=a;edge[num].v=b;edge[num].next=head[a];head[a]=num++;}void dfs(int x){left[x]=cnt;for(int i=head[x];i!=-1;i=edge[i].next){cnt++;dfs(edge[i].v);}right[x]=cnt;}void update(int x,int d,int n){while(x<=n){sum[x]+=d;x+=lowbit(x);}}int getadd(int x){int ans=0;while(x>0){ans+=sum[x];x-=lowbit(x);}return ans;}int main(){int n;while(scanf("%d",&n)==1){init(n);for(int i=0;i<n-1;i++){int a,b;scanf("%d%d",&a,&b);addedge(a,b);}dfs(1);for(int i=1;i<=n;i++){s[i]=true;update(i,1,n);}int m;scanf("%d",&m);for(int i=0;i<m;i++){char q[3];int w;scanf("%s",q);scanf("%d",&w);if(q[0]=='Q'){printf("%d\n",getadd(right[w])-getadd(left[w]-1));}else{if(s[w])update(left[w],-1,n);elseupdate(left[w],1,n);s[w]=!s[w];}}}}
- poj 3321 Ultra-QuickSort (树状数组)
- poj - 2299 - Ultra-QuickSort(树状数组)
- POJ 2299 Ultra-QuickSort(树状数组)
- poj 2229 Ultra-QuickSort(树状数组)
- POJ 2299 Ultra-QuickSort (树状数组)
- POJ 2299 Ultra-QuickSort(树状数组)
- poj 2299 Ultra-QuickSort(树状数组)
- POJ 2299 Ultra-QuickSort(树状数组)
- POJ 2299 Ultra-QuickSort (树状数组)
- POJ 2299 Ultra-QuickSort(树状数组)
- poj 2299 Ultra-QuickSort (树状数组)
- POJ 3321 Ultra-QuickSort(树状数组+DFS序)
- Ultra-QuickSort(树状数组)
- POJ-2299 Ultra-QuickSort(树状数组)(离散化)
- poj 2299 Ultra-QuickSort(离散化+树状数组)
- POJ 2299 Ultra-QuickSort(树状数组+离散化)
- POJ 2299 Ultra-QuickSort(树状数组+离散化处理)
- POJ 2299-Ultra-QuickSort (树状数组+离散化)
- 关于位域的理解
- java web学习笔记1-tomcat conf文件解析笔记之server.xml
- 如何在react中封装服务
- MeasureSpec 的分析
- Hadoop 分布式环境搭建
- poj 3321 Ultra-QuickSort (树状数组)
- 28份技术知识图谱
- git分支管理
- EventBus3.0框架使用详细讲解
- 微信小程序开发之IOS/Android兼容坑
- JavaEE的三层架构
- 假设检验
- 代码审查工具SonarQube
- c语言求解一元二次方程