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 ...
随机推荐
- BBS - 文章评论
一.文章评论 <div class="comment_region"> <div class="row"> <div class= ...
- vue - 指令系统
指令系统: 所谓指令系统,大家可以联想咱们的cmd命令行工具,只要我输入一条正确的指令,系统就开始干活了. 在vue中,指令系统,设置一些命令之后,来操作我们的数据属性,并展示到我们的DOM上. 1. ...
- Grafana+Prometheus监控
添加模板一定要看说明以及依赖 监控redis https://blog.52itstyle.com/archives/2049/ http://www.cnblogs.com/sfnz/p/65669 ...
- 《码农周刊》干货精选--Python篇(转)
原文:http://baoz.me/446252 码农周刊,本人有修改 Python标准库,第三方库 按功能进行了分类,之前有一Pythoner说there is a library for ev ...
- saltstack master minion 配置文件
Master端的配置是修改/etc/salt下master配置文件.以下是Master端常用的配置. interface: 指定bind 的地址(默认为0.0.0.0) publish_port: 指 ...
- leetcode——Search for a Range 排序数组中寻找目标下标范围(AC)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- arthas使用介绍
背景: 一次线上问题的综合排查排查,两个相同的系统的某个模块,数据量更少的系统查询更慢. 先说下整体思路: 查看系统整理负载,网络有100左右毫秒的延迟,看起来影响不大 查看正序运行整体情况,一次查询 ...
- zookeeper集群-solrcloud集群
本文只写具体的搭建过程,具体原理请看官网文档.国内博客都是基本上都是通过tomcat搭建的solr,本文是通过内部集成的jetty容器搭建. 一.zookeeper集群搭建 1.安装JAVA环境,版本 ...
- rails性能优化
1,使用Unicorn或者Thin服务器替代默认的webrick.2,静态资源压缩合并,放到云存储上.3,同时可以使用rails的Turbolinks,使用js替换title和body,但也带来了js ...
- 字典树 trie
Trie树 Trie树,就是字母树.Trie树是多叉树,每个节点为一个字母.其根节点为象征节点(就是说没有含义,但是存在这个节点),从根节点开始建立,每个节点至多为26个子节点(不要我说 ...