博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 3321(树状数组)
阅读量:4975 次
发布时间:2019-06-12

本文共 2335 字,大约阅读时间需要 7 分钟。

Apple Tree
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 24954   Accepted: 7447

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

31 21 33Q 1C 2Q 1

Sample Output

32 题意:一棵树上有n个结点,每个节点上面都有一个苹果,现在给两个操作: C x 如果第 x 个节点上存在苹果,则摘掉,如果没有,那么会长一个出来。 Q x 问 x 的子树里面有多少个苹果。 题解:DFS进行节点的重新标记,求出每个结点的"管辖范围",然后每次更新左区间,求和就用sum(R[x]) - sum(L[x]-1)
#include 
#include
#include
#include
#include
using namespace std;const int N = 100005;int L[N],R[N],c[N]; ///[L[i],R[i]] 是第i个点的管辖范围bool flag[N];int n,key;vector
edge[N];int lowbit(int x){ return x&(-x);}void update(int idx,int v){ for(int i=idx;i<=n;i+=lowbit(i)){ c[i]+=v; }}int getsum(int idx){ int sum = 0; for(int i=idx;i>=1;i-=lowbit(i)){ sum+=c[i]; } return sum;}void dfs(int idx){ L[idx] = key; for(int i=0;i

 

转载于:https://www.cnblogs.com/liyinggang/p/5655213.html

你可能感兴趣的文章
extjs动态改变样式
查看>>
宏定义
查看>>
笔记:git基本操作
查看>>
生成php所需要的APNS Service pem证书的步骤
查看>>
JavaWeb之JSON
查看>>
HOT SUMMER 每天都是不一样,积极的去感受生活 C#关闭IE相应的窗口 .
查看>>
windows平台上编译mongdb-cxx-driver
查看>>
optionMenu-普通菜单使用
查看>>
2016-2017-2点集拓扑作业[本科生上课时]讲解视频
查看>>
【MemSQL Start[c]UP 3.0 - Round 1 C】 Pie Rules
查看>>
Ognl中“%”、“#”、“$”详解
查看>>
我对应用软件——美团的看法
查看>>
执行了的程序,才是你的程序.
查看>>
struts2.x + Tiles2.x读取多个xml 配置文件
查看>>
表单校验之datatype
查看>>
python第六篇文件处理类型
查看>>
ubuntu16系统磁盘空间/dev/vda1占用满的问题
查看>>
grid网格布局
查看>>
JSP常用标签
查看>>
九涯的第一次
查看>>