2014广州网络预选赛1008(树链剖分)HDU5029

本文介绍了一种利用树链剖分算法解决救援粮高效分配的问题。通过将树形结构转换为线性结构,实现了在树的路径上进行快速增减操作,进而找出每个村庄接收最多类型的救援粮。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Relief grain

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


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

题意:RT

思路:树链剖分,比赛的时候不会树链剖分,但是用LCT又yy不出来,只好哭等比赛结束,sad~

            今天去看了树链剖分,才豁然开朗啊(LCT也是可做的)

            用一个vector维护每条重链即可,如果要在u和v之间路径上的所有点加一个数z

            那么就在u和v路径上的所有重链上都将z加上去,因为每条重链上的点转化为线性结构以后在线段上一定是连续的一段区间

            那么就转化为了在一个区间里加z,这个可以用经典操作[l,r],在l这里+z,在r+1这里-z,那么前缀和就是该点的值

            其中对每条路径的操作复杂度为logn,最后的查询用线段树来维护区间最优值,其中线段树维护的是z,操作只有+1或-1

            具体看代码吧~~

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         #include 
        
          using namespace std; const int MAXN = 100010; int head[MAXN]; int next[MAXN<<1]; int edge[MAXN<<1]; int esz; int deep[MAXN]; int son[MAXN]; int fa[MAXN]; int sz[MAXN]; int ord[MAXN]; int up[MAXN]; int ha[MAXN]; int ans[MAXN]; int dfs_clock; vector 
         
           vadd[MAXN]; vector 
          
            vsub[MAXN]; void init() { memset(head,-1,sizeof(head)); esz=0;dfs_clock=0;deep[0]=0; } void add(int u,int v) { edge[esz]=v; next[esz]=head[u]; head[u]=esz++; } void dfs(int u,int p) { fa[u]=p; deep[u]=deep[p]+1; sz[u]=1; son[u]=0; for(int i=head[u];i!=-1;i=next[i]){ int v=edge[i]; if(v==p)continue; dfs(v,u); sz[u]+=sz[v]; if(!son[u] || sz[v] > sz[son[u]]){ son[u]=v; } } } void dfs1(int u,int p) { ord[u]=++dfs_clock; up[u]=p; ha[ord[u]]=u; if(!son[u])return; dfs1(son[u],p); for(int i=head[u];i!=-1;i=next[i]){ int v=edge[i]; if(v==fa[u] || v==son[u])continue; dfs1(v,v); } } void fx(int u,int v,int z) { int pu=up[u]; int pv=up[v]; while(pu != pv){ if(deep[pu] 
           
             deep[v])swap(u,v); vadd[ord[u]].push_back(z); vsub[ord[v]+1].push_back(z); } #define lson l,m,o<<1 #define rson m+1,r,o<<1|1 int mx[MAXN<<2]; int id[MAXN<<2]; void pushup(int o) { mx[o<<1] 
            
              <<1|1] ? (mx[o]=mx[o<<1|1],id[o]=id[o<<1|1]):(mx[o]=mx[o<<1],id[o]=id[o<<1]); } void build(int l,int r,int o) { if(l==r){ mx[o]=0; id[o]=l; return; } int m=l+r>>1; build(lson); build(rson); pushup(o); } void update(int l,int r,int o,int k,int v) { if(l==r){ mx[o]+=v; return; } int m=l+r>>1; if(k<=m)update(lson,k,v); else update(rson,k,v); pushup(o); } int main() { int n,m; while(scanf("%d%d",&n,&m)!=EOF) { if(!n&&!m)break; init(); for(int i=1;i 
              
             
            
           
          
         
       
      
      
     
     
    
    
   
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值