ACM学习历程——POJ3321 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

这道题的关键是如何把这题转换成一个区间操作的问题。

由于每次查询的是子树中苹果的个数,说明查询的区间是子树的中节点的所在区间,即子树中所有节点应在查区间内。即要创建一种映射,使得,子树中节点的标号在查询区间内。

于是一种映射便是,对每个结点赋予两个值lt和rt,lt表示以此点为根节点的子树中序号最小的,rt表示当前节点的序号。

这种映射可以通过Dfs生成。然后题目就转换成了区间操作的问题。

不过对于存初始图的问题,我一开始使用了STL里的vector和list都超时了。最后用了链式前向星。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <queue>
#include <string>
#define inf 0xfffffff
#define eps 1e-10
#define N 1000000007

using namespace std;

//线段树
//区间每点增值,求区间和
const int maxn = 100005;
struct node
{
    int lt, rt;
    int val;
}tree[4*maxn];

//向上更新
void PushUp(int id)
{
    tree[id].val = tree[id<<1].val + tree[id<<1|1].val;
}

//建立线段树
void Build(int lt, int rt, int id)
{
    tree[id].lt = lt;
    tree[id].rt = rt;
    tree[id].val = 0;//每段的初值,根据题目要求
    if (lt == rt)
    {
        tree[id].val = 1;
        return;
    }
    int mid = (lt + rt) >> 1;
    Build(lt, mid, id<<1);
    Build(mid + 1, rt, id<<1|1);
    PushUp(id);
}

//增加区间内每个点固定的值
void Add(int lt, int rt, int id, int pls)
{
    if (lt <= tree[id].lt && rt >= tree[id].rt)
    {
        if (tree[id].val)
            pls = -pls;
        tree[id].val += pls * (tree[id].rt-tree[id].lt+1);
        return;
    }
    int mid = (tree[id].lt + tree[id].rt) >> 1;
    if (lt <= mid)
        Add(lt, rt, id<<1, pls);
    if (rt > mid)
        Add(lt, rt, id<<1|1, pls);
    PushUp(id);
}

//查询某段区间内的和
int Query(int lt, int rt, int id)
{
    if (lt <= tree[id].lt && rt >= tree[id].rt)
        return tree[id].val;
    int mid = (tree[id].lt + tree[id].rt) >> 1;
    int ans = 0;
    if (lt <= mid)
        ans += Query(lt, rt, id<<1);
    if (rt > mid)
        ans += Query(lt, rt, id<<1|1);
    return ans;
}

//链式前向星
struct Edge
{
    int to, next;
}edge[200005];

int head[100005], cnt;

void AddEdge(int u, int v)
{
    edge[cnt].to = v;
    edge[cnt].next = head[u];
    head[u] = cnt;
    cnt++;
}

void InitEdge()
{
    memset(head, -1, sizeof(head));
    cnt = 0;
}

struct
{
    int lt, rt;
}id[100005];

int n, m, now;

void Dfs(int k)
{
    if (id[k].lt != 0)
        return;
    id[k].lt = now;
    for (int i = head[k]; i != -1; i = edge[i].next)
    {
        Dfs(edge[i].to);
    }
    id[k].rt = now;
    now++;
}

void Init()
{
    memset(id, 0, sizeof(id));
    int u, v;
    InitEdge();
    for (int i = 1; i < n; ++i)
    {
        scanf("%d%d", &u, &v);
        AddEdge(u, v);
        AddEdge(v, u);
    }
    now = 1;
    Dfs(1);
    Build(1, n, 1);
}

void Work()
{
    char op[3];
    int v;
    for (int i = 0; i < m; ++i)
    {
        scanf("%s%d", op, &v);
        if (op[0] == ‘C‘)
        {
            Add(id[v].rt, id[v].rt, 1, 1);
        }
        else
        {
            printf("%d\n", Query(id[v].lt, id[v].rt, 1));
        }
    }
}

int main()
{
    //freopen("test.in", "r", stdin);
    while (scanf("%d", &n) != EOF && n)
    {
        Init();
        scanf("%d", &m);
        Work();
    }
    return 0;
}
时间: 2024-12-17 07:51:50

ACM学习历程——POJ3321 Apple Tree(搜索,线段树)的相关文章

ACM学习历程——HDU3333 Turing Tree(线段树 &amp;&amp; 离线操作)

Problem Description After inventing Turing Tree, 3xian always felt boring when solving problems about intervals, because Turing Tree could easily have the solution. As well, wily 3xian made lots of new problems about intervals. So, today, this sick t

ACM学习历程—POJ1151 Atlantis(扫描线 &amp;&amp; 线段树)

Description There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe different regions of Atlantis. Your friend

POJ 题目3321 Apple Tree(线段树)

Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21566   Accepted: 6548 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

[poj3321]Apple Tree(dfs序+树状数组)

Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26762   Accepted: 7947 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

ACM学习历程——POJ3295 Tautology(搜索,二叉树)

Description WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some subset of the possible symbols K, A, N, C, E, p, q, r, s, t. A Well-formed formula (WFF) is any string of these symbols obeying the following rules: p

ACM学习历程—HDU 4287 Intelligent IME(字典树 || map)

Description We all use cell phone today. And we must be familiar with the intelligent English input method on the cell phone. To be specific, the number buttons may correspond to some English letters respectively, as shown below: 2 : a, b, c    3 : d

ACM学习历程—HDU2222 Keywords Search(字典树)

Keywords Search Description In the modern time, Search engine came into the life of everybody like Google, Baidu, etc.       Wiskey also wants to bring this feature to his image retrieval system.       Every image have a long description, when users

ACM学习历程—SNNUOJ 1239 Counting Star Time(树状数组 &amp;&amp; 动态规划 &amp;&amp; 数论)

http://219.244.176.199/JudgeOnline/problem.php?id=1239 这是这次陕西省赛的G题,题目大意是一个n*n的点阵,点坐标从(1, 1)到(n, n),每个点都有权值,然后从(x, y)引x轴的垂线,然后构成一个三角形,三个顶点分别是(0, 0),(x, 0)和(x, y).求三角形内点的权值和,包括边界,n的范围是1000,m的范围是100000,说起来也比较坑..学弟n*m的复杂度竟然水过去了,目测比赛数据比较水..不过我挂到我们OJ上给了一组随

ACM学习历程—HDU 5023 A Corrupt Mayor&#39;s Performance Art(广州赛区网赛)(线段树)

Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this seemed a safe way for mayor X to make money. Becaus