POJ3321 Apple Tree(树状数组)
先做一次dfs求得每个节点为根的子树在树状数组中编号的起始值和结束值,再树状数组做区间查询 与单点更新。
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
using namespace std;
typedef long long LL;
const int N = , INF = 0x3F3F3F3F;
#define MS(a, num) memset(a, num, sizeof(a))
int leftid[N], rightid[N];
struct Node{
int to,next;
}edge[ * N];
int head[N],tot;
void init(){
memset(head, -, sizeof(head));
tot = ;
}
inline void addedge(int u, int to){
edge[tot].to=to;
edge[tot].next=head[u];
head[u]=tot++;
}
int n, m;
bool sta[N];
int C[N];
inline int lowbit(int x){
return x&-x;
}
inline void add(int x, int val){
for(int i=x;i<=n;i+=lowbit(i)){
C[i] += val;
}
}
inline int sum(int x){
int ret = ;
for(int i=x;i>;i-=lowbit(i)){
ret+=C[i];
}
return ret;
} int cnt = ; void dfs(int u, int fa){
leftid[u] = cnt + ;
for(int i = head[u]; ~i ; i = edge[i].next){
int v = edge[i].to;
if(v != fa){
dfs(v, u);
}
}
rightid[u] = ++cnt;
} int main(){
while(~scanf("%d", &n) && n){
MS(C, );
init();
for(int i = ; i <= n; i++){
add(i, );
sta[i] =;
}
int u, v;
for(int i = ; i < n -; i++){
scanf("%d %d", &u, &v);
addedge(u, v);
addedge(v, u);
}
cnt = ;
dfs(, -);
cin>>m;
char ope; while(m--){
scanf(" %c %d", &ope, &u);
int l = leftid[u], r = rightid[u];
if(ope == 'C'){
if(sta[r]){
sta[r] = ;
add(r, -);
}else{
sta[r] = ;
add(r, );
}
}else{
int tp = sum(r) - sum(l - );
printf("%d\n", tp);
}
} }
return ;
}
POJ3321 Apple Tree(树状数组)的更多相关文章
- POJ--3321 Apple Tree(树状数组+dfs(序列))
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22613 Accepted: 6875 Descripti ...
- POJ 3321 Apple Tree(树状数组)
Apple Tree Time Limit: 2000MS Memory Lim ...
- POJ 3321:Apple Tree 树状数组
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22131 Accepted: 6715 Descr ...
- 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. ...
- POJ 3321 Apple Tree 树状数组+DFS
题意:一棵苹果树有n个结点,编号从1到n,根结点永远是1.该树有n-1条树枝,每条树枝连接两个结点.已知苹果只会结在树的结点处,而且每个结点最多只能结1个苹果.初始时每个结点处都有1个苹果.树的主人接 ...
- POJ 3321 Apple Tree (树状数组+dfs序)
题目链接:http://poj.org/problem?id=3321 给你n个点,n-1条边,1为根节点.给你m条操作,C操作是将x点变反(1变0,0变1),Q操作是询问x节点以及它子树的值之和.初 ...
- POJ 3321 Apple Tree 树状数组 第一题
第一次做树状数组,这个东西还是蛮神奇的,通过一个简单的C数组就可以表示出整个序列的值,并且可以用logN的复杂度进行改值与求和. 这道题目我根本不知道怎么和树状数组扯上的关系,刚开始我想直接按图来遍历 ...
- 3321 Apple Tree 树状数组
LIANJIE:http://poj.org/problem?id=3321 给你一个多叉树,每个叉和叶子节点有一颗苹果.然后给你两个操作,一个是给你C清除某节点上的苹果或者添加(此节点上有苹果则清除 ...
- HDU3333 Turing Tree 树状数组+离线处理
Turing Tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
随机推荐
- mysql 同步
http://dev.mysql.com/doc/refman/5.5/en/replication-howto.html http://blog.csdn.net/mycwq/article/det ...
- BOM基础部分
打开.关闭窗口 •open –蓝色理想运行代码功能 •close –关闭时提示问题 常用属性 •window.navigator.userAgent •window.location 窗口尺寸 ...
- Nginx 使用 sever 段规则屏蔽恶意 User Agent
相对于 Apache,Nginx 占用的系统资源更少,更适合 VPS 使用.恶意的 User Agent 无处不在,博客更换到 WordPress 没几天,就被 SPAM(垃圾留言)盯上,又被暴力破解 ...
- Service错误
错误日志: Caused by: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act= ...
- IntelliJ IDEA License
http://jetbrains.tech/ http://jetbrains.tencent.click/
- java实现远程储存读取文件
import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileN ...
- CLR环境中内置了几个常用委托(转)
CLR环境中给我们内置了几个常用委托Action. Action<T>.Func<T>.Predicate<T>,一般我们要用到委托的时候,尽量不要自己再定义一 个 ...
- jQuery之元素筛选
1.eq() 筛选指定索引号的元素2.first() 筛选出第一个匹配的元素3.last() 筛选出最后一个匹配的元素4.hasClass() 检查匹配的元素是否含有指定的类5.filter() ...
- Django~Models1
不用数据库不用Models 省却 --------------- startapp urls +链接 view以下 ---------------- 添加Model 在project/settings ...
- 【leetcode】Text Justification(hard) ☆
Given an array of words and a length L, format the text such that each line has exactly L characters ...