• Apple Tree (树形结构变为线性结构+树状数组)


    题目链接

    描述

    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?

    输入

    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

    输出

    For every inquiry, output the correspond answer per line.

    样例输入

    3
    1 2
    1 3
    3
    Q 1
    C 2
    Q 1

    样例输出

    3
    2

    题意

    有一颗苹果树,刚开始每个结点上都有一个苹果,现在有一系列操作,Q N 代表着查询N节点下面的子树一共有多少个苹果(包括n节点
    C N代表着 如果N节点有有一个苹果,就把这个苹果拿走,如果没有,就把这个点上放上一个苹果

    分析

    在查询的时候,要求出这个节点下所有子树的苹果总数,相当于求一个区间和,如果给这棵树的每个节点都给合理的编号,那么就可以按
    照树状数组来求区间和了。
    参考

    代码

    #include<iostream>
    #include<vector>
    #include<string.h>
    #include<stdio.h>
    #define MAXV 100005
    using namespace std;
    vector<int>v[MAXV];
    int cnt;
    int low[MAXV];
    int hight[MAXV];
    int vis[MAXV];
    int hasApple[MAXV];
    int e[MAXV];
    int n;
    void dfs(int id)
    {
        low[id]=++cnt;/// id这个点所管辖区间的左端点
        vis[id]=1;
        for(int i=0; i<v[id].size(); i++ )
        {
            int t=v[id][i];
            if(vis[t]==0)
                dfs(t);
        }
        hight[id]=cnt;/// id这个点所管辖区间的右端点
    }
    int lowBit(int x)
    {
        return x&(-x);
    }
    void updata(int id,int val)
    {
        while(id<=n)
        {
            e[id]+=val;
            id=id+lowBit(id);
        }
    }
    int SUM(int id)
    {
        int sum=0;
        while(id>0)
        {
            sum+=e[id];
            id-=lowBit(id);
        }
        return sum;
    }
    int main()
    {
        //freopen("2.txt","r",stdin);
        scanf("%d",&n);
        {
            for(int i=0; i<=n; i++)
                v[i].clear();
            for(int i=0; i<n-1; i++)
            {
                int a,b;
                scanf("%d%d",&a,&b);
                v[a].push_back(b);
                v[b].push_back(a);
            }
            cnt=0;
            dfs(1);
            for(int i=1; i<=n; i++)
            {
                updata(i,1);
                hasApple[i]=1;
            }
            int m;
            scanf("%d",&m);
            while(m--)
            {
                char ch;
                int num;
                scanf(" %c %d",&ch,&num);
                if(ch=='Q')
                {
                    printf("%d
    ",SUM(hight[num])-SUM(low[num]-1));
                }
                else if(ch=='C')
                {
                    if(hasApple[num]==1)
                    {
                        hasApple[num]=0;
                        updata(low[num],-1);
                    }
                    else
                    {
                        hasApple[num]=1;;
                        updata(low[num],1);
                    }
                }
            }
    
        }
        return 0;
    }
    
  • 相关阅读:
    spring cloud-之入门技术选型的抉择
    jvm系列之-gc日志查看
    jvm系列之-参数设置
    Code completion has become quite slow under Delphi7
    Python4Delphi注意事项
    SHFileOperation删除文件夹
    开漏输出,推挽输出
    DxGrexpt中的ExcelFormat (BIFF)
    通过exe名字查询句柄,String与ShortString转换函数分析
    Netstat判断商品是否正在使用
  • 原文地址:https://www.cnblogs.com/dccmmtop/p/6761088.html
Copyright © 2020-2023  润新知