poj 3321 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?
qwq

Input

The first line contains an integer N(N100000) , which is the number of the forks in the tree.
The following N1 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(M100000) .
The following M lines each contain a message which is either
Cx” 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
Qx” 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,一开始点权全部为1的树,支持两种操作

  • Cx :如果 x 节点权值为1,就变成0,否则变为1

  • Qx:查询以 x <script type="math/tex" id="MathJax-Element-411">x</script> 为根的子树的点权和

solution

  • 很裸的dfs序+很裸的线段树…感觉没什么好说的啊…

code

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;

template<typename T>
void input(T &x) {
    x=0; T a=1;
    register char c=getchar();
    for(;c<'0'||c>'9';c=getchar())
        if(c=='-') a=-1;
    for(;c>='0'&&c<='9';c=getchar())
        x=x*10+c-'0';
    x*=a;
    return;
}

#define MAXN 100010

struct  Edge {
    int u,v,next;
    Edge(int u=0,int v=0,int next=0):
        u(u),v(v),next(next) {}
};

Edge edge[MAXN];
int head[MAXN],cnt;

void addedge(int u,int v) {
    edge[++cnt]=Edge(u,v,head[u]);
    head[u]=cnt;
    return;
}

int dfn[MAXN],last[MAXN],timee;

void dfs(int now) {
    dfn[now]=++timee;
    for(int i=head[now];i;i=edge[i].next)
        if(!dfn[edge[i].v]) dfs(edge[i].v);
    last[now]=timee;
    return;
}

struct Segment_Tree {
    int l,r,sum;
};

Segment_Tree t[MAXN<<2];

void updata(int now) {
    t[now].sum=t[now<<1].sum+t[now<<1|1].sum;
    return;
}

void build(int now,int l,int r) {
    t[now].l=l,t[now].r=r;
    if(l==r) {
        t[now].sum=1;
        return;
    }
    int mid=l+r>>1;
    build(now<<1,l,mid);
    build(now<<1|1,mid+1,r);
    updata(now);
    return;
}

void Modify(int now,int pos) {
    int l=t[now].l,r=t[now].r;
    if(l==r) {
        t[now].sum^=1;
        return;
    }
    int mid=l+r>>1;
    if(pos<=mid) Modify(now<<1,pos);
    else Modify(now<<1|1,pos);
    updata(now);
    return;
}

int query(int now,int L,int R) {
    int l=t[now].l,r=t[now].r;
    if(l==L&&R==r) return t[now].sum;
    int mid=l+r>>1;
    if(R<=mid) return query(now<<1,L,R);
    else if(L>mid) return query(now<<1|1,L,R);
    else {
        int a=query(now<<1,L,mid),
            b=query(now<<1|1,mid+1,R);
        return a+b;
    }
}

int main() {
    int n,m;
    input(n);
    for(int i=1;i<n;i++) {
        int u,v;
        input(u),input(v);
        addedge(u,v);
    }
    dfs(1);
    build(1,dfn[1],last[1]);
    input(m);
    char op[5];
    int x;
    while(m--) {
        scanf("%s",op);
        input(x);
        if(op[0]=='C') Modify(1,dfn[x]);
        else printf("%d\n",query(1,dfn[x],last[x]));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值