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模拟测试]:影魔(树状数组+线段树合并)
题目背景 影魔,奈文摩尔,据说有着一个诗人的灵魂.事实上,他吞噬的诗人灵魂早已成千上万.千百年来,他收集了各式各样的灵魂,包括诗人.牧师.帝王.乞丐.奴隶.罪人,当然,还有英雄.每一个灵魂,都有着自己 ...
随机推荐
- 过滤函数filter
>>> def validate(usernames): if (len(usernames) > 4) and (len(usernames) < 12): retur ...
- mongdb 数据库
安装mongdb 下载地址 https://www.runoob.com/mongodb/mongodb-window-install.html 检查 mongdb 是否安装成功which mongd ...
- SpringBoot最新教程IDEA版【狂神说Java系列】
Spring Boot入门 1.spring boot是配置好的spring集成框架,约定大于配置 2.微服务:把service拆出来独立运行在各个机器上.看下面这两篇论文 原文地址:http://m ...
- asp.net能否上传文件夹下所有文件
HTML部分 <%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="index.aspx. ...
- [洛谷2257]ZAP-Queries 题解
前言 这道题还是比较简单的 解法 首先将题目转化为数学语言. 题目要我们求的是: \[\sum_{i=1}^a\sum_{j=1}^b[gcd(i,j)=d]\] 按照套路1,我们将其同时除以d转换为 ...
- Redis高可用分布式
阅读目录: 高可用 数据同步 分布式 分布式集群时代 总结 高可用 高可用(High Availability),是当一台服务器停止服务后,对于业务及用户毫无影响. 停止服务的原因可能由于网卡.路由器 ...
- eclipse配置apache tomcat运行时访问路径不需要项目名称
问题:tomcat运行项目默认是要带上项目名的,有时候不想要项目名来访问,如何解决呢? 方法: 1:双击打开tomcat 2:选择Modules,选择你要修改的项目 3:点击Edit,把path修改成 ...
- [BZOJ3236]:[Ahoi2013]作业(莫队+分块)
题目传送门 题目描述 此时已是凌晨两点,刚刚做了$Codeforces$的小$A$掏出了英语试卷.英语作业其实不算多,一个小时刚好可以做完.然后是一个小时可与做完的数学作业,接下来是分别都是一个小时可 ...
- Linux添加目录到环境变量以及添加Sublime Text到环境变量
本文主要介绍了Linux添加目录到环境变量以及添加Sublime Text到环境变量,通过具体的解释说明,让我们从中学到Linux添加目录到环境变量以及添加Sublime Text到环境变量的精髓所在 ...
- sshd使用
sshd服务 1.sshd介绍 sshd为secure shell的简称:可以通过网络在主机中开机shell的服务 连接方式(在客户端):ssh username@ip #文本模式 ...