POJ 3321-Apple Tree [Fenwick tree + DFS Order]

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_41785863/article/details/101861032

Have an apple tree before Kaka house, every autumn, a lot of apples grow on trees. Kaka is like apples. The tree has N nodes, card numbers them 1 to N , the root number is always 1. Each node up to the junction on an apple. Kaka wants to know if a child end up with a total number of apple trees.

The question now is will the new Apple continues to grow, Kaka might take off at any time to eat an apple. Can you help Kaka it?

 

Input

Input Data: The first line contains an integer N ( N  <= 100000), representing the number of tree nodes.
Next N -1 rows, each row comprising two integers u and v , represent u and v are linked.
The next line contains an integer M ( M  ≤ 100,000).
The next M lines contain one of the following two commands:
" the X- " on a node that Apple has changed, if the original is not Apple, is now a grown up Apple; if the original Apple, it is Kaka ate it.
" x " indicates that the query x sub-tree apples on the node number. Node contains the X- .

Output

For each query, output the result.

Ideas: This title is not difficult, is to be determined by the sub-tree node dfs sequence contained, then the tree array to achieve a single point and range queries can be modified, but some small details that need attention. First, get stuck vector, open n-vector time out, only to open a two-dimensional vector will not. After another record dfs sequence, when updating element is updated in accordance with dfs sequence of numbers, which points to note.

#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
#define ls rt << 1
#define rs rt << 1|1
#define mid ((l + r) >> 1)
#define lson l, mid, ls
#define rson mid + 1, r, rs
const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;
vector<vector<int> > e(100010);
int L[maxn], R[maxn], c[maxn], vis[maxn];
int n, tot = 0;

inline int lowbit(int i)
{
    return (i & (-i));
}
void add(int i, int x)
{
    while(i <= n)
    {
        c[i] += x;
        i += lowbit(i);
    }
}
int ask(int i)
{
    int ret = 0;
    while(i > 0)
    {
        ret += c[i];
        i -= lowbit(i);
    }
    return ret;
}
void dfs(int u, int fa)
{
    L[u] = ++tot;
    for(int i = 0; i < e[u].size(); ++i)
    {
        int v = e[u][i];
        if(v == fa) continue;
        dfs(v, u);
    }
    R[u] = tot;
}

int main()
{
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i)
        add(i, 1);
    for(int i = 1; i < n; ++i)
    {
        int u, v;
        scanf("%d%d", &u, &v);
        e[u].push_back(v);
        e[v].push_back(u);
    }
    dfs(1, 0);
    int m;
    scanf("%d", &m);
    while(m--)
    {
        char s[2];
        int x;
        scanf("%s%d", s, &x);
        if(s[0] == 'C')
        {
            int tmp = 1;
            vis[x] ^= 1;
            if(vis[x])
                tmp = -1;
            x = L[x]; //这里要注意一下
            add(x, tmp);
        }
        else if(s[0] == 'Q')
        {
            printf("%d\n", ask(R[x]) - ask(L[x] - 1));
        }
    }
    return 0;
}

 

Guess you like

Origin blog.csdn.net/qq_41785863/article/details/101861032