• bzoj4756


    http://www.lydsy.com/JudgeOnline/problem.php?id=4756

    水题一枚。。。但是我写了一个小时。。。手贱打反查不出来。。。

    就是每次线段树合并,先把自己的儿子都合并了, 最后和自己合并。。。

    #include<bits/stdc++.h>
    using namespace std;
    const int N = 100010;
    vector<int> G[N];
    int n, sum, cnt;
    int t[N], id[N], size[N * 20], lc[N * 20], rc[N * 20], ans[N], root[N * 20];
    bool cp(int i, int j) { return t[i] < t[j]; } 
    void update(int l, int r, int &x, int pos)
    {
        x = ++cnt; int mid = (l + r) >> 1;
        ++size[x]; if(l == r) return; 
        if(pos <= mid) update(l, mid, lc[x], pos);
        else update(mid + 1, r, rc[x], pos); 
    }
    int merge(int x, int y)
    {
        if(!x || !y) return x + y;
        sum += size[lc[x]] * size[rc[y]];
        lc[x] = merge(lc[x], lc[y]);
        rc[x] = merge(rc[x], rc[y]);
        size[x] += size[y];
        return x; 
    }
    void dfs(int u)
    {
        int last = 0;
        for(int i = 0; i < G[u].size(); ++i)
        {
            int v = G[u][i];
            dfs(v); root[v] = merge(root[v], root[last]);
            last = v;
        }
        sum = 0; root[u] = merge(root[u], root[last]);
        ans[u] = sum;
    }
    int main()
    {
        scanf("%d", &n);
        for(int i = 1; i <= n; ++i) 
        {
            scanf("%d", &t[i]); id[i] = i;
        }
        sort(id + 1, id + n + 1, cp);
        for(int i = 1; i <= n; ++i) t[id[i]] = t[id[i - 1]] + (t[id[i]] != t[id[i - 1]]);
        for(int i = 1; i <= n; ++i) update(1, n, root[i], t[i]);
        for(int i = 2; i <= n; ++i) 
        {
            int x; scanf("%d", &x);
            G[x].push_back(i);
        }
        dfs(1);
        for(int i = 1; i <= n; ++i) printf("%d
    ", ans[i]);
        return 0;
    }
    View Code
  • 相关阅读:
    [SDOI2011] 消防 (树的直径,尺取法)
    [HNOI2006]公路修建问题 (二分答案,并查集)
    P1875 佳佳的魔法药水 (最短路,DP)
    [SCOI2016] 背单词 (Trie 树,贪心)
    [USACO08DEC] 秘密消息Secret Message (Trie树)
    [HDU4745] Two Rabbits (区间DP)
    [HDU4362] Palindrome subsequence (区间DP)
    评价手心输入法
    软件工程个人作业12
    第12周进度条
  • 原文地址:https://www.cnblogs.com/19992147orz/p/6882447.html
Copyright © 2020-2023  润新知