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模拟测试]:影魔(树状数组+线段树合并)
题目背景 影魔,奈文摩尔,据说有着一个诗人的灵魂.事实上,他吞噬的诗人灵魂早已成千上万.千百年来,他收集了各式各样的灵魂,包括诗人.牧师.帝王.乞丐.奴隶.罪人,当然,还有英雄.每一个灵魂,都有着自己 ...
随机推荐
- Kafka---系统学习
1.Topics 1.1.Topic 就是 数据主题: 1.2.作用:数据记录 发布的地方,用来 区分 业务系统: 1.3.每个Topic 可以有多个 消费者 订阅它的数据: 1.4.每个T ...
- apply()的使用
apply()方法的另一种使用,就是可以代替一次for循环 在封装 document.getElementsByTagName的时候,需要把伪数组转为一个真的数组,其是也不是真的数组 typeof 出 ...
- codevs 2039 骑马修栅栏 USACO x
题目描述 Description Farmer John每年有很多栅栏要修理.他总是骑着马穿过每一个栅栏并修复它破损的地方. John是一个与其他农民一样懒的人.他讨厌骑马,因此从来不两次经过一个栅栏 ...
- [USACO17JAN]Promotion Counting 题解
前言 巨佬说:要有线段树,结果蒟蒻打了一棵树状数组... 想想啊,奶牛都开公司当老板了,我还在这里码代码,太失败了. 话说奶牛开个公司老板不应该是FarmerJohn吗? 题解 刚看到这道题的时候竟然 ...
- ORB an efficient alternative to SIFT or SURF
AbstractFeature matching is at the base of many computer vision problems, such as object recognition ...
- [CF1093E]Intersection of Permutations:树套树+pbds
分析 裸的二维数点,博主用树状数组套平衡树写的,顺便pbds真好用. Update on 2018/12/20:再解释一下为什么是二维数点,第一维是\(la \leq i \leq ra\),第二维是 ...
- Actor Roles 图示
Udemy上的教程<Unreal Multiplayer Mastery - Online Game Development in C++>中对Actor Roles的总结非常直观到位,一 ...
- POJ 1432 Decoding Morse Sequences (DP)
Decoding Morse Sequences 题目链接: http://acm.hust.edu.cn/vjudge/contest/129783#problem/D Description Be ...
- HTML&&CSS基础知识点整理
HTML&&CSS基础知识点整理 一.WEB标准:一系列标准的集合 1. 结构(Structure):html 语言:XHTML[可扩展超文本标识语言]和XML[可扩展标记语言] 2. ...
- php正则匹配汉字提取其它信息剔除和验证邮箱
正则匹配汉字提取其它信息剔除demo <?php //提取字符串中的汉字其余信息剔除 $str='te,st 测 .试,.,.?!::·…~&@#,.?!:;.……-&@#“” ...