zoukankan      html  css  js  c++  java
  • pku-3321 Apple Tree(dfs序+树状数组)

    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

    题意:

    给一棵树,树上结点权值一开始都为1,每次操作将1与0相互转换,求某个结点的子树(包括自己)的权值和

    解题思路:

    将结点转换为dfs序,并用L和R数组维护该结点的子树以转换为线性结构,用树状数组或者线段树维护区间就行了,我写的是树状数组的

    题目链接

    #include <algorithm>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <iostream>
    #include <cstdlib>
    #include <set>
    #include <vector>
    #include <cctype>
    #include <iomanip>
    #include <sstream>
    #include <climits>
    #include <queue>
    #include <stack>
    using namespace std;
    //#pragma comment(linker, "/STACK:1024000000,1024000000")
    #define de(a) cout << #a << " = " << a << endl
    #define rep(i, a, n) for (int i = a; i < n; i++)
    #define per(i, a, n) for (int i = n - 1; i >= a; i--)
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<int, int> PII;
    typedef pair<double, double> PDD;
    typedef vector<int, int> VII;
    #define inf 0x3f3f3f3f
    const ll INF = 0x3f3f3f3f3f3f3f3f;
    const ll MAXN = 1e5 + 7;
    const ll MAXM = 1e5 + 7;
    const ll MOD = 1e9 + 7;
    const double eps = 1e-6;
    const double pi = acos(-1.0);
    struct node
    {
        int from, to;
        int next;
    } E[MAXN << 1];
    int n, q;
    int cnt = -1;
    int head[MAXN];
    void add_edge(int u, int v)
    {
        E[++cnt].from = u;
        E[cnt].to = v;
        E[cnt].next = head[u];
        head[u] = cnt;
    }
    int a[MAXN], c[MAXN];
    int cnt1 = 0;
    int L[MAXN];
    int R[MAXN];
    int lowbit(int x)
    {
        return x & (-x);
    }
    int gesum(int x)
    {
        int ans = 0;
        for (int i = x; i > 0; i -= lowbit(i))
            ans += c[i];
        return ans;
    }
    void add(int x, int y)
    {
        for (int i = x; i <= n; i += lowbit(i))
            c[i] += y;
    }
    void dfs(int now, int fa)
    {
        L[now] = ++cnt1;
        for (int i = head[now]; i != -1; i = E[i].next)
        {
            int to = E[i].to;
            if (to != fa)
                dfs(to, now);
        }
        R[now] = cnt1;
    }
    void init()
    {
        memset(head, -1, sizeof(head));
        cnt = -1;
        cnt1 = 0;
        for (int i = 1; i <= n; i++)
            a[i] = 1, add(i, 1);
    }
    int main()
    {
        scanf("%d", &n);
        init();
        for (int i = 1; i < n; i++)
        {
            int u, v;
            scanf("%d%d", &u, &v);
            add_edge(u, v);
            add_edge(v, u);
        }
        dfs(1, -1);
        scanf("%d", &q);
        for (int i = 0; i < q; i++)
        {
            int x;
            char op[10];
            scanf(" %s%d", op, &x);
            if (op[0] == 'Q')
                printf("%d
    ", gesum(R[x]) - gesum(L[x] - 1));
            else
            {
                if (a[L[x]])
                {
                    a[L[x]] ^= 1;
                    add(L[x], -1);
                }
                else
                {
                    a[L[x]] ^= 1;
                    add(L[x], 1);
                }
            }
        }
        return 0;
    }
    
  • 相关阅读:
    题解 P3842 【[TJOI2007]线段】
    题解 CF1366A 【Shovels and Swords】
    题解 CF1391D
    题解 CF1374B 【Multiply by 2, divide by 6】
    CSP-J2020爆零记
    YbtOJ20025 放置石子
    YbtOJ20001 立方数差
    [仅供参考]W-RB的码风及要求
    [敲黑板]CSP考试策略
    [水沝淼㵘]向量水解
  • 原文地址:https://www.cnblogs.com/graytido/p/11270445.html
Copyright © 2011-2022 走看看