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?

  题目就是给一棵树,然后动态维护节点子树的值的和。

  本来很简单的一个题,只要dfs记录进入时间和出的时间就好,结果我傻逼的用树链剖分来做得,不过也简单,算是个裸题。

  不过忘了初始化树状数组WA了,%>_<%。。。

代码如下:

// ━━━━━━神兽出没━━━━━━
// ┏┓ ┏┓
// ┏┛┻━━━━━━━┛┻┓
// ┃ ┃
// ┃ ━ ┃
// ████━████ ┃
// ┃ ┃
// ┃ ┻ ┃
// ┃ ┃
// ┗━┓ ┏━┛
// ┃ ┃
// ┃ ┃
// ┃ ┗━━━┓
// ┃ ┣┓
// ┃ ┏┛
// ┗┓┓┏━━━━━┳┓┏┛
// ┃┫┫ ┃┫┫
// ┗┻┛ ┗┻┛
//
// ━━━━━━感觉萌萌哒━━━━━━ // Author : WhyWhy
// Created Time : 2015年07月17日 星期五 15时35分58秒
// File Name : 3321.cpp #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h> using namespace std; const int MaxN=; struct Edge
{
int to,next;
}; Edge E[MaxN<<];
int head[MaxN],Ecou; int fa[MaxN],son[MaxN],siz[MaxN],dep[MaxN],top[MaxN],w[MaxN];
int Tcou; int C[MaxN]; void init()
{
Tcou=;
Ecou=;
w[]=;
top[]=; memset(head,-,sizeof(head));
memset(C,,sizeof(C));
} void addEdge(int u,int v)
{
E[Ecou].to=v;
E[Ecou].next=head[u];
head[u]=Ecou++;
} void dfs1(int u,int pre,int d)
{
int v; dep[u]=d;
fa[u]=pre;
siz[u]=;
son[u]=-; for(int i=head[u];i!=-;i=E[i].next)
if(E[i].to!=pre)
{
v=E[i].to;
dfs1(v,u,d+);
siz[u]+=siz[v]; if(son[u]==- || siz[son[u]]<siz[v])
son[u]=v;
}
} void dfs2(int u)
{
if(son[u]==-)
return; top[son[u]]=top[u];
w[son[u]]=++Tcou; dfs2(son[u]); int v; for(int i=head[u];i!=-;i=E[i].next)
if(E[i].to!=son[u] && E[i].to!=fa[u])
{
v=E[i].to;
top[v]=v;
w[v]=++Tcou;
dfs2(v);
}
} int N; inline int lowbit(int x)
{
return x&(-x);
} void add(int x,int d)
{
while(x<=N)
{
C[x]+=d;
x+=lowbit(x);
}
} int sum(int x)
{
int ret=; while(x>)
{
ret+=C[x];
x-=lowbit(x);
} return ret;
} void update(int u,int v,int d)
{
int f1=top[u],f2=top[v]; while(f1!=f2)
{
if(dep[f1]<dep[f2])
{
swap(f1,f2);
swap(u,v);
} add(w[f1],d);
add(w[u]+,-d);
u=fa[f1];
f1=top[u];
} if(dep[u]>dep[v])
swap(u,v); add(w[u],d);
add(w[v]+,-d);
} bool rem[MaxN]; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int M;
int a,b;
char s[]; while(~scanf("%d",&N))
{
init();
memset(rem,,sizeof(rem)); for(int i=;i<N;++i)
{
scanf("%d %d",&a,&b);
addEdge(a,b);
addEdge(b,a);
} dfs1(,-,);
dfs2(); scanf("%d",&M); for(int i=;i<=N;++i)
update(,i,); while(M--)
{
scanf("%s %d",s,&a); if(s[]=='Q')
printf("%d\n",sum(w[a]));
else
{
update(,a,rem[a]?:-);
rem[a]=!rem[a];
}
}
} return ;
}

