Apple Tree:http://poj.org/problem?id=3321

题意:

  告诉你一棵树,每棵树开始每个点上都有一个苹果,有两种操作,一种是计算以x为根的树上有几个苹果,一种是转换x这个点上的苹果,就是有就去掉,没有就加上。

思路:

  先对树求一遍dfs序,每个点保存一个l,r。l是最早到这个点的时间戳,r是这个点子树中的最大时间戳,这样就转化为区间问题,可以用树状数组,或线段树维护区间的和。

#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert>
using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
// #pragma GCC diagnostic error "-std=c++11"
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3) #define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue
#define max3(a,b,c) max(max(a,b),c) typedef long long ll;
typedef unsigned long long ull; typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e9+;
const double esp = 1e-;
const double PI=acos(-1.0); template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} /*-----------------------showtime----------------------*/
const int maxn = ;
struct edge
{
int to,nx;
}e[maxn];
int h[maxn],all;
void addedge(int u,int v){
e[all].to = v;
e[all].nx = h[u];
h[u] = all++;
}
int sum[maxn],vis[maxn];
char op[];
struct node
{
int l,r;
}a[maxn];
int tot = ;
void dfs(int x,int fa){
a[x].l = tot;
for(int i=h[x]; ~i; i = e[i].nx){
int v = e[i].to;
tot++;
if(v!=fa){
dfs(v,x);
}
}
a[x].r = ++tot;
}
int lowbit(int x){
return x&(-x);
}
void add(int x,int c){
while(x < maxn){
sum[x] += c;
x += lowbit(x);
}
}
int getsum(int x){
int res = ;
while(x>){
res += sum[x];
x -= lowbit(x);
}
return res;
}
int main(){
int n,m,x;
scanf("%d", &n);
memset(h,-,sizeof(h));
for(int i=; i<n; i++){
int u,v;
scanf("%d%d",&u, &v);
addedge(u,v);
addedge(v,u);
}
dfs(,-);
for(int i=; i<=n; i++){
vis[i] = ;
add(a[i].l , );
}
scanf("%d", &m);
while(m--){
scanf("%s%d", op,&x);
if(op[] == 'Q'){
printf("%d\n", getsum(a[x].r) - getsum(a[x].l-));
}
else {
if(vis[x] == ){
add(a[x].l,-);
vis[x] = ;
}
else {
add(a[x].l,);
vis[x] = ;
}
}
}
return ;
}

POJ 3321

POJ3321 - Apple Tree DFS序 + 线段树或树状数组的更多相关文章

  1. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  2. [poj3321]Apple Tree(dfs序+树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26762   Accepted: 7947 Descr ...

  3. poj 3321 Apple Tree dfs序+线段树

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K       Description There is an apple tree outsid ...

  4. codechef T6 Pishty and tree dfs序+线段树

    PSHTTR: Pishty 和城堡题目描述 Pishty 是生活在胡斯特市的一个小男孩.胡斯特是胡克兰境内的一个古城,以其中世纪风格 的古堡和非常聪明的熊闻名全国. 胡斯特的镇城之宝是就是这么一座古 ...

  5. POJ 3321 Apple Tree DFS序 + 树状数组

    多次修改一棵树节点的值,或者询问当前这个节点的子树所有节点权值总和. 首先预处理出DFS序L[i]和R[i] 把问题转化为区间查询总和问题.单点修改,区间查询,树状数组即可. 注意修改的时候也要按照d ...

  6. POJ 3321 Apple Tree DFS序+fenwick

    题目大意:有一颗长满苹果的苹果树,有两个操作. 1.询问以一个点为根的子树中有多少个苹果. 2.看看一个点有没有苹果,假设没有苹果.那么那里就立即长出一个苹果(= =!):否则就把那个苹果摘下来. 思 ...

  7. Codeforces Round #225 (Div. 2) E. Propagating tree dfs序+-线段树

    题目链接:点击传送 E. Propagating tree time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  8. codeforces 620E. New Year Tree dfs序+线段树+bitset

    题目链接 给一棵树, 每个节点有颜色, 两种操作, 一种是将一个节点的子树全都染色成c, 一种是查询一个节点的子树有多少个不同的颜色, c<=60. 每个节点一个bitset维护就可以. #in ...

  9. CodeForces 620E:New Year Tree(dfs序+线段树)

    E. New Year Treetime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputout ...

随机推荐

  1. 这半年时间学Mysql的总结

    一条sql语句的执行流程 select * from t where id=1 1.mysql执行一条查询语句的流程 1.1客户端输入用户名密码连接mysql服务器 1.2查询这条sql语句有没有对应 ...

  2. 创建软RAID5

    一  创建4块硬盘组软  RAID5  新增四块20G的新硬盘,将四块硬盘分别分区,全部大小都分为一个区,并改  id  为 “ fd ”      1创建阵列mdadm -Cv /dev/md5 - ...

  3. 昏睡了8年的我带着第一个微信小程序今年醒了

    工作8年之久的我今年算是彻底长进了,以前是知道自己的水平不咋地,但是没什么行动,理由是3年抱2娃,需要照顾孩子. 去年年底偶然一次看技术贴的时候,看到了博客园这个平台,看了很多大牛们的经历,也知道公司 ...

  4. SpringBoot 使用JPA时解决no session的方法

    1.在application.yml中添加 spring.jpa.open-in-view: true 2.在使用查询的方法添加 @Transactional

  5. C#使用代理IP发送请求

    https://www.cnblogs.com/benbenfishfish/p/5830149.html   获取可代理的IP https://www.cnblogs.com/ShalenChe/p ...

  6. Java中...的作用

    Java中...的作用,代表接收若干个相同类型的参数 public void testFunction(int...arr){    //接收若干个int类型的参数     for (int i:ar ...

  7. hadoop学习(四)----windows环境下安装hadoop

    因为我们不能在线上环境进行调试hadoop,这样就只能在本地先调试好了再放到线上去啦.我本地是windows环境,今天先记下windows下搭建hadoop2.7的步骤. 1 本地环境 windows ...

  8. Ubuntu 18.04 LTS版本 GoldenDict安装与配置

    为何安装? GoldenDict是一款Linux下很好用的词典软件,其具有的关于词典的裁剪功能使得用户能够方便地对各种词典进行添加或删除,其具有的屏幕取词功能能够帮助用户方便地进行翻译,其具有的网络源 ...

  9. 从零开始学习GDI+ (一)

    前言: GDI+从Windows XP操作系统(大概2002-2003年)开始引入的,现在都9102年了,再学习这么古老的技术肯定是过时了.windows桌面程序没落了,随着移动的兴起,用户被惯坏了, ...

  10. 强烈推荐优秀的Vue UI组件库

    Vue 是一个轻巧.高性能.可组件化的MVVM库,API简洁明了,上手快.从Vue推出以来,得到众多Web开发者的认可.在公司的Web前端项目开发中,多个项目采用基于Vue的UI组件框架开发,并投入正 ...