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;
    }
    
  • 相关阅读:
    Exp4 恶意代码分析 20155223
    20155223 Exp3 免杀原理与实践 实验报告
    20155223 Exp2 后门原理与实践
    网络对抗第一次实验——PC平台逆向破解(5)M
    苏黄的假期学习报告3
    我的假期学习报告2
    苏黄永郦的假期报告——1
    20155223 《信息安全系统设计基础》课程总结
    2017-2018-1 20155223 《信息安全系统设计基础》第14周学习总结
    2017-2018-1 20155223 实验五 通讯协议设计
  • 原文地址:https://www.cnblogs.com/hihocoder/p/12077468.html
Copyright © 2011-2022 走看看