codevs1228 (dfs序+线段树)

1228 苹果树

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 钻石 Diamond

题目描述 Description

在卡卡的房子外面,有一棵苹果树。每年的春天,树上总会结出很多的苹果。卡卡非常喜欢吃苹果,所以他一直都精心的呵护这棵苹果树。我们知道树是有很多分叉点的,苹果会长在枝条的分叉点上面,且不会有两个苹果结在一起。卡卡很想知道一个分叉点所代表的子树上所结的苹果的数目,以便研究苹果树哪些枝条的结果能力比较强。

卡卡所知道的是,每隔一些时间,某些分叉点上会结出一些苹果,但是卡卡所不知道的是,总会有一些调皮的小孩来树上摘走一些苹果。

于是我们定义两种操作:

C x

表示编号为x的分叉点的状态被改变(原来有苹果的话,就被摘掉,原来没有的话,就结出一个苹果)

G x

查询编号为x的分叉点所代表的子树中有多少个苹果

 

我们假定一开始的时候,树上全都是苹果,也包括作为根结点的分叉1。

输入描述 Input Description

第一行一个数N (n<=100000)

接下来n-1行,每行2个数u,v,表示分叉点u和分叉点v是直接相连的。

再接下来一行一个数M,(M<=100000)表示询问数

接下来M行,表示询问,询问的格式如题目所述Q x或者C x

输出描述 Output Description

对于每个Q x的询问,请输出相应的结果,每行输出一个

样例输入 Sample Input

3

1 2

1 3

3

Q 1

C 2

Q 1

样例输出 Sample Output

3

2

题意:

对于一颗树,有两个操作:

1.改变一个节点的值,若原来为 0 则变为 1,若原来为 1 则变为 0 ;

2.查询一个节点和其子树所有节点值的和。

总结:

第一次遇到dfs序的问题,对于一颗树,记录节点 i 开始搜索的序号 Left[i] 和结束搜索的序号 Righti[i],那么序号在 Left[i] ~ Right[i] 之间的都是节点 i 子树上的节点。

并且此序号与线段树中 L~R 区间对应,在纸上模拟了几遍确实如此,但暂时还未理解为何对应。

此题就是dfs序+线段树的裸题

代码:

#include<iostream>
#include<vector>
#include<cstring>
#include<cstdio>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int maxn = 1e5+5;

int dfs_order=0, Right[maxn], Left[maxn], vis[maxn];
int sumv[maxn<<2], lazy[maxn<<2];
vector<int> G[maxn];

void push_up(int l, int r, int rt)
{
    sumv[rt]=sumv[rt<<1]+sumv[rt<<1|1];
}

void build(int l, int r, int rt)
{
    if(l==r)
    {
        //cout<<l<<endl;
        sumv[rt]=1;
        return;
    }
    int m=(l+r)/2;
    build(lson);
    build(rson);
    push_up(l, r, rt);
}

void update(int l, int r, int rt, int x, int v)
{
    if(l==r)
    {
        sumv[rt]+=v;
        return;
    }
    int m=(l+r)/2;
    if(x<=m) update(lson, x, v);
    if(m<x) update(rson, x, v);
    push_up(l, r, rt);
}

int query(int l, int r, int rt, int ql, int qr)
{
    if(ql<=l && r<=qr)
    {
        return sumv[rt];
    }
    int m=(l+r)/2, res;
    if(qr<=m) res=query(lson, ql, qr);
    else if(m<ql) res=query(rson, ql, qr);
    else res=query(lson, ql, qr)+query(rson, ql, qr);
    //cout<<"query "<<res<<endl;
    return res;
}

void dfs(int x)
{
    dfs_order++;
    Left[x]=dfs_order;
    //cout<<x<<" "<<Left[x]<<endl;
    for(int i=0; i<G[x].size(); ++i)
    {
        if(!vis[G[x][i]]) {vis[G[x][i]]=1;dfs(G[x][i]);}
    }
    Right[x]=dfs_order;
}

int main()
{
    int n, m;
    scanf("%d", &n);
    build(1, n, 1);
    for(int i=1; i<n; ++i)
    {
        int u, v;
        scanf("%d%d", &u, &v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    vis[1]=1;
    dfs(1);
    memset(vis, 0, sizeof vis);
    scanf("%d", &m);
    for(int i=1; i<=m; ++i)
    {
        char op;
        int t;
        cin>>op>>t;
        if(op=='Q')
        {
            //cout<<Left[t]<<" "<<Right[t]<<endl;
            printf("%d
", query(1, n, 1, Left[t], Right[t]));
        }
        else
        {
            if(!vis[t])
                update(1, n, 1, Left[t], -1);
            else
                update(1, n, 1, Left[t], 1);
            vis[t]=!vis[t];
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/xiepingfu/p/7265672.html