• POJ 3321:Apple Tree 树状数组


    Apple Tree
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 22131   Accepted: 6715

    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
    "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
    "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

    题意是有一个树,树根始终是1。最开始每一个节点上都上有苹果,然后就开始折腾了。

    操作为Q时是询问,该分叉上现在一共有多少个苹果。

    操作为C时,如果当前该节点上有苹果,摘掉。如果当前该节点没有苹果,长出一个来。

    先dfs出每一个节点的起始时间和结束时间,然后根据起始时间和结束时间使用树状数组求和的思想就能得到结果。

    代码:

    #include <iostream>  
    #include <algorithm>  
    #include <cmath>  
    #include <vector>  
    #include <string>  
    #include <cstring>  
    #pragma warning(disable:4996)  
    using namespace std;
    
    #define MY_MAX 220000
    int C[MY_MAX];
    vector<vector<int>> G(MY_MAX/2);
    int HasApple[MY_MAX/2];
    int sta[MY_MAX];
    int en[MY_MAX];
    int ncount=0;
    int n,m;
    
    void dfs(int v)
    {
    	sta[v] = ++ncount;
    	for(int i=0;i<G[v].size();i++)
    	{
    		dfs(G[v][i]);
    	}
    	en[v] = ++ncount;
    }
    
    int lowbit(int x)  
    {  
        return x&(-x);  
    }  
      
    void add(int x,int val)  
    {  
        while(x<=en[1]+1)  
        {  
            C[x] = C[x] + val;  
            x=x+lowbit(x);  
        }  
    }  
      
    int sum(int x)  
    {  
        int res=0;  
        while(x>0)  
        {  
            res+=C[x];  
            x=x-lowbit(x);
        }  
        return res;  
    }  
    int main()
    {
    	//freopen("i.txt","r",stdin);
    	//freopen("o.txt","w",stdout);
    	
    	int i,temp1,temp2;
    	char oper[5];
    
    	scanf("%d",&n);
    	for(i=1;i<=n-1;i++)
    	{
    		scanf("%d%d",&temp1,&temp2);
    		G[temp1].push_back(temp2);
    		HasApple[i]=1;
    	}
    	HasApple[n]=1;
    	dfs(1);
    	scanf("%d",&m);
    	memset(C,0,sizeof(C));
    	for(i=1;i<=en[1]+1;i++)
    	{
    		add(i,1);
    	}
    	for(i=1;i<=m;i++)
    	{
    		scanf("%s%d",oper,&temp1);
    		if(oper[0]=='C')
    		{
    			if(HasApple[temp1])
    			{
    				HasApple[temp1]=0;
    				add(sta[temp1]+1,-1);
    				add(en[temp1]+1,-1);
    			}
    			else
    			{
    				HasApple[temp1]=1;
    				add(sta[temp1]+1,1);
    				add(en[temp1]+1,1);
    			}
    		}
    		else if(oper[0]=='Q')
    		{
    			printf("%d
    ",(sum(en[temp1])-sum(sta[temp1]))/2+HasApple[temp1]);
    		}
    	}
    
    	//system("pause");
    	return 0;
    }



    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    Did not find handler method for springMVC资源文件扫描不到---关于spring的那些坑
    mysql中OPTIMIZE TABLE的作用
    Linux环境下apache性能测试工具ab使用详解
    sqlite数据库 adb 从配置到查询表中数据全过程-----献给初学的自己
    c3p0参数解释
    linux下如何启动/停止/重启mysql:
    [MySQL] 变量(参数)的查看和设置
    mysql运行参数详解
    单例模式 理解,简单通透
    this的一些场景
  • 原文地址:https://www.cnblogs.com/lightspeedsmallson/p/4899538.html
Copyright © 2020-2023  润新知