poj 3321 Apple Tree 线段树
来源:互联网 发布:宝矿力水特 知乎 编辑:程序博客网 时间:2024/05/14 11:03
一棵多叉树,每一个节点上都可以长出或摘掉一个苹果,节点从1至n标记,根为1节点
C为改变该树枝上苹果有无
Q为问次节点及其根节点共有多少 苹果
后续遍历多叉树,给每一个节点按遍历顺序重新标号,并标记每一个节点的子树的起始位置,再利用线段树维护和
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;const int maxn = 100010;struct node{ //每个节点其子树在遍历后顺序的起始位置,即询问此节点及其子树时,线段树中对应的lef与rig位置 int lef, rig, id; // 遍历后坐标}tree[maxn]; //yuanint root[maxn]; // root[原] = 遍历后坐标int n, q, v;struct E{ //邻接表存树 int v, next;}edge[maxn];int pre[maxn], apple[maxn]; //apple记录次节点之前有误苹果struct SEG{ int lef, rig, mid, ans;}seg[maxn*4];void init(){ int i, j, k, aa, bb, value = 0; memset( pre, -1, sizeof(pre)); for( i= 1; i<n; i++){ scanf("%d%d", &aa, &bb); edge[value].v = bb; edge[value].next = pre[aa]; pre[aa] = value++; } for( i= 1; i<= n; i++){ apple[i] = 1; }}void dfs(int pp){//后序遍历树 int i, j, k; tree[pp].lef = v; for( i= pre[pp]; i!= -1; i= edge[i].next){ dfs(edge[i].v); } root[pp] = v; tree[pp].rig = v++;}void maketree(int num, int lef, int rig){ seg[num].lef = lef; seg[num].rig = rig; seg[num].mid = (lef + rig) >> 1; seg[num].ans = rig - lef + 1; if( lef != rig){ maketree(num*2, lef, seg[num].mid); maketree(num*2+1, seg[num].mid+1, rig); }}int cal( int num, int lef, int rig){ if( seg[num].lef == lef && seg[num].rig == rig){ return seg[num].ans; } if( rig <= seg[num].mid){ return cal( num*2, lef, rig); } else if( lef > seg[num].mid){ return cal( num*2+1 , lef, rig); } else return cal( num*2, lef , seg[num].mid) + cal(num*2+1, seg[num].mid+1, rig);}void insert( int num, int lef, int val){ seg[num].ans += val; if( seg[num].lef == seg[num].rig) return; if( lef <= seg[num].mid){ insert( num*2, lef, val); } else if( lef > seg[num].mid){ insert( num*2+1, lef, val); }}int main(){//freopen("1.txt", "r", stdin); int i, j, k, aa, bb; char tmp[10]; while( scanf("%d", &n) != EOF){ init(); v = 1; dfs(1); maketree(1, 1, n); scanf("%d", &q); for( i= 0; i<q; i++){ scanf("%s%d", tmp, &aa); if( tmp[0] == 'Q'){ printf("%d\n", cal(1, tree[aa].lef, tree[aa].rig)); } else{ if( apple[root[aa]] == 1) bb = -1; //注意,读入为原始编号,但线段树中为遍历后编号 else bb = 1; insert( 1, root[aa], bb); apple[root[aa]] = 1 - apple[root[aa]]; } } }return 0;}
- 线段树 POJ 3321 Apple Tree
- poj 3321 Apple Tree 线段树
- POJ 3321 Apple Tree 线段树
- POJ 3321 Apple Tree 线段树+DFS
- poj 3321 Apple Tree(线段树)
- poj 3321 Apple Tree 线段树
- POJ 3321 Apple Tree 线段树
- poj 3321 Apple Tree 线段树
- [poj 3321]Apple Tree[模拟DFS][时间戳][线段树]
- poj - 3321 - Apple Tree(dfs+线段树)
- POJ 3321 Apple Tree (dfs+线段树)
- POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)
- POJ 题目3321 Apple Tree(线段树)
- poj 3321 Apple Tree(dfs序+线段树)
- POJ 3321 Apple Tree(dfs序+线段树)
- POJ 3321 Apple Tree dfs序+线段树
- poj 3321 Apple Tree(dfs序+线段树)
- POJ-3321-Apple Tree,线段树,树状数组。
- 装饰模式--Decorator
- 图解实用电子技术丛书
- (数据库)手动设置数据约束
- spring2.5的概念
- How to Insert OLE Object (Adobe Acrobat Document) in Word with C#(如何使用C#在Word中插入OLE对象-PDF文件)
- poj 3321 Apple Tree 线段树
- EPROCESS进程断链
- @Autowired默认按照类型进行注入
- 非802.11r的Fast Roaming
- yuyan
- 计算机科学中最重要的32个算法
- C++STL-算法transform
- BACnet协议栈移植分析之二:rs485.mak
- 将一个非负十进制整数转换成二进制数,用非递归算法和递归算法来实现