(简单) POJ 3321 Apple Tree,树链剖分+树状数组。的更多相关文章

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

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

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

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

  3. POJ 3321 Apple Tree(后根遍历将树转化成序列,用树状数组维护)

    题意:一棵树,有很多分叉,每个分叉上最多有1个苹果. 给出n,接下来n-1行,每行u,v,表示分叉u,v之间有树枝相连.这里数据中u相当于树中的父节点,v相当于子节点. 给出两个操作: 1.C x  ...

  4. POJ 3321 Apple Tree(dfs序树状数组)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=10486 题意:一颗有n个分支的苹果树,根为1,每个分支只有一个苹果,给出n- ...

  5. POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)

    id=10486" target="_blank" style="color:blue; text-decoration:none">POJ - ...

  6. hdu 3966 Aragorn's Story(树链剖分+树状数组/线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意: 给出一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路 ...

  7. Aragorn's Story 树链剖分+线段树 && 树链剖分+树状数组

    Aragorn's Story 来源:http://www.fjutacm.com/Problem.jsp?pid=2710来源:http://acm.hdu.edu.cn/showproblem.p ...

  8. 洛谷 P3384 【模板】树链剖分-树链剖分(点权)(路径节点更新、路径求和、子树节点更新、子树求和)模板-备注结合一下以前写的题目,懒得写很详细的注释

    P3384 [模板]树链剖分 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点最短路径上所有节 ...

  9. POJ 3321 Apple Tree 【树状数组+建树】

    题目链接:http://poj.org/problem?id=3321 Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submiss ...

随机推荐

  1. 转 shell中字分隔的妙用:变量IFS

    IFS 的全称是 Interal Field Separator  ,即"内部区域分隔符",它也是一个内置环境变量,存储着默认的文本分隔符,默认下这分隔符是空格符(space  c ...

  2. Android OpenGL ES(七)基本几何图形定义 .

    在前面Android OpenGL ES(六):创建实例应用OpenGLDemos程序框架 我们创建了示例程序的基本框架,并提供了一个“Hello World”示例,将屏幕显示为红色. 本例介绍Ope ...

  3. PHP配置安全小技巧

  4. Android摄像头:只拍摄SurfaceView预览界面特定区域内容(矩形框)---完整(原理:底层SurfaceView+上层绘制ImageView)

    Android摄像头:只拍摄SurfaceView预览界面特定区域内容(矩形框)---完整实现(原理:底层SurfaceView+上层绘制ImageView) 分类: Android开发 Androi ...

  5. 通过ReconstructMe实现3D扫描

    实物3D建模 目前在3D游戏制作过程中,需要专业人士花几天甚至数星期的时间,借助于Autodesk 3ds Max和Maya等昂贵的软件工具制作3D模型.纹理和动画.游戏制作中经常使用一种方法,即设计 ...

  6. hdu_2222_Keywords Search(AC自动机板子)

    题目连接:hdu_2222_Keywords Search 存个自己写的AC自动机 #include<cstdio> #include<cstring> #define F(i ...

  7. DataBinding注意事项Error parsing XML: duplicate attribute以及如何在listview中使用DataBinding

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  8. acm的第一场比赛的总结

    6.4-6.5号很激动的去湖南湘潭打了一场邀请赛,这是第一次acm的旅程吧.毕竟大一上册刚开始接触c,然后现在就能抱着学长的大腿(拖着学长的后腿)打比赛,也是有一点小小的激动. 第一天很早就起床了,由 ...

  9. stock 仓位

    别胆大求多,不轻信盲从.抓住几只自己长期关注并看好的股票.将这几只股票选为自选股,而其他股,不管是机构推荐还是股评荐股,都要谨慎,不轻易听从. 巧用“三三制”,根据趋势控制仓位.当不知道是在涨还是在跌 ...

  10. 正则抓取网页所有href和src

    根据抓取的页面,用正则来匹配页面href和src string UserAgent = "Mozilla/5.0 (Windows NT 5.2; rv:29.0) Gecko/201001 ...