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. C++成员变量初始化顺序问题

    由于面试题中,考官出了一道简单的程序输出结果值的题:如下, class A { private: int n1; int n2; public: A():n2(0),n1(n2+2){} void P ...

  2. Where does Windows store MSI files for uninstallation?

    Original link: Where does Windows store MSI files for uninstallation? Following content are only use ...

  3. VS2010 error RC2135: file not found

    VS2010 C++ win32 DLL 工程, 添加 rc 文件, 编辑 String Table. 默认情况下英文版本的 rc 文件能够顺序编译通过,为了让工程支持多语言,将字符串修改为其他语言时 ...

  4. PHPEXCEL使用实例

    最近在项目中要用到PHP生成EXCEL,上网找了一下,发现PHPEXCEL挺不错,用了一下,感觉还行,就是设置单元格格式的时候比较麻烦,总体来说功能还是比较强大的,还有生成PDF什么的,发一个实例吧 ...

  5. 带你初识Angular中MVC模型

    简介 MVC是一种使用 MVC(Model View Controller 模型-视图-控制器)设计模式,该模型的理念也被许多框架所吸纳,比如,后端框架(Struts.Spring MVC等).前端框 ...

  6. Nginx 域名转发

    例如访问www.b.cn直接跳到www.a.cn上去,又不想多域名捆绑一个目录. server { listen 80; server_name www.b.cn; rewrite ^/(.*)$ h ...

  7. git 使用事项

    基本安装可查看 http://help.github.com 如果删除了本地的文件,要恢复相关文件,在github存在(别人增加的),则:git pull <远程主机名> <远程分支 ...

  8. 一个简单的WebService实例

    WebService在.NET平台下的作用是在不同应用程序间共享数据与数据交换. 要达到这样的目标,Web services要使用两种技术: XML(标准通用标记语言下的一个子集):XML是在web上 ...

  9. yii2配置表前缀

    前缀设置 component中db的配置修改 'db'=>array( 'connectionString' => 'mysql:host=localhost;dbname=xxxx', ...

  10. django 中的延迟加载技术,python中的lazy技术

    ---恢复内容开始--- 说起lazy_object,首先想到的是django orm中的query_set.fn.Stream这两个类. query_set只在需要数据库中的数据的时候才 产生db ...