Relief grain

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

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
2014 ACM/ICPC Asia Regional Guangzhou Online
 

这个题大概和5044一样的、也是利用前缀和的思想,比如1到5都加了3,那么就在查询1时加上3,查询6时减去3,用线段树来维护出现次数最多的。

最开始把100000写成n、找了好久的错。。。

#pragma comment(linker, "/STACK:1024000000,1024000000") //手动加栈、windows系统容易爆栈
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
#define INF 0x7ffffff
#define ll __int64
#define N 100010 struct Edge
{
int to,next;
}edge[N<<];
int head[N],tot; int top[N];
int fa[N];
int deep[N];
int size[N];
int p[N];
int fp[N];
int son[N];
int pos;
int ans[N];
vector<int>v1[N],v2[N]; void init()
{
tot=;
pos=;
memset(head,-,sizeof(head));
memset(son,-,sizeof(son));
}
void add(int x,int y)
{
edge[tot].to=y;
edge[tot].next=head[x];
head[x]=tot++;
}
void dfs1(int now,int pre,int d)
{
deep[now]=d;
fa[now]=pre;
size[now]=;
for(int i=head[now];i!=-;i=edge[i].next)
{
int next=edge[i].to;
if(next!=pre)
{
dfs1(next,now,d+);
size[now]+=size[next];
if(son[now]==- || size[next]>size[son[now]])
{
son[now]=next;
}
}
}
}
void dfs2(int now,int tp)
{
top[now]=tp;
p[now]=pos++;
fp[p[now]]=now;
if(son[now]==-) return;
dfs2(son[now],tp);
for(int i=head[now];i!=-;i=edge[i].next)
{
int next=edge[i].to;
if(next!=son[now] && next!=fa[now])
{
dfs2(next,next);
}
}
}
void change(int x,int y,int z)
{
int f1=top[x];
int f2 = top[y];
while(f1!=f2)
{
if(deep[f1]<deep[f2])
{
swap(f1,f2);
swap(x,y);
}
v1[p[f1]].push_back(z);
v2[p[x]+].push_back(z);
x=fa[f1];
f1=top[x];
}
if(deep[x]>deep[y]) swap(x,y);
v1[p[x]].push_back(z);
v2[p[y]+].push_back(z);
} /* 线段树 */
int mx[N<<];
int id[N<<]; void pushup(int rt)
{
if(mx[rt<<]<mx[rt<<|])
{
mx[rt]=mx[rt<<|];
id[rt]=id[rt<<|];
}
else
{
mx[rt]=mx[rt<<];
id[rt]=id[rt<<];
}
}
void build(int l,int r,int rt)
{
if(l==r)
{
id[rt]=l;
mx[rt]=;
return;
}
int m=(l+r)>>;
build(l,m,rt<<);
build(m+,r,rt<<|);
pushup(rt);
}
void update(int l,int r,int rt,int pos,int op)
{
if(l== pos && r == pos)
{
mx[rt]+=op;
return;
}
int m=(l+r)>>;
if(pos<=m) update(l,m,rt<<,pos,op);
else update(m+,r,rt<<|,pos,op);
pushup(rt);
}
int main()
{
int n,m,i,j;
while(scanf("%d%d",&n,&m), n||m)
{
init();
for(i=;i<n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
add(a,b);
add(b,a);
}
dfs1(,,);
dfs2(,);
for(i=;i<=;i++)
{
v1[i].clear();
v2[i].clear();
}
while(m--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
change(a,b,c);
}
build(,,);
for(i=;i<=n;i++)
{
for(j=;j<v1[i].size();j++)
{
update(,,,v1[i][j],);
}
for(j=;j<v2[i].size();j++)
{
update(,,,v2[i][j],-);
}
if(!mx[]) ans[fp[i]]=;
else ans[fp[i]]=id[];
}
for(i=;i<=n;i++)
{
printf("%d\n",ans[i]);
}
}
return ;
}

