POJ - 3321 Apple Tree dfs序+线段树 简单题

Apple Tree

  POJ - 3321


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 toN 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 forkx, 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

Source
POJ - 3321

My Solution
题意:初始时树上每个节点都有1个苹果,然后对一个节点操作,如果有苹果,就拿走,没苹果,就放上,然后询问以x为根的子树上有多少个苹果。
 
 
dfs序+线段树 简单题
POJ这题好像没有开O2,vector<int> sons[MAXN];一直TLE,换了邻接表就过了。
时间复杂度 O(n)
空间复杂度 O(4*n)
 
 
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
typedef long long LL;
const int MAXN = 1e5 + 8;

struct Edge{
    int v,next;
}edge[2*MAXN];
int head[MAXN];
int tot;
//dfs序
int p1[MAXN], p2[MAXN], ti = 0;
//int dfsnum[MAXN];  //这个按情况是否需要。
void init(){
    memset(head, -1, sizeof head);
    tot = 0; ti = 0;
}

void add_edge(int a,int b){
    edge[tot] = (Edge){b, head[a]};
    head[a] = tot++;
}

void dfs(int u, int fa){
    p1[u] = ++ti;
    int i, v;
    for(i = head[u]; i != -1; i = edge[i].next){
        v = edge[i].v;
        if(v == fa) continue;
        dfs(v, u);
    }
    p2[u] = ti;
}
//线段树
int sum[4*MAXN];
int size;
inline void pushup(int Ind){
    sum[Ind] = sum[Ind<<1] + sum[(Ind<<1) + 1];
}

inline int _Query(int a, int b, int l, int r, int Ind){
    if(a <= l && b >= r) return sum[Ind];
    int mid = (l+r)>>1; int ret = 0;
    if(a <= mid) ret += _Query(a, b, l, mid, Ind<<1);
    if(b > mid) ret += _Query(a, b, mid + 1, r, (Ind<<1) + 1);
    return ret;
}

inline void _Modify(int a, int l, int r, int Ind){
    if(l == r && l == a){
        sum[Ind] ^= 1;
        return;
    }
    int mid = (l+r)>>1;
    if(a <= mid) _Modify(a, l, mid, Ind<<1);
    else _Modify(a, mid + 1, r, (Ind<<1) + 1);
    pushup(Ind);
}

inline void _build(int l, int r, int Ind){
    if(l == r){
        sum[Ind] = 1;
        return;
    }
    int mid = (l+r)>>1;
    _build(l, mid, Ind<<1);
    _build(mid + 1, r, (Ind<<1) + 1);
    pushup(Ind);
}

inline int Query(int a, int b) {return _Query(a, b, 1, size, 1);}
inline void Modify(int a){return _Modify(a, 1, size, 1);}


int main()
{
    #ifdef LOCAL
    freopen("b.txt", "r", stdin);
    //freopen("b.out", "w", stdout);
    int T = 1;
    while(T--){
    #endif // LOCAL
    //ios::sync_with_stdio(false); cin.tie(0);

    int n, m, i, u, v, x, root;
    char ch[6];
    scanf("%d", &n);
    init();
    for(i = 1; i < n; i++){
        scanf("%d%d", &u, &v);
        add_edge(u, v);
        add_edge(v, u);
    }
    dfs(1, -1);

    scanf("%d", &m);

    size = ti;
    _build(1, size, 1);
    while(m--){
        scanf("%s%d", ch, &x);
        if(ch[0] == 'C'){
            Modify(p1[x]);
        }
        else{
            printf("%d\n", Query(p1[x], p2[x]));
        }
    }


    #ifdef LOCAL
    cout << endl;
    }
    #endif // LOCAL
    return 0;
}



                                                                                                                                             ------from ProLights

  Thank you!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值