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模拟测试]:影魔(树状数组+线段树合并)
题目背景 影魔,奈文摩尔,据说有着一个诗人的灵魂.事实上,他吞噬的诗人灵魂早已成千上万.千百年来,他收集了各式各样的灵魂,包括诗人.牧师.帝王.乞丐.奴隶.罪人,当然,还有英雄.每一个灵魂,都有着自己 ...
随机推荐
- 【NOIP2016提高A组8.12】礼物
题目 夏川的生日就要到了.作为夏川形式上的男朋友,季堂打算给夏川买一些生日礼物. 商店里一共有种礼物.夏川每得到一种礼物,就会获得相应喜悦值Wi(每种礼物的喜悦值不能重复获得). 每次,店员会按照一定 ...
- Vue在移动端App中使用的问题总结
1.客户端中弹出键盘使得fixed布局错乱 Vue 在移动端中使用,当弹出键盘时,fixed 布局的元素可能会被键盘顶起. 例子图示及解决方法参考:https://blog.csdn.net/qq_3 ...
- Supervised pre-trainning有监督预训练
如我们有一个分类任务,数据库很小,这时还是需要通过预训练来避免深度模型的过拟合问题的,只不过预训练是通过在一个大的数据库上(比如imagenet),通过有监督的训练来完成的.这种有监督预训练加小的数据 ...
- Java中高级面试题(1)
List和Set比较,各自的子类比较 对比一:Arraylist与LinkedList的比较 1.ArrayList是实现了基于动态数组的数据结构,因为地址连续,一旦数据存储好了,查询操作效率会比较高 ...
- LUOGU P2569 [SCOI2010]股票交易(单调队列优化dp)
传送门 解题思路 不难想一个\(O(n^3)\)的\(dp\),设\(f_{i,j}\)表示第\(i\)天,手上有\(j\)股的最大收益,因为这个\(dp\)具有单调性,所以\(f_i\)可以贪心的直 ...
- C++中一些容易迷惑的语法点总结
#include<iostream> #include<cstring> using namespace std; int main(){ ][]={{,,},{,,}}; ] ...
- wap开发tips
1.overflow-x 这真的是一个大坑,一旦你在body或者html上用了这个属性,对不起,如果你的页面出现滚动条的话,那就会出现莫名其妙的bug,滑动页面的时候fix在顶部或者底部的会挡住. 解 ...
- hypermesh中怎么设置支反力(反作用力)
Analysis page >> Control cards >> Global output request 勾选 SPCF 和 GPFORCE .
- SQL Server中数据去重单列数据合并
sql中我们偶尔会用到对数据进行合并,但其中的某一列数据要进行合并的操作: 如下图,一个用户有多个角色ID,如果我们想要统计一个用户有哪些角色,并且以单列的展现形式,单纯的用DISTINCT去掉肯定是 ...
- HTML-空格字符实体
不换行空格,全称No-Break Space,它是最常见和我们使用最多的空格,大多数的人可能只接触了 ,它是按下space键产生的空格.在HTML中,如果你用空格键产生此空格,空格是不会累加的(只 ...