3321 Apple Tree 树状数组
LIANJIE:http://poj.org/problem?id=3321
给你一个多叉树,每个叉和叶子节点有一颗苹果。然后给你两个操作,一个是给你C清除某节点上的苹果或者添加(此节点上有苹果则清除没苹果就添加)q询问这个节点的子树上有多少个苹果。
直接dfs遍历一遍,每个节点给一个时间戳,记录一下遍历玩这个节点是的始终时间戳,直接对时间戳进行询问即可。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <stack>
#define loop(s,i,n) for(i = s;i < n;i++)
#define cl(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&-x using namespace std;
const int maxn = ; struct node
{
int u,v,w,next;
}edges[maxn*];
int head[maxn];
bool vis[maxn];
int dfsclock,n,cnt,pt[maxn],c[maxn];
struct time
{
int s,e;
}a[maxn];
void init()
{
int i;
for(i = ;i <= n;i++)
head[i] = -,vis[i] = ,c[i] = ;
cnt = dfsclock = ;
}
int sum(int x)
{
int ret = ;
while(x > )
{
ret+=c[x];
x-=lowbit(x);
}
return ret;
}
void add(int x,int d)
{
while(x <= n)
{
c[x]+= d;
x+=lowbit(x);
}
}
void addedge(int u,int v,int w)
{
int i;
edges[cnt].u = u;
edges[cnt].v = v;
edges[cnt].w = w;
edges[cnt].next = head[u];
head[u] = cnt;
cnt++;
}
void dfs(int u)
{
dfsclock++;
vis[u] = ;
a[u].s = dfsclock;
int i;
for(i = head[u];i != -;i = edges[i].next)
{
int v;
v = edges[i].v;
if(!vis[v])
{
dfs(v);
}
}
a[u].e = dfsclock; }
int main()
{
int q;
while(~scanf("%d",&n))
{ int i;
init();
//memset(head,-1,sizeof(head));
//cnt = 0;
loop(,i,n-)
{
int u,v;
scanf("%d %d",&u,&v);
addedge(u,v,);
addedge(v,u,);
} dfs(); for(i = ;i <= n;i++)
add(i,);
scanf("%d",&q);
while(q--)
{
char s[];
int num;
scanf("%s%d",s,&num);
if(s[] == 'Q')
{
printf("%d\n",sum(a[num].e)-sum(a[num].s-));
}
else
{
int leap;
leap = sum(a[num].s)-sum(a[num].s-);
// cout<<leap<<"**"<<endl;
if(leap)
add(a[num].s,-);
else
add(a[num].s,);
}
} }
return ;
}
3321 Apple Tree 树状数组的更多相关文章
- POJ 3321 Apple Tree(树状数组)
Apple Tree Time Limit: 2000MS Memory Lim ...
- 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 树状数组+DFS
题意:一棵苹果树有n个结点,编号从1到n,根结点永远是1.该树有n-1条树枝,每条树枝连接两个结点.已知苹果只会结在树的结点处,而且每个结点最多只能结1个苹果.初始时每个结点处都有1个苹果.树的主人接 ...
- POJ 3321 Apple Tree 树状数组 第一题
第一次做树状数组,这个东西还是蛮神奇的,通过一个简单的C数组就可以表示出整个序列的值,并且可以用logN的复杂度进行改值与求和. 这道题目我根本不知道怎么和树状数组扯上的关系,刚开始我想直接按图来遍历 ...
- POJ 3321:Apple Tree 树状数组
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22131 Accepted: 6715 Descr ...
- POJ--3321 Apple Tree(树状数组+dfs(序列))
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22613 Accepted: 6875 Descripti ...
- 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. ...
- POJ3321 Apple Tree(树状数组)
先做一次dfs求得每个节点为根的子树在树状数组中编号的起始值和结束值,再树状数组做区间查询 与单点更新. #include<cstdio> #include<iostream> ...
- HDU3333 Turing Tree 树状数组+离线处理
Turing Tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
随机推荐
- Systemd unit generators unit
systemd.generator(7) - Linux manual page http://man7.org/linux/man-pages/man7/systemd.generator.7.ht ...
- nginx 与 浏览器缓存
一.本地静态文件 location /html/{ rewrite ^(html/.*)$ /$1 break; root /data/static; expires 12h; etag off; i ...
- JavaScript中的作用域以及this变量
原文:Scope and this in JavaScript 今天我想简单讨论下关于JavaScript的作用域和this变量."作用域"的概念就是说.我们的代码能够从哪里去訪问 ...
- git-【五】远程仓库
一.准备工作 在了解之前,先注册github[https://github.com/]账号,由于你的本地Git仓库和github仓库之间的传输是通过SSH加密的,所以需要一点设置: 第一步 创建SSH ...
- IETF国标查询
IETF官网 https://www.ietf.org/ rfc国标官网 https://www.ietf.org/standards/rfcs/ rfc国标查询 https://www.rfc-ed ...
- 朴素贝叶斯算法原理及Spark MLlib实例(Scala/Java/Python)
朴素贝叶斯 算法介绍: 朴素贝叶斯法是基于贝叶斯定理与特征条件独立假设的分类方法. 朴素贝叶斯的思想基础是这样的:对于给出的待分类项,求解在此项出现的条件下各个类别出现的概率,在没有其它可用信息下,我 ...
- 运行HBase应用开发程序产生异常,提示信息包含org.apache.hadoop.hbase.ipc.controller.ServerRpcControllerFactory的解决办法
Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties Exception in thread ...
- uva1025 dp
这题说的是给了n个车站 从1号 车站到 n号车站,有m1辆车从1 开往n 有m2 辆车从n 开往1 一个人从1 车站 到达n 车站在T 时刻 要求再 车站呆的时间尽量少 dp[i][j] 表示 在 第 ...
- Java final finally finalize有什么不同
① final 可以用来修饰类.方法.变量, ----final修饰的class代表不可以继承扩展 ----final的变量不可以修改 ----final的方法不可以override ----fina ...
- 2017-2018-2 20165207 实验三《敏捷开发与XP实践》实验报告
java 实验三 实验报告 实验内容 代码规范 不规范的代码可能妨碍阅读,在粘贴下来老师在云班课中设置的提交点一的代码之后,我首先使用了IDEA中Code选项卡的Reformat Code功能规范代码 ...