先做一次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. @ModelAttribute运用详解

      @ModelAttribute使用详解 1.@ModelAttribute注释方法     例子(1),(2),(3)类似,被@ModelAttribute注释的方法会在此controller每个 ...

  2. spring和hibernate整合时无法自动建表

    在使用spring整合hibernate时候代码如下: <property name="dataSource" ref="dataSource" /> ...

  3. TortoiseSVN使用方法

    1.初始化本地SVN目录,在某个文件夹鼠标右键点击. 初始化后目录 2.将某个目录下代码,添加到SVN目录中.                            3.将添加进去代码提取出来. 在某 ...

  4. django-cms安装

    ubuntu:12.04 (32bit) djangocms 0.5.1 =========================== 首先,跟着这个做: https://github.com/divio/ ...

  5. JavaBean转换为XML的源码

    package com.cmge.utils; import java.util.Iterator; import com.cmge.org.oa.bean.OADepartment; import ...

  6. Valid Number

    Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...

  7. sharepoint定义固定的网站集

    SPSite site = new SPSite(http://192.168.0.3/);            SPWeb web = site.RootWeb;

  8. centos7删除已经安装的docker

    centos下可以使用yum来删除docker. 列出docker包的具体的名字. $ yum list installed | grep docker docker-engine.x86_64 -0 ...

  9. ios 中使用https的知识

    先看文章,这篇文章说的是使用AFNetworing进行https时的事项,十分好!http://blog.cnbang.net/tech/2416/ ios中使用https,主要就是使用NSURLCr ...

  10. cxGrid 速度

    在做AdoHelper实用程序的时候,我用了DevExpress的cxGrid控件.在此之前用的是dbgrid,考虑到不能把所有的数据都拉到本地,我用了动态生成的select top 500的命令.这 ...