P3605 [USACO17JAN] 线段树合并 / BIT

题意

传送门 P3605 [USACO17JAN]Promotion Counting P

题解
线段树合并

对每一个节点,建立一颗动态开点的权值线段树,将对应能力值插入线段树后,进行一次 D F S DFS DFS,将所有子节点与当前节点合并,同时查询答案。时间复杂度 O ( N log ⁡ N ) O(N\log N) O(NlogN)

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005, maxlg = 18;
struct node
{
#define lc(x) tree[x].lc
#define rc(x) tree[x].rc
#define sum(x) tree[x].sum
    int lc, rc, sum;
} tree[maxn * maxlg];
int N, tot, P[maxn], sp[maxn], rt[maxn], res[maxn];
int E, head[maxn], to[maxn << 1], nxt[maxn << 1];

void add(int x, int y) { to[++E] = y, nxt[E] = head[x], head[x] = E; }

void insert(int &p, int l, int r, int x, int d)
{
    if (!p)
        p = ++tot;
    if (r - l == 1)
    {
        sum(p) += d;
        return;
    }
    int m = (l + r) >> 1;
    x < m ? insert(lc(p), l, m, x, d) : insert(rc(p), m, r, x, d);
    sum(p) = sum(lc(p)) + sum(rc(p));
}

int merge(int p, int q, int l, int r)
{
    if (!p || !q)
        return p + q;
    if (r - l == 1)
    {
        sum(p) += sum(q);
        return p;
    }
    int m = (l + r) >> 1;
    lc(p) = merge(lc(p), lc(q), l, m), rc(p) = merge(rc(p), rc(q), m, r);
    sum(p) = sum(lc(p)) + sum(rc(p));
    return p;
}

int ask(int a, int b, int p, int l, int r)
{
    if (r <= a || b <= l)
        return 0;
    if (a <= l && r <= b)
        return sum(p);
    int m = (l + r) >> 1;
    return ask(a, b, lc(p), l, m) + ask(a, b, rc(p), m, r);
}

void dfs(int x, int f)
{
    for (int i = head[x]; i; i = nxt[i])
    {
        int y = to[i];
        if (y != f)
        {
            dfs(y, x);
            rt[x] = merge(rt[x], rt[y], 0, N);
        }
    }
    res[x] = ask(P[x] + 1, N, rt[x], 0, N);
}

int main()
{
    scanf("%d", &N);
    for (int i = 1; i <= N; ++i)
        scanf("%d", P + i), sp[i] = P[i];
    for (int i = 2, f; i <= N; ++i)
    {
        scanf("%d", &f);
        add(i, f), add(f, i);
    }
    sort(sp + 1, sp + N + 1);
    for (int i = 1; i <= N; ++i)
        P[i] = lower_bound(sp + 1, sp + N + 1, P[i]) - (sp + 1);
    for (int i = 1; i <= N; ++i)
        insert(rt[i], 0, N, P[i], 1);
    dfs(1, 0);
    for (int i = 1; i <= N; ++i)
        printf("%d\n", res[i]);
    return 0;
}
BIT

能力值的个数满足区间可加性,使用 B I T BIT BIT 维护。在树上 D F S DFS DFS 至节点 x x x 时,统计不属于以节点 x x x 为根节点的子树上的点中大于 p x p_x px 的节点个数,回溯时将 s u m ( N ) − s u m ( x ) sum(N)-sum(x) sum(N)sum(x) 减去这个值,就得到了属于以节点 x x x 为根节点的子树上的点中大于 p x p_x px 的节点个数。时间复杂度 O ( N log ⁡ N ) O(N\log N) O(NlogN)

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int N, tot, P[maxn], sp[maxn], bit[maxn], res[maxn];
int E, head[maxn], to[maxn << 1], nxt[maxn << 1];

void add(int x, int y) { to[++E] = y, nxt[E] = head[x], head[x] = E; }

void add(int i)
{
    while (i <= N)
        ++bit[i], i += i & -i;
}

int sum(int i)
{
    int s = 0;
    while (i)
        s += bit[i], i -= i & -i;
    return s;
}

void dfs(int x, int f)
{
    int pre = sum(N) - sum(P[x]);
    add(P[x]);
    for (int i = head[x]; i; i = nxt[i])
    {
        int y = to[i];
        if (y != f)
            dfs(y, x);
    }
    res[x] = sum(N) - sum(P[x]) - pre;
}

int main()
{
    scanf("%d", &N);
    for (int i = 1; i <= N; ++i)
        scanf("%d", P + i), sp[i] = P[i];
    for (int i = 2, f; i <= N; ++i)
    {
        scanf("%d", &f);
        add(i, f), add(f, i);
    }
    sort(sp + 1, sp + N + 1);
    for (int i = 1; i <= N; ++i)
        P[i] = lower_bound(sp + 1, sp + N + 1, P[i]) - sp;
    dfs(1, 0);
    for (int i = 1; i <= N; ++i)
        printf("%d\n", res[i]);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值