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;
    }
    
  • 相关阅读:
    动态绑定表格
    双缓冲设置控件
    编写模块化插件式应用程序
    网上收集整理SharePoint的母版页master占位符(改动不大)
    改变SharePoint工作流任务的界面
    SharePoint定时器作业部署步骤
    定时器作业将sap数据更新至列表库
    网络没问题,MSN登录不了解决方法
    以后就搬家到cnblogs吧。
    记录我的第一个OpenGL程序
  • 原文地址:https://www.cnblogs.com/hihocoder/p/12077468.html
Copyright © 2011-2022 走看看