原题链接

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
 
题意:有n个村庄面临饥荒,这些村庄由n-1条路连接,构成一棵树,现在政府发放粮食,这些粮食有不同的类型,粮食发放分为m个阶段,每个阶段选择一条路径发放一种类型的粮食,输出每个点发放哪种粮食最多;
 
思路:树链剖分将路径变为线性的,每次对一条路径发放粮食可以转化为在一段连续区间的开始位置加上1,在结束的下一位减1;

方法就是打标记。线段树维护的是颜色。也就是维护的是[a,b]就是维护a颜色到b颜色某种颜色出现的最多次数。

假设我们处理的是序列而不是树吧。比如我们要把区间[x,y]图成a颜色.那么我们就在x出加个标记a。在y+1就标记-a。

多个标记用邻接表连起来就行了。然后从序列的最左端处理到最右端先把所有标记更新到线段树里。a则a颜色+1。

-a则在线段树将a颜色-1.然后再询问线段树里出现最多的次数就是序列该位置的次数最多的颜色了。相当于递推的思想吧。知道了x位置的颜色线段树.x+1位置的颜色线段树          无非是多了一些颜色或者少了某些颜色。多了减掉。少了的加上就是自己这个位置上的了。这样做之所以高效的原因是标记的是区间的端点而不是区间类的每一个元素。总的          时间复杂度m*log(n)*log(c)。m为询问数。n为结点数。c为颜色种数。

