POJ - 3321
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 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 For every inquiry, output the correspond answer per line. Sample Input 3 1 2 1 3 3 Q 1 C 2 Q 1 Sample Output 3 2 /* Author: 2486 Memory: 10004 KB Time: 766 MS Language: G++ Result: Accepted */ #include <cstdio> #include <algorithm> #include <cstring> #include <vector> #include <queue> using namespace std; #define lson rt << 1, l, mid #define rson rt << 1|1, mid + 1, r #define root 1, 1, N const int MAXN = 2e5 + 5; int sum[MAXN << 2], LU[MAXN], RU[MAXN], N, M, A, B, tot, K; char op[10]; /***********加边模板***************/ int Head[MAXN], Next[MAXN], rear; struct edge { int u,v; } es[MAXN]; void Edge_Init() { rear = 0; memset(Head, -1, sizeof(Head)); } void Edge_Add(int u,int v) { es[rear].u = u; es[rear].v = v; Next[rear] = Head[u]; Head[u] = rear ++; } void DFS(int to, int from) { LU[to] = ++ tot;//用来标记属于它的子树的序列 for(int i = Head[to] ; ~ i; i = Next[i]) { int v = es[i].v; if(v == from) continue; DFS(v, to); } RU[to] = tot; } /**********************************/ void pushup(int rt) { sum[rt] = sum[rt << 1] + sum[rt << 1|1]; } void build(int rt, int l, int r) { if(l == r) { sum[rt] = 1; return ; } int mid = (l + r) >> 1; build(lson); build(rson); pushup(rt); } void update(int p,int rt,int l, int r) { if(l == r) { sum[rt] ^= 1; return; } int mid = (l + r) >> 1; if(p <= mid) update(p, lson); else update(p, rson); pushup(rt); } int query(int L, int R,int rt, int l, int r) { if(L <= l && r <= R) { return sum[rt]; } int mid = (l + r) >> 1; int res = 0; if(L <= mid) res += query(L, R, lson); if(R > mid) res += query(L, R, rson); return res; } int main() { //freopen("D://imput.txt","r",stdin); while(~ scanf("%d", &N)) { tot = 0; build(root); Edge_Init(); for(int i = 1; i < N ; i ++) { scanf("%d%d", &A, &B); Edge_Add(A, B); Edge_Add(B, A); } DFS(1, -1); scanf("%d", &M); while(M --) { scanf("%s %d", op, &K); if(op[0] == ‘C‘) { update(LU[K],root); } else { printf("%d\n", query(LU[K],RU[K],root)); } } } return 0; } |
POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)
时间: 2024-10-10 07:48:57
POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)的相关文章
poj 3321:Apple Tree(树状数组,提高题)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18623 Accepted: 5629 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
POJ 3321 Apple Tree 【树状数组+建树】
题目链接:http://poj.org/problem?id=3321 Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 34812 Accepted: 10469 Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka like
(简单) POJ 3321 Apple Tree,树链剖分+树状数组。
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.
POJ 3321—— Apple Tree(树状数组)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19160 Accepted: 5831 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
poj 3321 Apple Tree(树状数组)
辉煌北大的月赛题质量真高啊,这种树状数组真难想到. 树状数组的基本用法是区间,单点的应用,起初这个怎么都想不到如何套用到树状数组. 转化方法是 将树上的节点信息查询,转为深度优先中节点顺序(代表结点编号).进结点与出结点分别代表该结点管辖范围. 题目大意级是说,给你一颗树,最初每个节点上都有一个苹果,有两种操作:修改(即修改某一个节点,修改时这一个节点苹果从有到无,或从无到有)和查询(查询某一个节点他的子树上有多少个苹果). 由于此题数据比较大(N<=10^5),而且不是标准的二叉树,所以这里我
POJ 3321 Apple Tree (dfs+线段树)
题目大意: 修改树上的节点,然后求子树的和. 思路分析: dfs 重新编号,烂大街了... #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #define maxn 100005 #define lson num<<1,s,mid #define rson num<<1|1,mid+1,e using namespace std
【树状数组】POJ 3321 Apple Tree
/** * @author johnsondu * @time 2015.8.25 20:04 * @problem POJ 3321 Apple Tree * @type Binary Index Tree * @description 从根节点开始,dfs遍历树,先访问的节点 * 记为beg, 从当前结点遍历访问到的最后的 * 一个节点,记为end.然后按照树状数组的 * 方法进行求解. * @url http://poj.org/problem?id=3321 */ #include <i
POJ 3321 DFS序+线段树
单点修改树中某个节点,查询子树的性质.DFS序 子树序列一定在父节点的DFS序列之内,所以可以用线段树维护. 1: /* 2: DFS序 +线段树 3: */ 4: 5: #include <cstdio> 6: #include <cstring> 7: #include <cctype> 8: #include <algorithm> 9: #include <vector> 10: #include <iostream> 1
POJ 3321 Apple Tree 【树形结构转变为线性结构+线段树OR树状数组】
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 21587 Accepted: 6551 POJ 3321链接: 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