1. 程式人生 > >Apple Tree(樹狀陣列+線段樹)

Apple Tree(樹狀陣列+線段樹)

Apple Tree

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
題目大意:
一棵樹上長了蘋果,每一個樹枝節點上有長蘋果和不長蘋果兩種狀態,兩種操作,一種操作能夠改變樹枝上蘋果的狀態,另一種操作詢問某一樹枝節點一下的所有的蘋果有多少。

樹狀陣列版:

#include<iostream>
using namespace std;
const int maxn=210100;
struct node
{
    int to;
    int next;
}e[maxn];
char s;int x;
int in[maxn],out[maxn],c[maxn],cnt;
int
n,m,tot,head[maxn],vis[maxn]; void edd_edge(int u,int v) { tot++; e[tot].to=v; e[tot].next=head[u]; head[u]=tot; } int lowbit(int x) { return x&(-x); } void dfs(int x) { in[x]=++cnt; for(int i=head[x];i;i=e[i].next) dfs(e[i].to); out[x]=cnt; } void update(int x,int add) { while(x<=n) { c[x]+=add; x+=lowbit(x); } } int sum(int x) { int s=0; while(x>0) { s+=c[x]; x-=lowbit(x); } return s; } int main() { int a,b; cin>>n; for(int i=1;i<n;i++) { cin>>a>>b; edd_edge(a,b); } dfs(1); for(int i=1;i<=n;i++) update(in[i],1),vis[i]=1; cin>>m; for(int i=1;i<=m;i++) { cin>>s>>x; if(s=='C') { if(vis[x])update(in[x],-1); else update(in[x],1); vis[x]=1-vis[x]; } if(s=='Q') cout<<sum(out[x])-sum(in[x]-1)<<endl; } return 0; }

線段樹版:

#include<iostream>
using namespace std;
const int maxn=100010;
struct edge
{
    int to;
    int next;
}e[maxn*2];
struct node
{
    int l,r;
    int lch,rch;
    int sum;
}tree[maxn*4];
char c;
int n,m,tot,a[maxn];
int head[maxn],in[maxn],out[maxn];
void add_edge(int u,int v)
{
    tot++;
    e[tot].to=v;
    e[tot].next=head[u];
    head[u]=tot;
}
void dfs(int u)
{
    in[u]=++tot;
    for(int i=head[u];i;i=e[i].next)
    dfs(e[i].to);
    out[u]=tot;
}
void build_tree(int ll,int rr)
{
    int cur=++tot;
    tree[cur].l=ll;
    tree[cur].r=rr;
    if(ll!=rr-1)
    {
        int mid=(ll+rr)>>1;
        tree[cur].lch=tot+1;
        build_tree(ll,mid);
        tree[cur].rch=tot+1;
        build_tree(mid,rr);
        tree[cur].sum=tree[tree[cur].lch].sum+tree[tree[cur].rch].sum;
    }
    else tree[cur].sum=1;
}
int find(int k,int l,int r)
{
    int ans=0;
    if(tree[k].l>=l&&tree[k].r<=r)
    return tree[k].sum;
    int mid=(tree[k].l+tree[k].r)>>1;
    if(l<mid) ans+=find(tree[k].lch,l,r);
    if(r>mid) ans+=find(tree[k].rch,l,r);
    return ans;
}
void change(int k,int l,int r,int p)
{
    if(tree[k].l==l&&tree[k].r==r)
    {
        tree[k].sum+=p;
        return;
    }
    int mid=(tree[k].l+tree[k].r)>>1;
    if(l<mid) change(tree[k].lch,l,r,p);
    if(r>mid) change(tree[k].rch,l,r,p);
    tree[k].sum=tree[tree[k].lch].sum+tree[tree[k].rch].sum;
}
int main()
{
    int x,y;
    cin>>n;
    for(int i=1;i<=n-1;i++)
    {
        cin>>x>>y;
        add_edge(x,y);
    }tot=0;
    dfs(1);tot=0;
    build_tree(1,n+1);
    for(int i=1;i<=n;i++)
    a[i]=1;
    cin>>m;
    for(int i=1;i<=m;i++)
    {
        cin>>c>>x;
        if(c=='C')
        {
            if(a[x]==1)
            a[x]=0,change(1,in[x],in[x]+1,-1);
            else a[x]=1,change(1,in[x],in[x]+1,1);
        }
        else
        cout<<find(1,in[x],out[x]+1)<<endl;
    }
    return 0;
}