HDU 5029 Relief grain 树链剖分打标记 线段树区间最大值

Relief grain

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5029

Description

The soil is cracking up because of the drought and the rabbit kingdom is facing a serious famine. The RRC(Rabbit Red Cross) organizes the distribution of relief grain in the disaster area. 

We can regard the kingdom as a tree with n nodes and each node stands for a village. The distribution of the relief grain is divided into m phases. For each phases, the RRC will choose a path of the tree and distribute some relief grain of a certain type for every village located in the path. 

There are many types of grains. The RRC wants to figure out which type of grain is distributed the most times in every village.

Input

The input consists of at most 25 test cases. 

For each test case, the first line contains two integer n and m indicating the number of villages and the number of phases. 

The following n-1 lines describe the tree. Each of the lines contains two integer x and y indicating that there is an edge between the x-th village and the y-th village. 
   
The following m lines describe the phases. Each line contains three integer x, y and z indicating that there is a distribution in the path from x-th village to y-th village with grain of type z. (1 <= n <= 100000, 0 <= m <= 100000, 1 <= x <= n, 1 <= y <= n, 1 <= z <= 100000) 

The input ends by n = 0 and m = 0.

Output

For each test case, output n integers. The i-th integer denotes the type that is distributed the most times in the i-th village. If there are multiple types which have the same times of distribution, output the minimal one. If there is no relief grain in a village, just output 0.

Sample Input

2 4
1 2
1 1 1
1 2 2
2 2 2
2 2 1
5 3
1 2
3 1
3 4
5 3
2 3 3
1 5 2
3 3 3
0 0

Sample Output

1
2
2
3
3
0
2

HINT

 

题意

有n个点的树,有m次操作

操作是在x-y的链上的每一个点,都增加一个W

然后让你输出每个点出现次数最多的数是什么

题解:

看到链状,很显然就是树链剖分

我们对于每个询问都按照lca去打上标记,就G[u].push_back(v,1),G[v].push_back(v,-1)这种

打完标记之后,我们再按照树链剖分的顺序去处理这些标记就好了

代码:

#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <vector>
using namespace std;
const int N=800010;
const int INF=1<<30;

int n,tim;
vector<pair<int,int> >G[100050];
long long num[N];
int siz[N],top[N],son[N];
int dep[N],tid[N],Rank[N],fa[N];
int head[N],to[2*N],Next[2*N],edge;
int flag = 0;
struct Edge
{
    int u,v;
};
Edge tmp[2*N];
int ans[N];
void Init()
{
    memset(head,-1,sizeof(head));
    memset(son,-1,sizeof(son));
    tim=0;
    edge=0;
    for(int i=0;i<100050;i++)
        G[i].clear();
    memset(ans,0,sizeof(ans));
}

void addedge(int u,int v)
{
    to[edge]=v,Next[edge]=head[u],head[u]=edge++;
    to[edge]=u,Next[edge]=head[v],head[v]=edge++;
}

//树链剖分部分
void dfs1(int u,int father,int d)
{
    dep[u]=d;
    fa[u]=father;
    siz[u]=1;
    for(int i=head[u]; ~i; i=Next[i])
    {
        int v=to[i];
        if(v!=father)
        {
            dfs1(v,u,d+1);
            siz[u]+=siz[v];
            if(son[u]==-1||siz[v]>siz[son[u]])
                son[u]=v;
        }
    }
}

void dfs2(int u,int tp)
{
    top[u]=tp;
    tid[u]=++tim;
    Rank[tid[u]]=u;
    if(son[u]==-1) return;
    dfs2(son[u],tp);
    for(int i=head[u]; ~i; i=Next[i])
    {
        int v=to[i];
        if(v!=son[u]&&v!=fa[u])
            dfs2(v,v);
    }
}




void Do(int l,int r,int x)
{
    //cout<<l<<" "<<r<<endl;
    G[l].push_back(make_pair(x,1));
    G[r].push_back(make_pair(x,-1));
}

void solve(int x,int y,int val)
{
    while(top[x]!=top[y])
    {
        if(dep[top[x]]<dep[top[y]]) swap(x,y);
        Do(top[x],x,val);
        x=fa[top[x]];
    }
    if(dep[x]>dep[y]) swap(x,y);
    Do(x,y,val);
}
/////////////////////////////////////////////////////////线段树

typedef int SgTreeDataType;
struct treenode
{
  int L , R  ;
  SgTreeDataType Max1 , lazy ,Max2;
};

treenode tree[N];

inline void build_tree(int L , int R , int o)
{
    tree[o].L = L , tree[o].R = R,tree[o].Max1 = 0;
    tree[o].Max2 = L;
    if (R > L)
    {
        int mid = (L+R) >> 1;
        build_tree(L,mid,o*2);
        build_tree(mid+1,R,o*2+1);
    }
}

inline void updata(int QL,int QR,SgTreeDataType v,int o)
{
    int L = tree[o].L , R = tree[o].R;
    if (QL <= L && R <= QR)
        tree[o].Max1 += v;
    else
    {
        int mid = (L+R)>>1;
        if (QL <= mid) updata(QL,QR,v,o*2);
        if (QR >  mid) updata(QL,QR,v,o*2+1);
        if(tree[o*2].Max1>=tree[o*2+1].Max1)
            tree[o].Max1 = tree[o*2].Max1,tree[o].Max2 = tree[o*2].Max2;
        else
            tree[o].Max1 = tree[o*2+1].Max1,tree[o].Max2 = tree[o*2+1].Max2;
    }
}

///////////////////////////////////////////////////
int MM;
void Get(int u,int tp)
{
    //cout<<u<<endl;
    for(int j=0;j<G[u].size();j++)
    {
        if(G[u][j].second==-1)continue;
        //cout<<G[u][j].first<<" "<<G[u][j].second<<endl;
        updata(G[u][j].first,G[u][j].first,G[u][j].second,1);
    }
    //cout<<u<<" "<<tree[1].Max2<<" flag"<<endl;
    ans[u]=tree[1].Max2;
    for(int j=0;j<G[u].size();j++)
    {
        if(G[u][j].second==1)continue;
        //cout<<G[u][j].first<<" "<<G[u][j].second<<endl;
        updata(G[u][j].first,G[u][j].first,G[u][j].second,1);
    }
    if(son[u]==-1) return;
    Get(son[u],tp);
    for(int i=head[u]; ~i; i=Next[i])
    {
        int v=to[i];
        if(v!=son[u]&&v!=fa[u])
            Get(v,v);
    }
}
int main()
{
    while(scanf("%d%d",&n,&MM)!=EOF)
    {
        if(n==0&&MM==0)break;
        Init();
        for(int i=1; i<n; i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            addedge(a,b);
        }
        dfs1(1,1,1);
        dfs2(1,1);
        while(MM--)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            solve(x,y,z);
        }
        build_tree(0,100005,1);
        Get(1,1);
        for(int i=1;i<=n;i++)
            printf("%d\n",ans[i]);
    }
    return 0;
}

 

    原文作者:qscqesze
    原文地址: https://www.cnblogs.com/qscqesze/p/4999424.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