先做一次dfs求得每个节点为根的子树在树状数组中编号的起始值和结束值,再树状数组做区间查询 与单点更新。

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
using namespace std;
typedef long long LL;
const int N = , INF = 0x3F3F3F3F;
#define MS(a, num) memset(a, num, sizeof(a))
int leftid[N], rightid[N];
struct Node{
    int to,next;
}edge[ * N];
int head[N],tot;
void init(){
    memset(head, -, sizeof(head));
    tot = ;
}
inline void addedge(int u, int to){
    edge[tot].to=to;
    edge[tot].next=head[u];
    head[u]=tot++;
}
int n, m;
bool sta[N];
int C[N];
inline int lowbit(int x){
    return x&-x;
}
inline void add(int x, int val){
    for(int i=x;i<=n;i+=lowbit(i)){
        C[i] += val;
    }
}
inline int sum(int x){
    int ret = ;
    for(int i=x;i>;i-=lowbit(i)){
        ret+=C[i];
    }
    return ret;
} int cnt = ; void dfs(int u, int fa){
    leftid[u] = cnt + ;
    for(int i = head[u]; ~i ; i = edge[i].next){
        int v = edge[i].to;
        if(v != fa){
            dfs(v, u);
        }
    }
    rightid[u] = ++cnt;
} int main(){
    while(~scanf("%d", &n) && n){
        MS(C, );
        init();
        for(int i = ; i <= n; i++){
            add(i, );
            sta[i]  =;
        }
        int u, v;
        for(int i = ; i < n -; i++){
            scanf("%d %d", &u, &v);
            addedge(u, v);
            addedge(v, u);
        }
        cnt = ;
        dfs(, -);
        cin>>m;
        char ope;         while(m--){
            scanf(" %c %d", &ope, &u);
            int l  = leftid[u],  r = rightid[u];
            if(ope == 'C'){
                if(sta[r]){
                    sta[r] = ;
                    add(r, -);
                }else{
                    sta[r] = ;
                    add(r, );
                }
            }else{
                int tp = sum(r) - sum(l - );
                printf("%d\n", tp);
            }
        }     }
    return ;
}

POJ3321 Apple Tree(树状数组)的更多相关文章

  1. POJ--3321 Apple Tree(树状数组+dfs(序列))

    Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22613 Accepted: 6875 Descripti ...

  2. POJ 3321 Apple Tree(树状数组)

                                                              Apple Tree Time Limit: 2000MS   Memory Lim ...

  3. POJ 3321:Apple Tree 树状数组

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22131   Accepted: 6715 Descr ...

  4. E - Apple Tree(树状数组+DFS序)

    There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. ...

  5. POJ 3321 Apple Tree 树状数组+DFS

    题意:一棵苹果树有n个结点,编号从1到n,根结点永远是1.该树有n-1条树枝,每条树枝连接两个结点.已知苹果只会结在树的结点处,而且每个结点最多只能结1个苹果.初始时每个结点处都有1个苹果.树的主人接 ...

  6. POJ 3321 Apple Tree (树状数组+dfs序)

    题目链接:http://poj.org/problem?id=3321 给你n个点,n-1条边,1为根节点.给你m条操作,C操作是将x点变反(1变0,0变1),Q操作是询问x节点以及它子树的值之和.初 ...

  7. POJ 3321 Apple Tree 树状数组 第一题

    第一次做树状数组,这个东西还是蛮神奇的,通过一个简单的C数组就可以表示出整个序列的值,并且可以用logN的复杂度进行改值与求和. 这道题目我根本不知道怎么和树状数组扯上的关系,刚开始我想直接按图来遍历 ...

  8. 3321 Apple Tree 树状数组

    LIANJIE:http://poj.org/problem?id=3321 给你一个多叉树,每个叉和叶子节点有一颗苹果.然后给你两个操作,一个是给你C清除某节点上的苹果或者添加(此节点上有苹果则清除 ...

  9. HDU3333 Turing Tree 树状数组+离线处理

    Turing Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

随机推荐

  1. HDU 1024 max sum plus

    A - Max Sum Plus Plus Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  2. sql server 2008笔记

    sql server 2008开启远程访问数据库 1.以windows验证模式进入数据库管理器. 第二步:右击sa,选择属性: 在常规选项卡中,重新填写密码和确认密码(改成个好记的).把强制实施密码策 ...

  3. extjs动态改变样式

    { width:438, height:440, name:'loginDiv', ui:'123', x: '50%' , y: 200, border:true, bodyStyle:{ 'bor ...

  4. JavaScript Coding 模式荟萃

    1.自运行的匿名函数 <script type="text/javascript" src="./js/jquery-1.7.2.js"></ ...

  5. ios 在ios9中 NSNotificationCenter addObserver 不会影响对象释放

    如题,ios9上,  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test) name:@&qu ...

  6. MongoDB 数据库管理(不定时更新)

    之前的几篇文章大致说了副本集的搭建.副本集的管理,现在说下MongoDB数据库的管理.数据库管理包括:备份.还原.导入.导出.服务器管理等. 一:查看服务器状态,查看命令行参数.db.serverSt ...

  7. Java for LeetCode 207 Course Schedule【Medium】

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  8. ABAP 行列稳定刷新语句

    DATA stbl TYPE lvc_s_stbl. stbl-row = 'X'." 基于行的稳定刷新         stbl-col = 'X'." 基于列稳定刷新      ...

  9. Divide and conquer:Aggressive Cows(POJ 2456)

    侵略性的牛 题目大意:C头牛最大化他们的最短距离 常规题,二分法即可 #include <iostream> #include <algorithm> #include < ...

  10. [jquery]判断页面滚动到顶部和底部(适用于手机web加载)

    //判断页面滚动到顶部和底部 $(window).scroll(function(){ var doc_height = $(document).height(); var scroll_top = ...