1 条题解

  • 0
    @ 2022-08-14 12:07:02
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    inline int read() {
        int x = 0;
        char ch = getchar();
    
        while (ch < '0' || ch > '9')
            ch = getchar();
    
        while (ch >= '0' && ch <= '9') {
            x = x * 10 + ch - '0';
            ch = getchar();
        }
    
        return x;
    }
    int n, m, cnt, top, tim;
    int t[500005], head[500005];
    int st[250005], fa[250005], l[250005], r[250005];
    struct data {
        int to, next;
    } e[500005];
    void insert(int u, int v) {
        e[++cnt].to = v;
        e[cnt].next = head[u];
        head[u] = cnt;
    }
    inline int lowbit(int x) {
        return x & (-x);
    }
    void update(int x, int val) {
        for (int i = x; i <= n + n; i += lowbit(i))
            t[i] += val;
    }
    void ask(int x) {
        int ans = -1;
    
        for (int i = x; i; i -= lowbit(i))
            ans += t[i];
    
        printf("%d\n", ans);
    }
    void dfs() {
        st[++top] = 1;
    
        while (top) {
            int now = st[top], f = fa[top--];
    
            if (!l[now]) {
                l[now] = ++tim;
                st[++top] = now;
    
                for (int i = head[now]; i; i = e[i].next) {
                    if (e[i].to == f)
                        continue;
    
                    st[++top] = e[i].to;
                    fa[top] = now;
                }
            } else
                r[now] = ++tim;
        }
    }
    int main() {
        n = read();
    
        for (int i = 1; i < n; i++) {
            int u = read(), v = read();
            insert(u, v);
            insert(v, u);
        }
    
        dfs();
    
        for (int i = 1; i <= n; i++) {
            update(l[i], 1);
            update(r[i], -1);
        }
    
        m = read();
    
        for (int i = 1; i <= n + m - 1; i++) {
            int x, y;
            char ch[5];
            scanf("%s", ch);
    
            if (ch[0] == 'A') {
                x = read();
                y = read();
                update(l[y], -1);
                update(r[y], 1);
            } else {
                x = read();
                ask(l[x]);
            }
        }
    
        return 0;
    }
    
  • 1

[POI2007] 大都市 Megalopolis

信息

ID
1517
难度
12
分类
树状数组线段树搜索 点击显示
标签
递交数
1
已通过
1
通过率
100%
上传者