hdu5029 Relief grain (树链剖分)

Relief grain

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


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

Recommend
hujie | We have carefully selected several similar problems for you: 5041 5040 5039 5038 5037



解题思路:这道题想通的话很简单,对数值建线段树,由于是单点查询,对于一个区间[l,r]置z来说,它的影响就是在l
位置把z的次数+1,然后在r+1的位置把z的次数-1,所以通过树链剖分把区间拆成logn段,再把每段的左右点的z记录下来,最后从左往右扫一遍,把这个点所有z的记录都单点更新线段树就行了,因为只有mlogn段,所以总的插入的效率不会超过2m*logn*logn。
比赛时没做出来真是too young too naive。。。

#include <iostream>
#include <cstdio>
#include <vector>
#define maxn 100010
using namespace std;
int n,m;
vector<int> G[maxn],pl[maxn],pr[maxn];
int top[maxn],w[maxn],tw[maxn],pre[maxn],tn,son[maxn],dep[maxn],num[maxn],ans[maxn];
void init(){
    for(int i=0;i<maxn;i++) G[i].clear(),pl[i].clear(),pr[i].clear();
    tn=0;
    dep[1]=0;
}

void dfs(int u,int fa){
    pre[u]=fa,num[u]=1,son[u]=0;
    int nn=G[u].size();
    for(int i=0;i<nn;i++){
        int v=G[u][i];
        if(v!=fa){
            dep[v]=dep[u]+1;
            dfs(v,u);
            num[u]+=num[v];
            if(num[v]>num[son[u]]) son[u]=v;
        }
    }
}
void build_tree(int u,int fa){
    w[u]=++tn,tw[tn]=u,top[u]=fa;
    if(son[u]) build_tree(son[u],fa);
    int nn=G[u].size();
    for(int i=0;i<nn;i++){
        int v=G[u][i];
        if(v!=son[u]&&v!=pre[u]) build_tree(v,v);
    }
}
void calc(int x,int y,int z){
    while(top[x]!=top[y]){
        if(dep[top[x]]<dep[top[y]]) swap(x,y);
        pl[w[top[x]]].push_back(z);
        pr[w[x]+1].push_back(z);
        x=pre[top[x]];
    }
    if(dep[x]<dep[y]) swap(x,y);
    pl[w[y]].push_back(z);
    pr[w[x]+1].push_back(z);
}

struct tree{
    int l,r;
    int Max,v;
}a[maxn<<2];
void pushup(int k){
    if(a[k<<1].Max>=a[k<<1|1].Max){
        a[k].v=a[k<<1].v;
        a[k].Max=a[k<<1].Max;
    }else{
        a[k].v=a[k<<1|1].v;
        a[k].Max=a[k<<1|1].Max;
    }
}
void build(int l,int r,int k){
    a[k].l=l,a[k].r=r;
    if(l==r){
        a[k].v=l;
        a[k].Max=0;
    }else{
        int mid=(l+r)>>1;
        build(l,mid,k<<1);
        build(mid+1,r,k<<1|1);
        pushup(k);
    }
}

void insert(int x,int v,int k){
    if(a[k].l==a[k].r){
        a[k].Max+=v;
    }else{
        int mid=(a[k].l+a[k].r)>>1;
        if(x<=mid) insert(x,v,k<<1);
        else insert(x,v,k<<1|1);
        pushup(k);
    }
}
void read(){
    int u,v;
    for(int i=0;i<n-1;i++){
        scanf("%d%d",&u,&v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
}
void solve(){
    dfs(1,1);
    build_tree(1,1);
    int x,y,z;
    for(int i=0;i<m;i++){
        scanf("%d%d%d",&x,&y,&z);
        calc(x,y,z);
    }
    build(1,100000,1);
    for(int i=1;i<=n;i++){
        for(int j=0;j<pl[i].size();j++) insert(pl[i][j],1,1);
        for(int j=0;j<pr[i].size();j++) insert(pr[i][j],-1,1);
        if(a[1].Max==0) ans[tw[i]]=0;
        else ans[tw[i]]=a[1].v;
    }
    for(int i=1;i<=n;i++){
        printf("%d\n",ans[i]);
    }
}
int main(){
    while(~scanf("%d%d",&n,&m)){
        if(n==0&&m==0) break;
        init();
        read();
        solve();
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值