先做一次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. 27 GroupSock概述(一)——live555源码阅读(四)网络

    27 GroupSock概述(一)——live555源码阅读(四)网络 27 GroupSock概述(一)——live555源码阅读(四)网络 简介 1.网络通用数据类型定义 2.Tunnel隧道封装 ...

  3. bootstrap常用的定义风格

    primary()     首选项 success()     成功 info()        一般信息 warning()       警告 danger()         危险 <bod ...

  4. 一个简单的log

    #pragma once #include <windows.h> #include <process.h> class CLogger { public: static CR ...

  5. 转:js清空数组

    方式1,splice 1 2 3 var ary = [1,2,3,4]; ary.splice(0,ary.length); console.log(ary); // 输出 Array[0],空数组 ...

  6. PHP常用字符串操作函数实例总结(trim、nl2br、addcslashes、uudecode、md5等)

    /*常用的字符串输出函数 * * echo() 输出字符串 * print() 输出一个或多个字符串 * die() 输出一条信息,并退出当前脚本 * printf() 输出格式化字符串 * spri ...

  7. JustMock Lite (Free Mocking Framework For .net)

    通过 Nuget 安装 2.   官网下载(官网不行点这里) 3.   帮助文档 商业版和免费版区别概览 MockingContainer 测试类准备:一般来说也是业务类 public class C ...

  8. php bom \ufeff

    2015年5月29日 16:50:56 星期五 五月的最后一个周五............. 前两天遇到一个问题 PHP 返回json数据, 其他人死活解析不出来 json_last_error(); ...

  9. Delphi TcxCurrencyEditt控件说明

    金额类控件说明: AlignWithMargins:是否显示边框.由Margins 属性来设置边框的值 Anchors:控件停靠,来处理窗口最大化或是调动里的位置 AutoSize:是否自动变化大小 ...

  10. [Android]如何获取当前用户设置的时区

    方法:TimeZone.getDefault().getDisplayName(true, TimeZone.SHORT);获取的值如GMT+08:00,GMT-04:00,EDT  另附:国家码查询 ...