POJ3321[苹果树] 树状数组/线段树 + dfs序
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions:39452 | Accepted: 11694 |
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
Sample Input
3
1 2
1 3
3
Q 1
C 2
Q 1
Sample Output
3
2
Source
树状数组
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=,maxm=;
int n,m,Time,tot,lnk[maxn],son[maxm],nxt[maxm],in[maxn],out[maxn];
bool vis[maxn];
struct BLT
{
int c[maxn];
void Clear(){memset(c,,sizeof(c));}
int Lowbit(int x){return x&(-x);}
void Add(int x,int da){while (x<=n) c[x]+=da,x+=Lowbit(x);}
int Get(int x){int sum=; while (x) sum+=c[x],x-=Lowbit(x); return sum;}
}tr;//树状数组
inline int Read()
{
int res=;
char ch=getchar();
while (ch<''||ch>'') ch=getchar();
while (ch>=''&&ch<='') res=res*+ch-,ch=getchar();
return res;
}
void Dfs(int x,int fa)
{
in[x]=++Time;
for (int j=lnk[x]; j; j=nxt[j])
if (son[j]!=fa) Dfs(son[j],x);
out[x]=Time;
}
void Add_e(int x,int y)
{
son[++tot]=y; nxt[tot]=lnk[x]; lnk[x]=tot;
}
int main()
{ n=Read(); Time=;
for (int i=,x,y; i<n; i++) x=Read(),y=Read(),Add_e(x,y),Add_e(y,x);
Dfs(,); m=Read(); tr.Clear();
for (int i=; i<=n; i++) tr.Add(i,);
memset(vis,,sizeof(vis));
for (int i=; i<=m; i++)
{
char ch=getchar();
while (ch!='Q'&&ch!='C') ch=getchar();
int x=Read();
if (ch=='Q') printf("%d\n",tr.Get(out[x])-tr.Get(in[x]-));
else {if (vis[x]) tr.Add(in[x],); else tr.Add(in[x],-); vis[x]=-vis[x];}
}
return ;
}
线段树
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector> using namespace std; #define begin Begin
#define next Next #define REP(i, a, b) for (int i = (a), _end_ = (b); i <= _end_; ++i)
#define EREP(i, a) for (int i = begin[a]; ~i; i = next[i])
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define x first
#define y second
#define pb push_back
#define SZ(x) (int((x).size()))
#define ALL(x) (x).begin(), (x).end() template<typename T> inline bool chkmin(T &a, const T &b){ return a > b ? a = b, : ; }
template<typename T> inline bool chkmax(T &a, const T &b){ return a < b ? a = b, : ; } typedef long long LL; const int dmax = << , oo = 0x3f3f3f3f; int n, m; int begin[dmax], to[dmax], next[dmax], e; int d[dmax], size[dmax], dis; int c[dmax]; inline void init()
{
e = dis = ;
memset(begin, -, sizeof begin);
} inline void add(int x, int y)
{
to[++e] = y;
next[e] = begin[x];
begin[x] = e;
} void dfs(int x, int fa)
{
size[x] = ;
d[x] = ++dis;
EREP(i, x)
if (to[i] == fa) continue;
else
{
dfs(to[i], x);
size[x] += size[to[i]];
}
} #define left x << 1, l, mid
#define right x << 1 | 1, mid + 1, r template<typename T> inline T Mid(const T &x, const T &y){ return (x + y) >> ; } inline void push_up(int x){ c[x] = c[x << ] + c[x << | ]; } void create(int x, int l, int r)
{
if (l == r)
{
c[x] = ;
return;
}
int mid = Mid(l, r);
create(left);
create(right);
push_up(x);
} int query(int x, int l, int r, int s, int t)
{
if (l >= s && r <= t)
return c[x];
int mid = Mid(l, r);
int sum = ;
if (s <= mid)
sum += query(left, s, t);
if (t > mid)
sum += query(right, s, t);
return sum;
} void update(int x, int l, int r, int t)
{
if (l == r)
{
c[x] ^= ;
return;
}
int mid = Mid(l, r);
if (t <= mid)
update(left, t);
else update(right, t);
push_up(x);
} int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
while (scanf("%d", &n) != EOF)
{
init(); REP(i, , n - )
{
int x, y;
scanf("%d%d", &x, &y);
add(x, y);
add(y, x);
}
dfs(, -);
create(, , n);
scanf("%d", &m);
getchar();
REP(i, , m){
char c = getchar();
int x;
scanf("%d", &x);
if (c == 'Q')
printf("%d\n", query(, , n, d[x], d[x] + size[x] - ));
else update(, , n, d[x]);
getchar();
}
}
return ;
}
POJ3321[苹果树] 树状数组/线段树 + dfs序的更多相关文章
- 洛谷P2414 阿狸的打字机 [NOI2011] AC自动机+树状数组/线段树
正解:AC自动机+树状数组/线段树 解题报告: 传送门! 这道题,首先想到暴力思路还是不难的,首先看到y有那么多个,菜鸡如我还不怎么会可持久化之类的,那就直接排个序什么的然后按顺序做就好,这样听说有7 ...
- 树状数组 && 线段树应用 -- 求逆序数
参考:算法学习(二)——树状数组求逆序数 .线段树或树状数组求逆序数(附例题) 应用树状数组 || 线段树求逆序数是一种很巧妙的技巧,这个技巧的关键在于如何把原来单纯的求区间和操作转换为 求小于等于a ...
- hdu1394(枚举/树状数组/线段树单点更新&区间求和)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给出一个循环数组,求其逆序对最少为多少: 思路:对于逆序对: 交换两个相邻数,逆序数 +1 ...
- hdu 1166:敌兵布阵(树状数组 / 线段树,入门练习题)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- hdu 5147 Sequence II【树状数组/线段树】
Sequence IITime Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- 【bzoj2819】Nim(dfs序+树状数组/线段树)
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=2819 首先根据SG定理,可得若每堆石子数量的异或值为0,则后手必胜,反之先手必胜.于是 ...
- 沈阳网络赛J-Ka Chang【分块】【树状数组】【dfs序】
Given a rooted tree ( the root is node 11 ) of NN nodes. Initially, each node has zero point. Then, ...
- HDU 5293 Train chain Problem - 树链剖分(树状数组) + 线段树+ 树型dp
传送门 题目大意: 一颗n个点的树,给出m条链,第i条链的权值是\(w_i\),可以选择若干条不相交的链,求最大权值和. 题目分析: 树型dp: dp[u][0]表示不经过u节点,其子树的最优值,dp ...
- [CSP-S模拟测试]:影魔(树状数组+线段树合并)
题目背景 影魔,奈文摩尔,据说有着一个诗人的灵魂.事实上,他吞噬的诗人灵魂早已成千上万.千百年来,他收集了各式各样的灵魂,包括诗人.牧师.帝王.乞丐.奴隶.罪人,当然,还有英雄.每一个灵魂,都有着自己 ...
随机推荐
- 最简单的注册美区Apple ID方法
最简单方法注册苹果美区Apple ID 1.打开苹果官网链接 苹果官网 2. 点击右下角的 United States 3. 点击图片中的选项 4.点击右上角的选项创建新的Apple ID 注意是新的 ...
- Python---Tkinter---贪吃蛇
# 项目分析: - 构成: - 蛇 Snake - 食物 Food - 世界 World - 蛇和食物属于整个世界 class World: self.snake self.food ------- ...
- 用设计模式来替代if-else
前言 物流行业中,通常会涉及到EDI报文(XML格式文件)传输和回执接收,每发送一份EDI报文,后续都会收到与之关联的回执(标识该数据在第三方系统中的流转状态).这里枚举几种回执类型:MT1101.M ...
- mysql DEFAULT约束 语法
mysql DEFAULT约束 语法 作用:用于向列中插入默认值. 说明:如果没有规定其他的值,那么会将默认值添加到所有的新记录.直线电机 mysql DEFAULT约束 示例 //在 "P ...
- Oulipo【Hash】
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45515 Accepted: 18181 Descript ...
- TCP UDP 包的最大字节
UDP 1500,常见会设置为1024 如: ]; TCP 60*1024 UDP如果设置为1024,但是实际发送超出1024,会直接接不到应答,所以,如果你的其他命令都能正常接收,而这个命令莫名其妙 ...
- UOJ37. 【清华集训2014】主旋律
http://uoj.ac/problem/37 题解 题目是让我们求出有多少个边集可以使这张图强连通. 先补集转化一下,求这张图不强连通的方案数. 我们考虑这样的图缩完点之后的情况,既然不强连通,那 ...
- Codeforce |Educational Codeforces Round 77 (Rated for Div. 2) B. Obtain Two Zeroes
B. Obtain Two Zeroes time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Linux相关基础知识
文件目录 /bin 放置系统执行档的目录,指令可被root与一般账户所使用. /boot 放置开机使用到的文档,包括linux核心档案,开机选单与所需设定档. /dev 任何装置与周边设备都是以档案的 ...
- node.js配置环境变量
今天配置node.js的时候,碰到了配置环境变量的问题 为什么会出这样的问题: 因为我将 node.js 安装到了D盘,(这是个坑,以后一定要安到C盘),当我发现,我的node操作指令无效的时候,知道 ...