poj 3321:Apple Tree(树状数组,提高题)

时间:2021-12-24 22:14:22
Apple Tree
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 18623   Accepted: 5629

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?

poj 3321:Apple Tree(树状数组,提高题)

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

Source

POJ Monthly--2007.08.05, Huang, Jinsong

  树状数组,提高题

  有些难度,要转一下弯。

  题意

  一开始给你n-1个边,构成一颗苹果树,默认这个时候每个分叉上都有苹果。接下来有q个操作,这些操作分两种,要不有苹果摘走苹果,没苹果长出苹果,要不求某一分叉上所有的苹果个数。

  思路

  关键是在每一个分叉处记录一个开始时间,和结束时间,表示dfs时经过这个分叉的时间和回到这个分叉的时间。这里的时间其实是dfs遍历次序的定位。这样就相当于有了一个区间,用树状数组就可以求出这个分叉点上的所有苹果的个数。

  注意

  给你的n-1条边,是双向的,即a指向b,b也指向a,是无向图。

  测试数据(来自poj讨论版)


Q
C
Q
Q
C
Q
C
Q
C
Q
ans:

  代码

 #include <iostream>
#include <stdio.h>
using namespace std; #define MAXN 100010 int N;
int cnt=;
int c[MAXN];
int start[MAXN];
int end[MAXN]; struct Node{
int num;
Node* next; //孩子节点
Node()
{
next = NULL;
}
}tree[MAXN]; //临界表 int lowbit(int x)
{
return x & (-x);
} void add(int d,int x)
{
while(d<=N){
c[d] += x;
d += lowbit(d);
}
} int sum(int d)
{
int ans =;
while(d>=){
ans += c[d];
d -= lowbit(d);
}
return ans;
} void dfs(int v) //以r为根节点进行dfs遍历,返整个遍历之后的时间
{
start[v] = ++cnt;
Node* p = tree[v].next;
while(p){
if(start[p->num]==)
dfs(p->num);
p = p->next;
}
end[v] = cnt;
} void addedge(int a,int b) //在苹果树上加分支
{
Node* p = new Node;
p->num = b;
p->next = tree[a].next;
tree[a].next = p;
} int main()
{
int i,q;
scanf("%d",&N);
for(i=;i<N;i++){
int a,b;
scanf("%d%d",&a,&b);
addedge(a,b);
addedge(b,a);
}
dfs(); for(i=;i<=N;i++) //初始化c[]
add(i,); scanf("%d",&q);
while(q--){ //q次操作
char cmd[];
int d;
scanf("%s%d",cmd,&d);
if(cmd[]=='C'){
if(sum(start[d])-sum(start[d]-)==)
add(start[d],-);
else
add(start[d],);
}
else if(cmd[]=='Q'){
printf("%d\n",sum(end[d])-sum(start[d]-));
}
}
return ;
}

Freecode : www.cnblogs.com/yym2013

poj 3321:Apple Tree(树状数组,提高题)的更多相关文章

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

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

  2. POJ 3321 Apple Tree&lpar;树状数组&rpar;

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

  3. POJ 3321 Apple Tree &lpar;树状数组&plus;dfs序&rpar;

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

  4. POJ 3321 Apple Tree 树状数组&plus;DFS

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

  5. 3321 Apple Tree 树状数组

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

  6. POJ 3321:Apple Tree 树状数组

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

  7. POJ--3321 Apple Tree&lpar;树状数组&plus;dfs&lpar;序列&rpar;)

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

  8. E - Apple Tree&lpar;树状数组&plus;DFS序)

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

  9. POJ3321 Apple Tree&lpar;树状数组&rpar;

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

  10. POJ 3928 Ping pong 树状数组模板题

    開始用瓜神说的方法撸了一发线段树.早上没事闲的看了一下树状数组的方法,于是又写了一发树状数组 树状数组: #include <cstdio> #include <cstring&gt ...

随机推荐

  1. jquery知识 内部 外部插入元素

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. AsyncTask加载图片

    http://blog.csdn.net/sodino/article/details/7741674 http://www.cnblogs.com/weisenz/archive/2012/04/1 ...

  3. unslider插件的使用

    深入理解unslider.js源码 最近用到了一个挺好用的幻灯片插件,叫做unslider.js,就想看看怎么实现幻灯片功能,就看看源码,顺便自己也学习学习.看完之后收获很多,这里和大家分享一下. u ...

  4. 【转】Python3—UnicodeEncodeError &&num;39&semi;ascii&&num;39&semi; codec can&&num;39&semi;t encode characters in position 0-1

    转自:https://blog.csdn.net/AckClinkz/article/details/78538462 环境 >>> import sys >>> ...

  5. Unity 新手入门 如何理解协程 IEnumerator yield

    Unity 新手入门 如何理解协程 IEnumerator 本文包含两个部分,前半部分是通俗解释一下Unity中的协程,后半部分讲讲C#的IEnumerator迭代器 协程是什么,能干什么? 为了能通 ...

  6. python——jieba分词过程

    import jieba """函数2:分词函数""" def fenci(training_data): ""&quo ...

  7. Java之Servlet

    Servlet规范了JavaWeb项目的结构Servlet的规范约束了服务器如何来实现Servlet规范,如何解析JavaWeb项目的结构. Java就是通过接口来约束 Servlet规范的jar就在 ...

  8. UESTC - 1172 三句话题意

    题目链接 记一个集合的gcd为该集合内所有数的最大公约数, 求一个给定集合的非空子集的gcd的k次方的期望~ Input 第一行有一个数t,表示数据组数 接下去每组数据两行,第一行两个数n,k(0 & ...

  9. keydown&comma;keypress&comma;keyup三者之间的区别

    最近看了Javascript高级教程中对过滤输入的介绍,想实现比如电话号码中不能包好非数值的字符,而相应文本中插入字符的操作是keypress事件,所以就想通过阻止这个事件的默认事件行为来阻止这个事件 ...

  10. zookeeper未授权访问漏洞

    1.什么是zookeeper? ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,它是集群的管理者,监视着集群中各个节点的状态根据节点提交 ...