zoukankan      html  css  js  c++  java
  • POJ 3321 Apple Tree(树状数组)

                                                              Apple Tree
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 27470   Accepted: 8140

    Description

    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 to N 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
    "C 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
    "Q x" which means an inquiry for the number of apples in the sub-tree above the fork x, 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
    【分析】题意很简单,这里就不多说了。做法是先dfs编号,找出每个节点所包含的节点编号区间,然后就是树状数组的事了。
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <string>
    #include <map>
    #include <stack>
    #include <queue>
    #include <vector>
    #define inf 2e9
    #define met(a,b) memset(a,b,sizeof a)
    typedef long long ll;
    using namespace std;
    const int N = 2e5+5;
    const int M = 4e5+5;
    int n,m,tot=0,cnt=0;
    int head[N],x[N],y[N],r[N];
    int tree[N];
    struct EDG{
        int to,next;
    }edg[M];
    inline void addedg(int u,int v){
        edg[tot].to=v;edg[tot].next=head[u];head[u]=tot++;
    }
    void add(int k,int num){
        while(k<=n){
            tree[k]+=num;
            //printf("####%lld
    ",tree[k]);
            k+=k&(-k);
        }
    }
    int Sum(int k){
        int sum=0;
        while(k>0){
            sum+=tree[k];
            k-=k&(-k);
        }
        return sum;
    }
    void dfs(int u){
        int v;
        x[u]=++cnt;
        for(int i=head[u];i!=-1;i=edg[i].next){
            v=edg[i].to;
            if(!x[v])dfs(v);
        }
        y[u]=cnt;
        return;
    }
    int main() {
        met(tree,0);met(head,-1);met(x,0);
        int apple[N];
        for(int i=1;i<N;i++)apple[i]=1;
        int u,v;
        char str[5];
        scanf("%d",&n);
        for(int i=1;i<n;i++){
            scanf("%d%d",&u,&v);
            addedg(u,v);addedg(v,u);
        }
        dfs(1);
        for(int i=1;i<=n;i++)add(i,1);
        scanf("%d",&m);
        while(m--){
            scanf("%s",str);
            scanf("%d",&u);
            if(str[0]=='C'){
                int l=x[u],rr=y[u];
                if(apple[l]){
                    apple[l]=0;
                    add(l,-1);
                }else {
                    apple[l]=1;
                    add(l,1);
                }
            }else {
                int ans=Sum(y[u])-Sum(x[u]-1);
                printf("%d
    ",ans);
            }
        }
        return 0;
    }
  • 相关阅读:
    使用 Sublime、WebStorm 开发 Jade
    3-微信小程序开发(小程序的目录结构说明)
    1-微信小程序开发(安装软件和运行第一个微信小程序)
    29-ESP8266 SDK开发基础入门篇--编写TCP 客户端程序(Lwip RAW模式,非RTOS版,精简入门)
    28-ESP8266 SDK开发基础入门篇--编写wifi模块TCP 客户端程序(官方API版,非RTOS版)
    27-ESP8266 SDK开发基础入门篇--编写Android SmartConfig一键配网程序
    26-ESP8266 SDK开发基础入门篇--编写WIFI模块 SmartConfig/Airkiss 一键配网
    25-ESP8266 SDK开发基础入门篇--控制WIFI连接路由器
    17-网页,网站,微信公众号基础入门(使用Adobe Dreamweaver CS6 制作网页/网站)
    15-网页,网站,微信公众号基础入门(网页版MQTT,做自己的MQTT调试助手)
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/6090918.html
Copyright © 2011-2022 走看看