Apple Tree(树状数组+线段树)
来源:互联网 发布:仿站长之家网站源码 编辑:程序博客网 时间:2024/05/16 13:40
Apple Tree
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
For every inquiry, output the correspond answer per line.
Sample Input:
3
1 2
1 3
3
Q 1
C 2
Q 1
Sample Output:
3
2
题目大意:
一棵树上长了苹果,每一个树枝节点上有长苹果和不长苹果两种状态,两种操作,一种操作能够改变树枝上苹果的状态,另一种操作询问某一树枝节点一下的所有的苹果有多少。
树状数组版:
#include<iostream>using namespace std;const int maxn=210100;struct node{ int to; int next;}e[maxn];char s;int x;int in[maxn],out[maxn],c[maxn],cnt;int n,m,tot,head[maxn],vis[maxn];void edd_edge(int u,int v){ tot++; e[tot].to=v; e[tot].next=head[u]; head[u]=tot;}int lowbit(int x){ return x&(-x);}void dfs(int x){ in[x]=++cnt; for(int i=head[x];i;i=e[i].next) dfs(e[i].to); out[x]=cnt;}void update(int x,int add){ while(x<=n) { c[x]+=add; x+=lowbit(x); }}int sum(int x){ int s=0; while(x>0) { s+=c[x]; x-=lowbit(x); } return s;}int main(){ int a,b; cin>>n; for(int i=1;i<n;i++) { cin>>a>>b; edd_edge(a,b); } dfs(1); for(int i=1;i<=n;i++) update(in[i],1),vis[i]=1; cin>>m; for(int i=1;i<=m;i++) { cin>>s>>x; if(s=='C') { if(vis[x])update(in[x],-1); else update(in[x],1); vis[x]=1-vis[x]; } if(s=='Q') cout<<sum(out[x])-sum(in[x]-1)<<endl; } return 0;}
线段树版:
#include<iostream>using namespace std;const int maxn=100010;struct edge{ int to; int next;}e[maxn*2];struct node{ int l,r; int lch,rch; int sum;}tree[maxn*4];char c;int n,m,tot,a[maxn];int head[maxn],in[maxn],out[maxn];void add_edge(int u,int v){ tot++; e[tot].to=v; e[tot].next=head[u]; head[u]=tot;}void dfs(int u){ in[u]=++tot; for(int i=head[u];i;i=e[i].next) dfs(e[i].to); out[u]=tot;}void build_tree(int ll,int rr){ int cur=++tot; tree[cur].l=ll; tree[cur].r=rr; if(ll!=rr-1) { int mid=(ll+rr)>>1; tree[cur].lch=tot+1; build_tree(ll,mid); tree[cur].rch=tot+1; build_tree(mid,rr); tree[cur].sum=tree[tree[cur].lch].sum+tree[tree[cur].rch].sum; } else tree[cur].sum=1;}int find(int k,int l,int r){ int ans=0; if(tree[k].l>=l&&tree[k].r<=r) return tree[k].sum; int mid=(tree[k].l+tree[k].r)>>1; if(l<mid) ans+=find(tree[k].lch,l,r); if(r>mid) ans+=find(tree[k].rch,l,r); return ans;}void change(int k,int l,int r,int p){ if(tree[k].l==l&&tree[k].r==r) { tree[k].sum+=p; return; } int mid=(tree[k].l+tree[k].r)>>1; if(l<mid) change(tree[k].lch,l,r,p); if(r>mid) change(tree[k].rch,l,r,p); tree[k].sum=tree[tree[k].lch].sum+tree[tree[k].rch].sum;}int main(){ int x,y; cin>>n; for(int i=1;i<=n-1;i++) { cin>>x>>y; add_edge(x,y); }tot=0; dfs(1);tot=0; build_tree(1,n+1); for(int i=1;i<=n;i++) a[i]=1; cin>>m; for(int i=1;i<=m;i++) { cin>>c>>x; if(c=='C') { if(a[x]==1) a[x]=0,change(1,in[x],in[x]+1,-1); else a[x]=1,change(1,in[x],in[x]+1,1); } else cout<<find(1,in[x],out[x]+1)<<endl; } return 0;}
- Apple Tree(树状数组+线段树)
- POJ-3321-Apple Tree,线段树,树状数组。
- Apple Tree-----树状数组
- Apple Tree(POJ3321)(树状数组)
- 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 【树形结构转变为线性结构+线段树OR树状数组】
- system("cls")的用法
- sdut oj3345 数据结构实验之二叉树六:哈夫曼编码
- 数据结构实验之二叉树四:还原二叉树
- 数据结构实验之求二叉树后序遍历和层次遍历
- 解决Eclipse error: could not install *smartsocket* listener: cannot bind to 127.0.0.1:5037:
- Apple Tree(树状数组+线段树)
- Android 高德地图开发,手机显示用户MD5安全码未通过
- android webview shouldOverrideUrlLoading html iframe内部链接
- Android webview与js交互上传图片
- 高维网络 (dp+组合数)
- Java流
- java面试准备-day02
- Android 5.0+新控件学习地址
- iOS基于openssl 的AES-128-cbc-pkcs5 加解密