我的代码如下:
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <cstring>
  5. #define N 100005
  6. #define M 200005
  7. using namespace std;
  8. int n,q,cnt,sz;
  9. int fa[N][],deep[N],size[N],head[N];
  10. int pos[N],belong[N];
  11. bool vis[N];
  12. int mv[],leaf[],h[M];
  13.  
  14. struct data
  15. {
  16. int to,next;
  17. } e[M],w[M*];
  18.  
  19. void insert(int u,int v)
  20. {
  21. e[++cnt].to=v;
  22. e[cnt].next=head[u];
  23. head[u]=cnt;
  24. e[++cnt].to=u;
  25. e[cnt].next=head[v];
  26. head[v]=cnt;
  27. }
  28.  
  29. void init()
  30. {
  31. cnt=;
  32. sz=;
  33. memset(deep,,sizeof(deep));
  34. memset(head,,sizeof(head));
  35. memset(vis,,sizeof(vis));
  36. memset(h,,sizeof(h));
  37. memset(mv,,sizeof(mv));
  38. for(int i=; i<n; i++)
  39. {
  40. int x,y;
  41. scanf("%d%d",&x,&y);
  42. insert(x,y);
  43. }
  44. }
  45.  
  46. void dfs1(int x)
  47. {
  48. size[x]=;
  49. vis[x]=;
  50. for(int i=; i<=; i++)
  51. {
  52. if(deep[x]<(<<i))break;
  53. fa[x][i]=fa[fa[x][i-]][i-];//倍增处理祖先信息
  54. }
  55. for(int i=head[x]; i; i=e[i].next)
  56. {
  57. if(vis[e[i].to])continue;
  58. deep[e[i].to]=deep[x]+;
  59. fa[e[i].to][]=x;
  60. dfs1(e[i].to);
  61. size[x]+=size[e[i].to];
  62. }
  63. }
  64.  
  65. void dfs2(int x,int chain)
  66. {
  67. int k=;
  68. sz++;
  69. pos[x]=sz;//分配x结点在线段树中的编号
  70. belong[x]=chain;
  71. for(int i=head[x]; i; i=e[i].next)
  72. if(deep[e[i].to]>deep[x]&&size[e[i].to]>size[k])
  73. k=e[i].to;//选择子树最大的儿子继承重链
  74. if(k==)return;
  75. dfs2(k,chain);
  76. for(int i=head[x]; i; i=e[i].next)
  77. if(deep[e[i].to]>deep[x]&&k!=e[i].to)
  78. dfs2(e[i].to,e[i].to);//其余儿子新开重链
  79. }
  80.  
  81. void build(int rt,int l,int r)//建线段树
  82. {
  83. if(l==r)
  84. {
  85. leaf[l]=rt;
  86. return;
  87. }
  88. int mid=(l+r)>>;
  89. build(rt<<,l,mid);
  90. build(rt<<|,mid+,r);
  91. }
  92.  
  93. void update(int rt,int c)
  94. {
  95. if(c>) c=;
  96. else c=-;
  97. mv[rt]+=c;
  98. while(rt>)
  99. {
  100. rt>>=;
  101. mv[rt]=max(mv[rt<<],mv[rt<<|]);
  102. }
  103. }
  104.  
  105. int qu(int L,int R,int rt)
  106. {
  107. int ls,rs,mid;
  108. if(mv[rt]==)
  109. return ;
  110. while(L<R)
  111. {
  112. ls=rt<<,rs=ls|,mid=(L+R)>>;
  113. if(mv[rs]>mv[ls])
  114. L=mid+,rt=rs;
  115. else
  116. R=mid,rt=ls;
  117. }
  118. return L;
  119. }
  120.  
  121. void adde(int x,int d)
  122. {
  123. w[++cnt].to=d;
  124. w[cnt].next=h[x];
  125. h[x]=cnt;
  126. }
  127.  
  128. void uppath(int u,int v,int d)
  129. {
  130. int f1=belong[u],f2=belong[v];
  131. while(f1!=f2)
  132. {
  133. if(deep[f1]<deep[f2])
  134. swap(f1,f2),swap(u,v);
  135. adde(pos[f1],d);
  136. adde(pos[u]+,-d);
  137. u=fa[f1][],f1=belong[u];
  138. }
  139. if(deep[u]>deep[v])
  140. swap(u,v);
  141. adde(pos[u],d);
  142. adde(pos[v]+,-d);
  143. }
  144.  
  145. void solve()
  146. {
  147. build(,,);
  148. cnt=;
  149. for(int i=; i<=q; i++)
  150. {
  151. int x,y,z;
  152. scanf("%d%d%d",&x,&y,&z);
  153. uppath(x,y,z);
  154. }
  155. int ans[N];
  156. for(int i=;i<=n;i++)
  157. {
  158. for(int j=h[i];j;j=w[j].next)
  159. {
  160. update(leaf[abs(w[j].to)],w[j].to);
  161. }
  162. ans[i]=qu(,,);
  163. }
  164. for(int i=;i<=n;i++)
  165. {
  166. printf("%d\n",ans[pos[i]]);
  167. }
  168. }
  169.  
  170. int main()
  171. {
  172. while(scanf("%d%d",&n,&q)&&(n+q))
  173. {
  174. init();
  175. dfs1();
  176. dfs2(,);
  177. solve();
  178. }
  179. return ;
  180. }

