poj3321——Apple Tree(树上树状数组)

本文介绍了一种使用树状数组实现的苹果树查询与更新算法,通过对苹果树的分叉点进行标记,实现对苹果数量的快速查询和更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

一棵苹果树,树上的每个分叉和端点都会长苹果,现有两种操作
C操作是将某个分叉点或端点的苹果摘下或长出来,取决于该操作前这个位置是否有苹果,初始时所有节点都有苹果。
Q操作是询问某个分叉点的子树一共有多少个苹果

通过深搜为每个节点赋上初始和结束两个标号,这个编号就相当于辖区,代表这个节点之下的节点们在这个范围之内,也就是整个子树的范围。假设某个节点的初始和结束标号分别为a,b,则这个范围内拥有的苹果总数就是[1,b]-[1,a-1],至于怎么维护这个区间就要用树状数组了

#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <set>
#include <math.h>
#include <algorithm>
#include <queue>
#include <iomanip>
#include <ctime>
#define INF 0x3f3f3f3f
#define MAXN 500005
#define Mod 1000000007
using namespace std;
struct Node
{
    int to,next;
} edge[MAXN<<2];
int n,c[MAXN],head[MAXN],e,cnt;
int st[MAXN],en[MAXN],vis[MAXN],have[MAXN];
int lowbit(int x)
{
    return x&(-x);
}
int sum(int x)
{
    int ans=0;
    for(; x>0; x-=lowbit(x))
        ans+=c[x];
    return ans;
}
void add(int x,int num)
{
    for(; x<=n; x+=lowbit(x))
        c[x]+=num;
}
void addedge(int u,int v)
{
    edge[e].to=v;
    edge[e].next=head[u];
    head[u]=e++;
}
void dfs(int u)
{
    cnt++;
    st[u]=cnt;
    vis[u]=1;
    for(int i=head[u]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].to;
        if(!vis[v])
            dfs(v);
    }
    en[u]=cnt;
}
int main()
{
    while(~scanf("%d",&n))
    {
        e=0;
        cnt=0;
        memset(c,0,sizeof(c));
        memset(head,-1,sizeof(head));
        memset(vis,0,sizeof(vis));
        int u,v;
        for(int i=1; i<n; ++i)
        {
            scanf("%d%d",&u,&v);
            addedge(u,v);
            addedge(v,u);
        }
        dfs(1);
        for(int i=1; i<=n; ++i)
        {
            add(i,1);
            have[i]=1;
        }
        int m,x;
        scanf("%d",&m);
        while(m--)
        {
            char op;
            cin>>op;
            if(op=='C')
            {
                scanf("%d",&x);
                if(have[x])
                {
                    add(st[x],-1);
                    have[x]=0;
                }
                else
                {
                    add(st[x],1);
                    have[x]=1;
                }
            }
            else
            {
                scanf("%d",&x);
                printf("%d\n",sum(en[x])-sum(st[x]-1));
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值