Apple Tree POJ - 3321 (线段树+DFS序)

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
"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
"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

题目大意:对树有两种操作,第一种是C X, 如果树节点X上有苹果就删除,没有就新生成。 第二种是Q X, 查询结点x以上有多少苹果。

解题思路:对每个节点进行DFS序编码,然后通过这个编码去确定线段树的区间,基本上就是个线段树裸题。

题目链接:http://poj.org/problem?id=3321

/*
@Author: Top_Spirit
@Language: C++
*/
// #include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std ;
typedef unsigned long long ull ;
typedef long long ll ;
typedef pair < ll, ll > P ;
const int Maxn = 100010 << 2 ;
const int Mod = 1e9 + 7  ;
const int INF = 0x3f3f3f3f ;

#define lson ri << 1, l, Mid
#define rson ri << 1 | 1, Mid + 1, r

int head[Maxn], to[Maxn], Next[Maxn], cnt ;
int d[Maxn], size[Maxn], dis ;
int c[Maxn], n, m ;

void init (){
    cnt = 0 ;
    dis = 0 ;
    memset(head, 0, sizeof(head)) ;
}

void add (int u, int v){
    to[++cnt] = v ;
    Next[cnt] = head[u] ;
    head[u] = cnt ;
}

void Dfs (int x, int pre){
    size[x] = 1 ;
    d[x] = ++dis ;
    for (int i = head[x]; i > 0; i = Next[i]){
        if (to[i] != pre) {
            Dfs(to[i], x) ;
            size[x] += size[to[i]] ;
        }
    }
}

void pushUp (int ri){
    c[ri] = c[ri << 1] + c[ri << 1 | 1] ;
}

void Build (int ri, int l, int r){
    if (l == r) {
        c[ri] =  1;
        return ;
    }
    int Mid = (l + r) >> 1 ;
    Build (lson) ;
    Build (rson) ;
    pushUp(ri) ;
}

int query (int L, int R, int ri, int l, int r){
    if (l >= L && r <= R) return c[ri]  ;
    int Mid = (l + r) >> 1 ;
    int ans = 0 ;
    if (L <= Mid) ans += query(L, R, lson) ;
    if (R > Mid) ans += query(L, R, rson) ;
    return ans ;
}

void Updata (int ri, int l, int r, int x){
    if (l == r) {
        c[ri] ^= 1 ;
        return ;
    }
    int Mid = (l + r) >> 1 ;
    if (x <= Mid) Updata (lson, x) ;
    else  Updata(rson, x) ;
    pushUp(ri) ;
}

int main (){
    // ios_base::sync_with_stdio(false) ;
    // cin.tie(0) ;
    // cout.tie(0) ;
    while (~scanf("%d", &n)){
        init() ;
        for (int i = 1; i < n; i++){
            int x, y ;
            // cin >> x >> y ;
            scanf("%d%d", &x, &y );
            add(x, y) ;
            add(y, x) ;
        }
        Dfs (1, 0) ;
        Build (1, 1, n) ;
        // cin >> m ;
        scanf("%d", &m);
        getchar() ;
        for (int i = 1; i <= m; i++){
            char ch = getchar() ;
            int x ;
            // cin >> x ;
            scanf("%d", &x);
            if (ch == 'Q') printf("%d\n", query (d[x], d[x] + size[x] - 1, 1, 1, n)) ;
            else Updata (1, 1, n, d[x]) ;
            getchar() ;
        }
    }
    return 0 ;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值