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;
    }
    
  • 相关阅读:
    IIS:日志代码分析
    SQL:查找被锁的表,以及锁表的SQL语句(重点推荐)
    SQL 2000/2005/2008 收缩日志方法
    SQL SERVER:使用工具观察与分析数据库中锁信息
    C# : Post 接收或发送XML
    WCF:没有终结点在侦听可以接受消息的*这通常是由于不正确的地址或者 SOAP操作导致的。
    SQL2005 遍历表插入
    SQL2005:SQL Server 2005还原数据库时出现“不能选择文件或文件组XXX_log用于此操作的解决办法
    C#:安装Windows服务,动态指定服务名及描述
    IE6与 javascript:void(0)
  • 原文地址:https://www.cnblogs.com/hihocoder/p/12077468.html
Copyright © 2011-2022 走看看