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

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


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


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

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];


vectora[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;
}


你可能感兴趣的:(HDU,数据结构——树链剖分)