HDU5029 Relief grain 树链剖分+线段树离线

HDU5029 Relief grain  树链剖分+线段树离线

Relief grain

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others)
Total Submission(s): 1715    Accepted Submission(s): 422


Problem 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
For the first test case, the relief grain in the 1st village is {1, 2}, and the relief grain in the 2nd village is {1, 2, 2}.
 Source
 
题意:给一颗树,每次给u和v路径上的所有结点增加某种粮食,增加量为1,要求最后输出每个点的粮食量最多的粮食种类,粮食种类100000种。
思路:这题难点不在树剖,在线段树离线维护上。
先剖分成链,然后记录每次修改的左右端点,左端点+1,右端点-1,然后按粮食种类建树,然后在剖分后的链上从左往右递推,修改线段树上特定种类的粮食数量。
好题!
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
#define PB push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define MS0(a) memset(a,0,sizeof(a))

using namespace std;

const int maxn=1000100;

int n,q;
int x,y,z;
vector<int> G[maxn];
int fa[maxn],son[maxn],dep[maxn],siz[maxn];
int top[maxn];
int id[maxn],num;
vector<int> add[maxn];
vector<int> cut[maxn];
int N=100010;
struct SegTree
{
    int cnt,id;
};SegTree T[maxn<<2];
int ans[maxn];

void dfs(int u,int f,int d)
{
    fa[u]=f;
    dep[u]=d;
    siz[u]=1;
    son[u]=0;
    for(int i=0;i<G[u].size();i++){
        int v=G[u][i];
        if(v==f) continue;
        dfs(v,u,d+1);
        siz[u]+=siz[v];
        if(siz[v]>siz[son[u]]) son[u]=v;
    }
}

void dfs1(int u,int tp)
{
    top[u]=tp;
    id[u]=++num;
    if(son[u]) dfs1(son[u],tp);
    for(int i=0;i<G[u].size();i++){
        int v=G[u][i];
        if(v==fa[u]||v==son[u]) continue;
        dfs1(v,v);
    }
}

void change(int u,int v,int z)
{
    while(top[u]!=top[v]){
        if(dep[top[u]]<dep[top[v]]) swap(u,v);
        add[id[top[u]]].PB(z);
        cut[id[u]+1].PB(z);
        u=fa[top[u]];
    }
    if(dep[u]>dep[v]) swap(u,v);
    add[id[u]].PB(z);
    cut[id[v]+1].PB(z);
}

void push_up(int rt)
{
    if(T[rt<<1].cnt>=T[rt<<1|1].cnt) T[rt]=T[rt<<1];
    else T[rt]=T[rt<<1|1];
}

void build(int l,int r,int rt)
{
    if(l==r){
        T[rt].cnt=0;
        T[rt].id=l;
        return;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    push_up(rt);
}

void update(int p,int c,int l,int r,int rt)
{
    if(l==r){
        T[rt].cnt+=c;
        return;
    }
    int m=(l+r)>>1;
    if(p<=m) update(p,c,lson);
    else update(p,c,rson);
    push_up(rt);
}

int main()
{
    freopen("in.txt","r",stdin);
    while(cin>>n>>q){
        if(n==0&&q==0) break;
        for(int i=1;i<=n;i++) G[i].clear();
        REP(i,1,n-1){
            scanf("%d%d",&x,&y);
            G[x].PB(y);
            G[y].PB(x);
        }
        num=0;
        dfs(1,0,1);
        dfs1(1,1);
        for(int i=0;i<maxn;i++) add[i].clear(),cut[i].clear();
        while(q--){
            scanf("%d%d%d",&x,&y,&z);
            change(x,y,z);
        }
        build(1,N,1);
        REP(i,1,num){
            for(int j=0;j<add[i].size();j++) update(add[i][j],1,1,N,1);
            for(int j=0;j<cut[i].size();j++) update(cut[i][j],-1,1,N,1);
            if(T[1].cnt==0) ans[i]=0;
            else ans[i]=T[1].id;
        }
        for(int i=1;i<=n;i++){
            printf("%d\n",ans[id[i]]);
        }
    }
    return 0;
}
View Code

 

 
 
 
posted @ 2015-09-10 13:54  __560  阅读(235)  评论(0编辑  收藏  举报