先做一次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. 39 网络相关函数(七)——live555源码阅读(四)网络

    39 网络相关函数(七)——live555源码阅读(四)网络 39 网络相关函数(七)——live555源码阅读(四)网络 简介 14)readSocket从套接口读取数据 recv/recvfrom ...

  2. Python检验一个文件是否存在,如果不在就自己填写内容。

    import os while True: filename=input('Please enter the filename') if os.path.exists(filename): print ...

  3. OI总结(垃圾排版就忽略了吧)

    学OI一年了,到现在联赛所需要的知识已经基本学完了.现在,有必要回过头来,总结总结自己一年来学到的知识以及得到的经验教训. 基础 语言基础 C++的语言基础啥的就略了吧. 算法复杂度分析 O:复杂度的 ...

  4. 2.2---找链表倒数第K个结点

    答案,注意,一种是递归,另一种是迭代,那么巧妙利用双指针: 迭代: public static LinkedListNode nthToLast(LinkedListNode head, int n) ...

  5. centos 谷歌浏览器安装

    首先,这个是坑 http://www.tecmint.com/install-google-chrome-on-redhat-centos-fedora-linux/ 安装会报错,按照错误找到以下资源 ...

  6. HIFI播放器--磨机吐槽篇

    最近看到淘宝店提供各种随身播放器磨机服务,说的是天花乱坠,给你更换零件, 甚至更改电路,搭载上去,是如何如何的好,整个播放器就上升了几个等次,收费还 不低,至少是好几百,我实在是忍不住吐槽了,你们这些 ...

  7. Python类的特点 (1):构造函数与方法

    Python中,类的特点: #encoding:utf-8 class Parent(object): x=1 #x是Parent类的属性(字段) def __init__(self): print ...

  8. How can I determine the URL that a local Git repository was originally cloned from?

    git remote show origin from: http://stackoverflow.com/questions/4089430/how-can-i-determine-the-url- ...

  9. 五步搞定Android开发环境部署——非常详细的Android开发环境搭建教程

      在windows安装Android的开发环境不简单也说不上算复杂,本文写给第一次想在自己Windows上建立Android开发环境投入Android浪潮的朋友们,为了确保大家能顺利完成开发环境的搭 ...

  10. 常见web错误码 404 500

    404表示文件或资源未找到java WEB常见的错误代码1.1xx-信息提示:这些状态代码表示临时的响应.客户端在收到常规响应之前,应准备接收一个或多个1xx响应.100-继续.101-切换协议.2. ...