zoukankan      html  css  js  c++  java
  • [POJ3321] 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
    

    Source

    POJ Monthly--2007.08.05, Huang, Jinsong

    题解

    树状数组(+DFS)

    先进行一次时间戳,然后用树状数组进行区间统计与修改

    具体见代码:

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<bitset>
    #include<set>
    #include<vector>
    #include<map>
    #include<iomanip>
    using namespace std;
    
    const int N=200000,NE=500000;
    int n,m,t,t1[N],t2[N],a[N],c[N];
    struct edge
    {
    	int v,nx;
    }e[NE];
    int ne,hd[N];
    
    void Build(int u,int v)
    {
    	++ne,e[ne]=(edge){v,hd[u]},hd[u]=ne;
    }
    
    void Dfs(int id)//时间戳
    {
    	++t,t1[id]=t;
    	for(int i=hd[id];i;i=e[i].nx)
    		Dfs(e[i].v);
    	t2[id]=t;
    }
    
    int Lowbit(int x)
    {
    	return x&-x;
    }
    
    void Updata(int id,int num)
    {
    	for(;id<=n;)
    	{
    		c[id]+=num,
    		id+=Lowbit(id);
    	}
    }
    
    int Get_sum(int id)
    {
    	int Res=0;
    	for(;id;)
    	{
    		Res+=c[id],
    		id-=Lowbit(id);
    	}
    	return Res;
    }
    
    int main()
    {
    	scanf("%d",&n);
    	int u,v;
    	for(int i=1;i<n;++i)
    	{
    		scanf("%d%d",&u,&v),
    		Build(u,v);
    	}
    	Dfs(1);
    	for(int i=1;i<=n;++i)
    	{
    		a[i]=1;
    		Updata(t1[i],1);
    	}
    	scanf("%d
    ",&m);
    	char s; int id;
    	for(int i=1;i<=m;++i)
    	{
    		scanf("%c%d
    ",&s,&id);
    		if(s=='Q') printf("%d
    ",Get_sum(t2[id])-Get_sum(t1[id]-1));
    		else
    		{
    			a[id]=0-a[id];
    			Updata(t1[id],a[id]);
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    1.ok6410移植bootloader,移植u-boot,学习u-boot命令
    ok6410按键中断编程,linux按键裸机
    四. jenkins部署springboot项目(1)--window环境
    一.jenkins安装(windows环境)
    oracle服务端导出/导入方式expdp/impdp
    linux 日志文件查看
    linux kafka进程挂了 自动重启
    kafka manager遇到的一些问题
    if条件语句
    shell脚本的条件测试与比较
  • 原文地址:https://www.cnblogs.com/hihocoder/p/12077468.html
Copyright © 2011-2022 走看看