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. 写入.csv文件

    #include "stdafx.h" #include "WriteCsv.h" CString m_strData;//写入记录的一条数据 CString ...

  2. 392. Is Subsequence

    392. Is Subsequence 水题,先是判断长度,长度t比s小,返回false,然后从左到右扫描t,然后同时扫描s,如果相同,s的index就往后拉一个,如果s的index等于s长度,返回t ...

  3. 学习C++ Primer 的个人理解(三)

    第三章,主要内容是字符串和数组.感觉作者的意图是希望读者可以早一点可以写出简单的小程序,并且可以早点接触迭代器这种思想. 在我看来,这种内容的难度并不大. 对于编程来说,最重要的应该是思想,类似vec ...

  4. mysql cluster 安装配置方案

    mysql cluster (mysql 集群)安装配置方案   一.准备 1.准备服务器 计划建立有5个节点的MySQL CLuster体系,需要用到5台服务器,但是我们做实验时没有这么多机器,可以 ...

  5. linux下面的查找

    locate:     速度快     不是实时的,每天定时执行把结果导入数据库     模糊匹配     updatedb  --手动生成文件数据库,执行时间较长   find:     实时查找 ...

  6. WPF 数据绑定Bingding基础(第四天)

    程序的本质是数据加算法.数据会在存储.逻辑和展示三个层面沟通,在WPF中,展示层和逻辑层的沟通就使用Data Bingding来实现. Binding即“绑定”,如果把Bingding比作数据的桥梁, ...

  7. eclipse:java.lang.OutOfMemoryError: PermGen space 最简单的解决方式

    我使用的工具是STS, Eclipse同理: 打开如下界面: 左则选择项目启动使用的Tomcat-->在右侧面板Tab项中选择" Arguments":在VM argumen ...

  8. HBASE的安装

    HBASE的安装: 安装的软件版本:hbase-0.98.4-hadoop2.tar.gz 下载链接:http://www.apache.org/dist/hbase/hbase-0.98.4/ 1. ...

  9. Sublime Text 3插件之Emmet:HTML/CSS代码快速编写神器

    一.快速编写HTML代码 1.  初始化 HTML文档需要包含一些固定的标签,比如<html>.<head>.<body>等,现在你只需要1秒钟就可以输入这些标签. ...

  10. JavaScript decodeURI() 和 encodeURI() 函数

    定义和用法 decodeURI() 函数可对 encodeURI() 函数编码过的 URI 进行解码. 语法 decodeURI(URIstring) 参数 描述 URIstring 必需.一个字符串 ...