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模拟测试]:影魔(树状数组+线段树合并)
题目背景 影魔,奈文摩尔,据说有着一个诗人的灵魂.事实上,他吞噬的诗人灵魂早已成千上万.千百年来,他收集了各式各样的灵魂,包括诗人.牧师.帝王.乞丐.奴隶.罪人,当然,还有英雄.每一个灵魂,都有着自己 ...
随机推荐
- E. Boxers
E. Boxers 给定N个数字,每个数字可以加一或者减一 使得结果集合中不同数字个数最多 贪心 用桶装数 假如相同的数字$i$超过三个,则上面$i+1$,下面$i-1$都可以分一个 如果相同数字$i ...
- 进阶3: zookeeper-3.4.9.tar.gz和hbase-1.2.4-bin.tar.gz 环境搭建(hbase 伪分布式)
前提条件: 成功安装了 jdk1.8, hadoop2.7.3 注意条件: zookeeper,hbase 版本必须要和hadoop 安装版本相互兼容,否则容易出问题: 本次:安装包 zookee ...
- Oracle And子句
Oracle And子句 作者:初生不惑 Oracle基础 评论:0 条 Oracle技术QQ群:175248146 在本教程中,将学习如何使用Oracle AND运算符来组合两个或更多的布尔表达式. ...
- iOS-Swizzle
最后更新:2017-06-21 一.先说结论 void swizzleMethod(Class cls, SEL originalSelector, SEL swizzledSelector) { M ...
- Cocoapods 版本
查看当前安装的版本 gem list 卸载版本 gem uninstall cocoapods 安装 gem install cocoapods gem install cocoapods -v 1. ...
- G-sensor概述及常用概念整理【转】
本文转载自:http://www.jianshu.com/p/d471958189a0?nomobile=yesG 本文对G-sensor进行整理,先介绍G-sensor的一些基本概念,再具体讲解BO ...
- LinkedTransferQueue 源码分析
LinkedTransferQueue LinkedTransferQueue 能解决什么问题?什么时候使用 LinkedTransferQueue? 1)LinkedTransferQueue 是基 ...
- drf 视图源码详解
目录 mixin类和Generic类 CreateModelMixin 创建 ListModelMixin - 查看多条数据 RetrieveModelMixin 获取单条数据 UpdateModel ...
- day51—JavaScript绑定事件
转换学开发,代码100天——2018-05-06 今天学习JavaScript的绑定事件.因为浏览器的原因绑定事件需要考虑兼容性问题. attachEvent IE浏览器 ,ie9以上事件执行 ...
- Powershell指令集_1
目录 目录 前言 程序进度条 Write-Progress 执行表达式 Invoke-Expression 表格化打印信息 Format-Table 获取系统服务 Get-WmiObject 循环 F ...