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;
    }
    
  • 相关阅读:
    详解PhpStudy集成环境升级MySQL数据库版本
    Mysql5.5升级到5.7的过程已经踩到的坑
    phpStudy中升级MySQL版本到5.7.17的方法步骤
    Windows上使用Vagrant打造Laravel Homestead可协同跨平台开发环境
    百度云下载加速的
    libsvm的安装,数据格式,常见错误,grid.py参数选择,c-SVC过程,libsvm参数解释,svm训练数据,libsvm的使用详解,SVM核函数的选择
    RBF神经网络的matlab简单实现
    spark mllib docs,MLlib: RDD-based API
    目前所有的ANN神经网络算法大全
    Spark1.6.1 MLlib 特征抽取和变换
  • 原文地址:https://www.cnblogs.com/hihocoder/p/12077468.html
Copyright © 2011-2022 走看看