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 ...
随机推荐
- spring boot实战(第一篇)第一个案例
版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] spring boot实战(第一篇)第一个案例 前言 写在前面的话 一直想将spring boot相关内容写成一个系列的 ...
- Webservice实践(七)CXF 与Spring结合+tomcat发布
上一节介绍了如何使用CXF 来发布服务,但是没有介绍使用web 容器来发布,很多项目需要用tomcat 这样的容器来发布.另外本节将介绍CXF 与spring 结合的方法. 一 目标: 1.利用spi ...
- Oracle SQL开发 之 Select语句完整的执行顺序
查询语句语法: Select 属性 From 表 Where 条件 Group by 分组条件 Having 分组选择条件 Order by 排序条件 1.from子句组装来自不同数据源的数据: 2. ...
- Delphi中那些容易混淆的基础(@、^、Addr、Pointer,Move、CopyMemory,GetMem和FreeMem、GetMemory和FreeMemory、New和Dispose、StrAlloc和StrDispose、AllocMem)
@.^.Addr.Pointer Delphi(Pascal)中有几个特殊的符号,如@.^等,弄清楚这些符号的运行,首先要明白Delphi指针的一些基础知识:指针,是一个无符号整数(unsigned ...
- 【Python】如何取到input中的value值?
练习:取到下方链接下所有海贼王的下载链接. # coding=utf-8 from selenium import webdriver from time import sleep import ke ...
- sql用法
1: SELECT `SCHEMA_NAME` FROM `information_schema`.`SCHEMATA`; 查询sql中的数据库名 2: select * from for ...
- WebStorm mac下如何安装WebStorm + 破解
1.下载软件最好的地址就是官网了下载地址 选择好系统版本以后点击DOWNLOAD 下载Webstorm 2.安装双击下载好的安装包.将WebStromt拖入application文件夹,然后在La ...
- with语句与__enter__,__exit__
class Foo(object): def func(self): print("func") pass def __enter__(self): print("ent ...
- 2.8 The Object Model -- Enumerables
在Ember.js中,枚举是包含许多子对象的任何对象,并允许你使用Ember.Enumerable API和那些子对象一起工作.在大部分应用程序中最常见的可枚举是本地JS数组,Ember.js扩展到符 ...
- 3.1 Templates -- Handlerbars Basics(Handlerbars基础知识)
一.简介 Ember.js使用Handlerbars模板库来强化应用程序的用户界面.它就像普通的HTML,但也给你嵌入表达式去改变现实的内容. Ember使用Handlerbars并且用许多新特性去扩 ...