高手的代码:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <vector>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <iostream>
  7. #include <algorithm>
  8. using namespace std;
  9. typedef long long ll;
  10. typedef unsigned long long ull;
  11. const int inf=0x3f3f3f3f;
  12. const ll INF=0x3f3f3f3f3f3f3f3fll;
  13. const int maxn=;
  14. int fa[maxn],siz[maxn],son[maxn],w[maxn],p[maxn],dep[maxn],fp[maxn],Rank[maxn],ans[maxn];
  15. //fa为父节点,siz为子节点中siz最大的,dep为深度,son为重儿子,w表示在线段树中的位置
  16. int num[maxn<<],ppp[maxn<<];
  17. int tree_id,n;
  18. vector<int>G[maxn];
  19. void dfs1(int u,int ff,int deep){
  20. son[u]=;fa[u]=ff;siz[u]=;dep[u]=deep;
  21. for(unsigned int i=;i<G[u].size();i++){
  22. int v=G[u][i];
  23. if(v==ff) continue;
  24. dfs1(v,u,deep+);
  25. siz[u]+=siz[v];
  26. if(siz[v]>siz[son[u]]) son[u]=v;
  27. }
  28. }
  29. void dfs2(int u,int ff){
  30. w[u]=++tree_id;p[u]=ff;Rank[w[u]]=u;
  31. if(son[u]) dfs2(son[u],ff);
  32. else return ;
  33. for(unsigned int i=;i<G[u].size();i++){
  34. int v=G[u][i];
  35. if(v!=fa[u]&&v!=son[u]) dfs2(v,v);
  36. }
  37. }
  38. void pushup(int node){
  39. if(num[node<<]>=num[node<<|]){
  40. num[node]=num[node<<];ppp[node]=ppp[node<<];
  41. }else{
  42. num[node]=num[node<<|];ppp[node]=ppp[node<<|];
  43.  
  44. }
  45. }
  46. void buildtree(int le,int ri,int node){
  47. if(le==ri){
  48. num[node]=;ppp[node]=le;
  49. return ;
  50. }
  51. int t=(le+ri)>>;
  52. buildtree(le,t,node<<);
  53. buildtree(t+,ri,node<<|);
  54. pushup(node);
  55. }
  56. void update(int pos,int val,int le,int ri,int node){
  57. if(le==ri){
  58. num[node]+=val;
  59. return ;
  60. }
  61. int t=(le+ri)>>;
  62. if(pos<=t) update(pos,val,le,t,node<<);
  63. else update(pos,val,t+,ri,node<<|);
  64. pushup(node);
  65. }
  66. struct nnnn{
  67. int u,v,z;
  68. nnnn(int a,int b,int c){u=a;v=b;z=c;}
  69. };
  70. vector<nnnn>GG;
  71. vector<int>GGG[maxn];
  72. void getsum(int u,int v,int z){
  73. int f1=p[u],f2=p[v];
  74. while(f1!=f2){
  75. if(dep[f1]<dep[f2]){
  76. swap(f1,f2);
  77. swap(u,v);
  78. }
  79. GG.push_back(nnnn(w[f1],w[u],z));
  80. u=fa[f1];f1=p[u];
  81. }
  82. if(dep[u]>dep[v]) swap(u,v);
  83. GG.push_back(nnnn(w[u],w[v],z));
  84. }
  85. int main(){
  86. int u,v,q,op,z;
  87. while(scanf("%d%d",&n,&q)!=-){
  88. if(n==&&q==) break;
  89. for(int i=;i<maxn;i++) G[i].clear(),GGG[i].clear();
  90. GG.clear();
  91. memset(son,,sizeof(son));tree_id=;
  92. for(int i=;i<n-;i++){
  93. scanf("%d%d",&u,&v);
  94. G[u].push_back(v);
  95. G[v].push_back(u);
  96. }
  97. dfs1(,,);
  98. dfs2(,);
  99. int max1=;
  100. for(int i=;i<=q;i++){
  101. scanf("%d%d%d",&u,&v,&z);
  102. max1=max(max1,z);
  103. getsum(u,v,z);
  104. }
  105. if(q==){
  106. for(int i=;i<=n;i++) printf("0\n");
  107. continue;
  108. }
  109. buildtree(,max1,);
  110. for(int i=;i<GG.size();i++){
  111. nnnn ne=GG[i];
  112. GGG[ne.u].push_back(ne.z);
  113. GGG[ne.v+].push_back(-ne.z);
  114. }
  115. for(int i=;i<=n;i++){
  116. for(int j=;j<GGG[i].size();j++){
  117. int ttt=GGG[i][j];
  118. if(ttt<) update(-ttt,-,,max1,);
  119. else update(ttt,,,max1,);
  120. }
  121. if(num[]==) ans[Rank[i]]=;
  122. else ans[Rank[i]]=ppp[];
  123. }
  124. for(int i=;i<=n;i++) printf("%d\n",ans[i]);
  125. }
  126. return ;
  127. }

