標籤:
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
For every inquiry, output the correspond answer per line.
Sample Input
31 21 33Q 1C 2Q 1
Sample Output
32
這道題的關鍵是如何把這題轉換成一個區間操作的問題。
由於每次查詢的是子樹中蘋果的個數,說明查詢的區間是子樹的中節點的所在區間,即子樹中所有節點應在查區間內。即要建立一種映射,使得,子樹中節點的標號在查詢區間內。
於是一種映射便是,對每個結點賦予兩個值lt和rt,lt表示以此點為根節點的子樹中序號最小的,rt表示當前節點的序號。
這種映射可以通過Dfs產生。然後題目就轉換成了區間操作的問題。
不過對於存初始圖的問題,我一開始使用了STL裡的vector和list都逾時了。最後用了鏈式前向星。
代碼:
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <algorithm>#include <set>#include <map>#include <vector>#include <list>#include <queue>#include <string>#define inf 0xfffffff#define eps 1e-10#define N 1000000007using namespace std;//線段樹//區間每點增值,求區間和const int maxn = 100005;struct node{ int lt, rt; int val;}tree[4*maxn];//向上更新void PushUp(int id){ tree[id].val = tree[id<<1].val + tree[id<<1|1].val;}//建立線段樹void Build(int lt, int rt, int id){ tree[id].lt = lt; tree[id].rt = rt; tree[id].val = 0;//每段的初值,根據題目要求 if (lt == rt) { tree[id].val = 1; return; } int mid = (lt + rt) >> 1; Build(lt, mid, id<<1); Build(mid + 1, rt, id<<1|1); PushUp(id);}//增加區間內每個點固定的值void Add(int lt, int rt, int id, int pls){ if (lt <= tree[id].lt && rt >= tree[id].rt) { if (tree[id].val) pls = -pls; tree[id].val += pls * (tree[id].rt-tree[id].lt+1); return; } int mid = (tree[id].lt + tree[id].rt) >> 1; if (lt <= mid) Add(lt, rt, id<<1, pls); if (rt > mid) Add(lt, rt, id<<1|1, pls); PushUp(id);}//查詢某段區間內的和int Query(int lt, int rt, int id){ if (lt <= tree[id].lt && rt >= tree[id].rt) return tree[id].val; int mid = (tree[id].lt + tree[id].rt) >> 1; int ans = 0; if (lt <= mid) ans += Query(lt, rt, id<<1); if (rt > mid) ans += Query(lt, rt, id<<1|1); return ans;}//鏈式前向星struct Edge{ int to, next;}edge[200005];int head[100005], cnt;void AddEdge(int u, int v){ edge[cnt].to = v; edge[cnt].next = head[u]; head[u] = cnt; cnt++;}void InitEdge(){ memset(head, -1, sizeof(head)); cnt = 0;}struct{ int lt, rt;}id[100005];int n, m, now;void Dfs(int k){ if (id[k].lt != 0) return; id[k].lt = now; for (int i = head[k]; i != -1; i = edge[i].next) { Dfs(edge[i].to); } id[k].rt = now; now++;}void Init(){ memset(id, 0, sizeof(id)); int u, v; InitEdge(); for (int i = 1; i < n; ++i) { scanf("%d%d", &u, &v); AddEdge(u, v); AddEdge(v, u); } now = 1; Dfs(1); Build(1, n, 1);}void Work(){ char op[3]; int v; for (int i = 0; i < m; ++i) { scanf("%s%d", op, &v); if (op[0] == ‘C‘) { Add(id[v].rt, id[v].rt, 1, 1); } else { printf("%d\n", Query(id[v].lt, id[v].rt, 1)); } }}int main(){ //freopen("test.in", "r", stdin); while (scanf("%d", &n) != EOF && n) { Init(); scanf("%d", &m); Work(); } return 0;}
ACM學習曆程——POJ3321 Apple Tree(搜尋,線段樹)