首页 > 其他分享 >POJ 3321 Apple Tree(树状数组)

POJ 3321 Apple Tree(树状数组)

时间:2023-05-04 14:32:48浏览次数:62  
标签:fork kaka Apple int tree Tree 3321 include apple


Apple Tree


Time Limit: 2000MS

 

Memory Limit: 65536K

Total Submissions: 21635

 

Accepted: 6573


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?



POJ   3321   Apple Tree(树状数组)_#include


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
"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
"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


Source


POJ Monthly--2007.08.05, Huang, Jinsong




给定一棵树,某些节点上有苹果,多次询问各子树上的节点数,并且在




询问的中途随时可能新增和删除苹果。




     思路:这个可以使用树状数组来做,不过和一般的树状数组不一样,因为这是




一棵树上 进行分叉,所以区间不是按照1-n进行排列的,所以要找到一种方法通过两个点




之间的关系把1-n进行重新排列,存在begin和end中,这样就可以用树状数组了。


    


     不可以用vector做,我用了直接超时,改成边表就AC了......Orz




 


点击打开链接




#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stack>
#include<queue>
#include<vector>

#define N 100005

using namespace std;

int n;
int cnt;
int m;
int head[N],c[N];
int begin[N],end[N];
bool v[N],du[N];

struct node{
    int xx;
    int next;
}q[N<<1];

void add(int x,int y){
    q[cnt].xx = y;
    q[cnt].next = head[x];
    head[x] = cnt++;
}

int lowbit(int x){
    return x&(-x);
}

int getsum(int x){
    int s = 0;
    while(x>0){
        s += c[x];
        x -= lowbit(x);
    }
    return s;
}

void updata(int x,int t){
    while(x<=n){
        c[x] += t;
        x += lowbit(x);
    }
}

void DFS(int x){
    cnt++;
    begin[x] = cnt;
    v[x] = 1;
    for(int i=head[x];i!=-1;i=q[i].next){
        if(v[q[i].xx] == 0){
            DFS(q[i].xx);
        }
    }
    end[x] = cnt;
}

int main(){
    while(scanf("%d",&n)!=EOF){
        memset(head,-1,sizeof(head));
        memset(v,0,sizeof(v));
        memset(du,1,sizeof(du));
        cnt = 0;
        int x,y;
        for(int i=1;i<n;i++){
            scanf("%d%d",&x,&y);
            add(x,y);
            add(y,x);
        }
        cnt = 0;
        DFS(1);
        for(int i=1;i<=n;i++){
            updata(i,1);
        }
        scanf("%d",&m);
        char str[10];
        while(m--){
            scanf("%s%d",str,&x);
            if(str[0] == 'C'){
                if(du[x]){
                    updata(begin[x],-1);
                    du[x] = 0;
                }else{
                    updata(begin[x],1);
                    du[x] = 1;
                }
            }else{
                printf("%d\n",getsum(end[x]) - getsum(begin[x]-1));
            }
        }
    }
    return 0;
}







标签:fork,kaka,Apple,int,tree,Tree,3321,include,apple
From: https://blog.51cto.com/u_14834528/6242759

相关文章

  • Odoo14 Tree视图创建按钮后面增加按钮
    1.继承ListView.buttons,在其按钮后面增加我们自定义的按钮,通过widget的一些属性去判断按钮的显示<templatesid="list_import_shipping_button_create"xml:space="preserve"><tt-extend="ListView.buttons"><tt-jquery="div.o_list_buttons&......
  • 1159 Structure of a Binary Tree + 根据前序和中序构建二叉树+ 层序遍历模板复习
    题目链接:https://pintia.cn/problem-sets/994805342720868352/exam/problems/1478635126488367104唉,今天的bug出在了下面这条语句。if(tree[root_key].left*tree[root_key].right<0)full_tree=false;我写成了full_tree=!(tree[root_key].left*tree[root_key].rig......
  • AtCoder Regular Contest 125 F Tree Degree Subset Sum
    洛谷传送门AtCoder传送门首先将度数\(-1\)。设\(f_i\)为体积为\(i\)至多能用几个物品凑出来,\(g_i\)为至少。我们现在要证明一个东西:\(x\in[g_i,f_i]\),\((i,x)\)合法。首先若\((s,x)\)合法,那么必须满足\(s-x\in[-z,z-2]\),其中\(z=\sum\limits_{i=1}......
  • Codeforces 280C Game on Tree
    设\(p_i\)为\(i\)涂色或不涂色,\(1\)为涂,\(0\)为不涂,答案即为\(E[\sum_{i=1}^np_i]\)然后转化一下柿子:\(\sum_{i=1}^nE[p_i]\),这就很好求了,单独求每个点\(E[p_i]\)的值就行了考虑对于\(u\)点,\(p_u=1\),即能被涂需要满足其祖先都比其晚涂色假设现在有一个序列里面......
  • CF911F Tree Destruction
    题意:给你一棵\(n\)个结点组成的树,你需要对树进行\(n-1\)次操作,一次操作包含如下的步骤:选择两个叶子结点将这两个结点之间简单路径的长度加到答案中从树上删去两个叶子结点之一初始答案为\(0\),显然在\(n-1\)次操作之后树上只剩下一个结点。计算最大的答案,并构造一组......
  • odoo tree下直接编辑, 免跳转form
      <recordid="mypartner_tree_view"model="ir.ui.view"><fieldname="name">Mypartner清单</field><fieldname="model">mypartner</field><fieldname="arch&......
  • [ABC148F] Playing Tag on Tree
    2023-03-04题目题目传送门翻译翻译难度&重要性(1~10):5题目来源AtCoder题目算法最短路解题思路考虑到T想活得久,A想尽早追上T,所以我们就将问题转化为在树上找一条最长链,使得T能比A先到达这条链。所以我们就可以在树上跑两遍单源最短路,因为边权为\(1\),所以......
  • CF 1709E XOR Tree(树上启发式合并)
    题目链接:https://codeforces.com/contest/1709/problem/E解题思路:定义sum(x,y)为x→y路径上的点的异或和,dx 为x→root路径上的点的异或和。对于一个点权树,sum(x,y)=dx ^dy ^vallca(x,y)。考虑修改一个点,可以将它改为一个很大的2为底数的幂,则经过此点的所有的不合......
  • AtCoder Regular Contest 117 D Miracle Tree
    洛谷传送门AtCoder传送门第一步就没想到可以考虑化简限制。设所有点按\(E_i\)从小到大排序后顺序是\(p_1,p_2,...,p_n\)。发现只需满足\(E_{p_{i+1}}-E_{p_i}\ge\operatorname{dis}(p_i,p_{i+1})\)。证明是对于任意\(i<j<k\),若\(p_i,p_j\)和\(p_j,p_k\)均满......
  • Codeforces 1799H - Tree Cutting(树形 dp)
    思考的时候一直卡在不会在低于\(O(n)\)的时间内储存一个连通块的\(siz\)有关的信息,看了洛谷题解之后才发现我真是个小丑。树形DP。对于一条我们需要操作的边\((i,fa_i)\),我们将其分为保留子树和删除子树两种类型,对于删除子树,我们在判定其是否合法时候改为判定删除的连通块......