2014 ICPC---Relief grain(树链剖分)的更多相关文章

  1. hdu 5029 Relief grain(树链剖分+线段树)

    题目链接:hdu 5029 Relief grain 题目大意:给定一棵树,然后每次操作在uv路径上为每一个节点加入一个数w,最后输出每一个节点个数最多的那个数. 解题思路:由于是在树的路径上做操作, ...

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

    Relief grain Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  3. HDU 5029 Relief grain --树链剖分第一题

    题意:给一棵树,每次给两个节点间的所有节点发放第k种东西,问最后每个节点拿到的最多的东西是哪种. 解法:解决树的路径上的修改查询问题一般用到的是树链剖分+线段树,以前不会写,后来学了一下树链剖分,感觉 ...

  4. hdu_5029_relief grain(树链剖分)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5029 题意:给你一个树,然后给你两点,将这两点之间的点涂上颜色,问涂色最多的那个颜色是什么,如果数量相 ...

  5. HDU5029--Relief grain (树链剖分+线段树 )

    题意:n个点构成的无根树,m次操作, 对于操作 x y z, 表示 x 到 y 路径上的 每个点 加一个 z 数字,可重复加.最后输出每个点 加的次数最多的那个数字,如果没有输出0. 赤裸裸的树链剖分 ...

  6. HDU 4897 Little Devil I(树链剖分)(2014 Multi-University Training Contest 4)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4897 Problem Description There is an old country and ...

  7. 2019 icpc南昌全国邀请赛-网络选拔赛J题 树链剖分+离线询问

    链接:https://nanti.jisuanke.com/t/38229 题意: 给一棵树,多次查询,每次查询两点之间权值<=k的边个数 题解: 离线询问,树链剖分后bit维护有贡献的位置即可 ...

  8. 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)

    Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...

  9. 树链剖分+线段树+离线(广州网选赛第八题hdu5029)

    http://acm.hdu.edu.cn/showproblem.php?pid=5029 Relief grain Time Limit: 10000/5000 MS (Java/Others)  ...

随机推荐

  1. Iframe去掉滚动条

    <html><head><title></title></head><body STYLE='OVERFLOW:SCROLL;OVER ...

  2. http的500,502,504错误

    500 500的错误通常是由于服务器上代码出错或者是抛出了异常 解决方法:查看一下对应的代码是不是有问题. 502 502即 Bad Gateway网关(这里的网关是指CGI,即通用网关接口,从名字就 ...

  3. CSS字体

    字体系列 [1]5种通用字体系列:拥有相似外观的字体系列 serif字体:字体成比例,且有上下短线,包括Times\Georgia\New century Schoolbook sans-serif字 ...

  4. android target unknown and state offline解决办法

    没有错,将adb的版本升级一下就好了! 下载地址为:http://files.cnblogs.com/files/hujunzheng/adb1.0.32.zip

  5. Angular从0到1:function(上)

    1.前言 Angular作为最流行的前端MV*框架,在WEB开发中占据了重要的地位.接下来,我们就一步一步从官方api结合实践过程,来学习一下这个强大的框架吧. Note:每个function描述标题 ...

  6. 2014 -> 2015

    2014年初在公司的发展不太顺利, 发现比好多小伙伴的发展速度都要慢了,钱不多,职位也不高,做的事情成长也不快. 为了职业发展考虑,年中就一直想换一个好一点的工作机会, 年中拿了好几个offer, 有 ...

  7. 使用ab进行页面的压力测试

    ab是apache自带的一个很好用的压力测试工具,当安装完apache的时候,就可以在bin下面找到ab 参数说明及示例 我们可以模拟100个并发用户,对一个页面发送1000个请求 输入命令:ab - ...

  8. Java多线程系列--“JUC原子类”04之 AtomicReference原子类

    概要 本章对AtomicReference引用类型的原子类进行介绍.内容包括:AtomicReference介绍和函数列表AtomicReference源码分析(基于JDK1.7.0_40)Atomi ...

  9. AngularJS入门心得3——HTML的左右手指令

    在<AngularJS入门心得1——directive和controller如何通信>我们提到“AngularJS是为了克服HTML在构建应用上的不足而设计的.HTML是一门很好的为静态文 ...

  10. 【Android】Volley做网络请求的几种用法

    前言: 最近在将自己写的烂代码重构,以前使用的网络请求全是基于apache的HttpClient,简单使用还好,使用多了发现重复代码太多,而且每次使用都很繁琐,因此在网上找了半天网络请求的相关类库,最 ...