HDU5029 Relief grain 树链剖分+差分统计答案

大致题意:给出一棵n个节点有根树,现在给m个x、y,使得x到y路径上所有点加上标记z,现需要统计每个节点中数量最多的标记种类


先考虑线性序列,在x-y添加标记z,利用差分思想,在x处添加z,在y+1减去z,然后用一个维护标记数的线段树顺序维护,每个节点询问数量最多的节点即可。然后树型结构转线性,利用树剖即可。


#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

const int maxn = 100010;

struct edge
{
    int v,next;
}e[maxn << 1];
int h[maxn],num;
struct node
{
    int l,r,x,y;
}t[maxn << 1];
int tot,cnt,ans[maxn];
int fa[maxn],son[maxn],pos[maxn],ppos[maxn],top[maxn],size[maxn],dep[maxn];


vector<int>a[maxn];
int n,q;

void add_edge(int u,int v)
{
    num++;
    e[num].v = v;
    e[num].next = h[u];
    h[u] = num;
}
int u,v;

void build(int p,int l,int r)
{
    int mid = l + r >> 1;
    if(l == r)
    {
        t[p].x = l;
        t[p].y = 0;
        return;
    }
    t[p].x = 0;
    t[p].y = 0;
    t[p].l = ++tot;
    t[p].r = ++tot;
    build(t[p].l,l,mid);
    build(t[p].r,mid+1,r);
}

void change(int p,int l,int r,int a,int b)
{
    int mid = l + r >> 1;
    if(l == r)
    {
        t[p].y += b;
        return;
    }
    if(a <= mid)
        change(t[p].l,l,mid,a,b);
    else if(a > mid)
        change(t[p].r,mid+1,r,a,b);
    if(t[t[p].l].y >= t[t[p].r].y)
    {
        t[p].x = t[t[p].l].x;
        t[p].y = t[t[p].l].y;
    }
    else
    {
        t[p].x = t[t[p].r].x;
        t[p].y = t[t[p].r].y;
    }
}

void dfs1(int x)
{
    dep[x] = dep[fa[x]] + 1;
    size[x] = 1;
    son[x] = 0;
    for(int i = h[x]; i; i = e[i].next)
    {
        if(e[i].v == fa[x])
            continue;
        fa[e[i].v] = x;
        dfs1(e[i].v);
        if(size[e[i].v] > size[son[x]])
            son[x] = e[i].v;
        size[x] += size[e[i].v];
    }
}

void dfs2(int x)
{
    if(son[fa[x]] == x)
        top[x] = top[fa[x]];
    else
    {
        top[x] = x;
        for(int i = x; i; i = son[i])
        {
            pos[i] = ++cnt;
            ppos[cnt] = i;
        }
    }
    for(int i = h[x]; i; i = e[i].next)
        if(e[i].v != fa[x])
            dfs2(e[i].v);
}

void updata(int x,int y,int z)
{
    int fx = top[x];
    int fy = top[y];
    while(fx != fy)
    {
        if(dep[fx] > dep[fy])
        {
            a[pos[x]+1].push_back(-z);
            a[pos[top[x]]].push_back(z);
            x = fa[fx];
            fx = top[x];
        }
        else
        {
            a[pos[y]+1].push_back(-z);
            a[pos[top[y]]].push_back(z);
            y = fa[fy];
            fy = top[y];
        }
    }
    if(pos[x] > pos[y])
    {
        a[pos[y]].push_back(z);
        a[pos[x]+1].push_back(-z);
    }
    else
    {
        a[pos[x]].push_back(z);
        a[pos[y]+1].push_back(-z);
    }
}

int main()
{
    while(scanf("%d%d",&n,&q), n != 0 || q != 0)
    {
        num = cnt = tot = 0;
        memset(h,0,sizeof(h));
        memset(fa,0,sizeof(fa));
        memset(size,0,sizeof(size));
        memset(son,0,sizeof(size));
        memset(top,0,sizeof(top));
        memset(dep,0,sizeof(dep));
        for(int i = 1; i <= n; i++)
            a[i].clear();
        for(int i = 0; i < n - 1; i++)
        {
            scanf("%d%d",&u,&v);
            add_edge(u,v);
            add_edge(v,u);
        }
        dfs1(1);
        dfs2(1);
        int x,y,z;
        int nn = 0;
        while(q--)
        {
            scanf("%d%d%d",&x,&y,&z);    
            updata(x,y,z);
            nn = max(nn,z);
        }
        build(0,0,nn);
        for(int i = 1; i <= n; i++)
        {
            for(int j = 0; j < a[i].size(); j++)
            {
                if(a[i][j] > 0)
                    change(0,0,nn,a[i][j],1);
                else
                    change(0,0,nn,-a[i][j],-1);    
            }
            ans[ppos[i]] = t[0].x;
        }
        for(int i = 1; i <= n; i++)
            printf("%d\n",ans[i]);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值