[HDU 5029] Relief grain的更多相关文章

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

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

  2. HDU 5029 Relief grain(离线+线段树+启发式合并)(2014 ACM/ICPC Asia Regional Guangzhou Online)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5029 Problem Description The soil is cracking up beca ...

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

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

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

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

  5. 树链剖分+线段树 HDOJ 5029 Relief grain(分配粮食)

    题目链接 题意: 分粮食我就当成涂色了.有n个点的一棵树,在a到b的路上都涂上c颜色,颜色可重复叠加,问最后每一个点的最大颜色数量的颜色类型. 思路: 首先这题的输出是每一个点最后的情况,考虑离线做法 ...

  6. J - Relief grain HDU - 5029

    Relief grain Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others)T ...

  7. 树链剖分处理+线段树解决问题 HDU 5029

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5029 题意:n个点的树,m次操作.每次操作输入L,R,V,表示在[L,R]这个区间加上V这个数字.比 ...

  8. hdu5029 Relief grain

    题目链接 树剖+线段树 将区间修改转化为单点修改,因为如果按DFS序进行修改,那么一定会对DFS序更大的点造成影响 #include<iostream> #include<vecto ...

  9. hdu 5029树链剖分

    /* 解:标记区间端点,按深度标记上+下-. 然后用线段树维护求出最小的,再将它映射回来 */ #pragma comment(linker, "/STACK:102400000,10240 ...

随机推荐

  1. 12_CXF入门

    [CXF] Apache CXF = Celtix + Xfire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF.Apache CXF 是一个开 ...

  2. Visualsvn Server的搭建

    最近做项目在用svn,对svn有个初步了解,今天利用一点时间在本地配置了一个svn的服务端.整个过程分为以下几步: ①下载Visualsvn Server并且进行安装. ②安装好以后,在Visuals ...

  3. [leetcode] 406. Queue Reconstruction by Height

    https://leetcode.com/contest/6/problems/queue-reconstruction-by-height/ 分析:每个表示成(a,b)的形式,其实找第一个,就是b为 ...

  4. MFC通过ADO操作Access数据库

    我在<VC知识库在线杂志>第十四期和第十五期上曾发表了两篇文章——“直接通过ODBC读.写Excel表格文件”和“直接通过DAO读.写Access文件”,先后给大家介绍了ODBC和DAO两 ...

  5. 关于C#虚函数和构造函数的一点理解

    虚函数感觉总是很神秘,在本质的原理上一直也没有弄得很透彻,今天又有一点的新的感悟,纪录下来,有时间的话可以去研究一下C++对象模型 using System; using System.Collect ...

  6. Less使用——让老司机带你飞

    为什么我要使用Less less的作为编写css的工具插件,省时.方便.检测,具体的安装,请参考我的一篇文章<sublime text3 个人使用心得>,里面我讲解了安装方法,使用webs ...

  7. Convert.ToInt32()与int.Parse()的区别 (转载)

    Convert.ToInt32()与int.Parse()的区别(1)这两个方法的最大不同是它们对null值的处理:Convert.ToInt32(null)会返回0而不会产生任何异常,但int.Pa ...

  8. div+css实现未知宽高元素垂直水平居中

    div+css实现未知宽高元素垂直水平居中.很多同学在面试的时候都会遇到这样的问题:怎么用div+css的方法实现一个未知宽高的弹出框(或者图片)垂直水平居中??如果用JS的话就好办了,但是JS的使用 ...

  9. MongoDB入门三步曲3--部署技术:主备、副本集和数据分片

    mongodb部署--主备.副本及数据分片 主备复制 副本集 数据分片 主备复制 主备复制是最基本的一种多点部署方案,在读写分离.热备份.数据恢复等方面具有重要作用. 在真实的生产环境,主备库肯定需要 ...

  10. JS的this本质

    1.this究竟为何物? 1.1 全局上下文(Global context ) 在全局运行上下文中(在任何函数体外部),this 指代全局对象window,无论是否在严格模式下. alert(this ...