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\n",(sum(en[temp1])-sum(sta[temp1]))/2+HasApple[temp1]);
}
} //system("pause");
return 0;
}

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

POJ 3321:Apple Tree 树状数组的更多相关文章

  1. POJ 3321 Apple Tree(树状数组)

                                                              Apple Tree Time Limit: 2000MS   Memory Lim ...

  2. POJ 3321 Apple Tree (树状数组+dfs序)

    题目链接:http://poj.org/problem?id=3321 给你n个点,n-1条边,1为根节点.给你m条操作,C操作是将x点变反(1变0,0变1),Q操作是询问x节点以及它子树的值之和.初 ...

  3. POJ 3321 Apple Tree 树状数组+DFS

    题意:一棵苹果树有n个结点,编号从1到n,根结点永远是1.该树有n-1条树枝,每条树枝连接两个结点.已知苹果只会结在树的结点处,而且每个结点最多只能结1个苹果.初始时每个结点处都有1个苹果.树的主人接 ...

  4. POJ 3321 Apple Tree 树状数组 第一题

    第一次做树状数组,这个东西还是蛮神奇的,通过一个简单的C数组就可以表示出整个序列的值,并且可以用logN的复杂度进行改值与求和. 这道题目我根本不知道怎么和树状数组扯上的关系,刚开始我想直接按图来遍历 ...

  5. 3321 Apple Tree 树状数组

    LIANJIE:http://poj.org/problem?id=3321 给你一个多叉树,每个叉和叶子节点有一颗苹果.然后给你两个操作,一个是给你C清除某节点上的苹果或者添加(此节点上有苹果则清除 ...

  6. POJ--3321 Apple Tree(树状数组+dfs(序列))

    Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22613 Accepted: 6875 Descripti ...

  7. E - Apple Tree(树状数组+DFS序)

    There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. ...

  8. POJ3321 Apple Tree(树状数组)

    先做一次dfs求得每个节点为根的子树在树状数组中编号的起始值和结束值,再树状数组做区间查询 与单点更新. #include<cstdio> #include<iostream> ...

  9. POJ 2486 Apple Tree [树状DP]

    题目:一棵树,每个结点上都有一些苹果,且相邻两个结点间的距离为1.一个人从根节点(编号为1)开始走,一共可以走k步,问最多可以吃多少苹果. 思路:这里给出数组的定义: dp[0][x][j] 为从结点 ...

随机推荐

  1. Linux下杀掉所有得java进程

    --转自https://blog.csdn.net/oppo62258801/article/details/81434038 1.Linux查看所有Java进程 ps -ef | grep java ...

  2. Linux centos7VMware Apache和PHP结合、Apache默认虚拟主机

    一.Apache和PHP结合 httpd主配置文件/usr/local/apache2.4/conf/httpd.conf 启动报错 [root@davery ~]# /usr/local/apach ...

  3. 华为平板暴力禁用wifi

    删除以下配置文件及动态链接库: /system/etc/wifi/* /system/etc/permission/*wifi* /system/lib/*wifi*

  4. 用Hyper-v 在win10下使用Docker-Desktop体验kubernetes

    首先开启Hyper-v ,会自动创建一个交换机. 开启internet共享,自动创建的那个交换机(虚拟的网络适配器)会分配一个默认的IP 192.168.137.1,这个IP你不爽,就用注册表搜索并修 ...

  5. scrapy(创建scrapy工程)报错:“ ImportError:DLL load failed:找不到指定的模块”

    先要确定什么模块找不到 解决方法 windowa环境下加 ( --user) pip install -I cryptography --user

  6. Mini_Linux需要搭的环境

    1.bash:ifconfig:command not found sudo yum install -y net-tools 2.如果Linux系统是通过复制得到  需要更改hostname vi ...

  7. RedHat OpenShift QuickStart 1.1 OpenShift基础

    openshift 提供了命令行工具和web可视化页面,这些工具通过REST API去和openshift交互 一.开始为开发人员使用OpenShift 1. 探索命令行 2. 探索web conso ...

  8. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  9. Windows篇:文件对比软件->"DiffMerge"

    文件对比软件->"DiffMerge" DiffMerge是什么? 如果没有DiffMerge! 想想一下,有两篇10000字的文章,找不同,眼睛都要看花吧.有了DiffMe ...

  10. 《Netlogo多主体建模入门》笔记3

    3- 用“生命游戏”认识Patch     代码:   patches-own[living] to setup clear-all ask patches [ < 0.3[ set pcolo ...