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. nodejs的cs模式聊天客户端和服务器实现

    学习完nodejs的基础后,自然要写点东西练练手,以下是一个基于nodejs的cs模式的聊天软件代码: net模块是nodejs的网络编程必定用到的一个模块,对socket通信进行了封装 实现的功能: ...

  2. HDOJ 1042 N! -- 大数运算

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1042 Problem Description Given an integer N(0 ≤ N ≤ 1 ...

  3. 第36条:坚持使用Override注解

    @Override 注解只能用在方法声明中,表示被注解的方法声明覆盖了超类型中的一个声明. @Target(ElementType.METHOD) @Retention(RetentionPolicy ...

  4. Linux C 程序 函数,数组,指针,gdb调试器(SEVEN)

    函数,数组,指针,gdb调试器 1.函数定义 如果明确指定返回类型,默认为int 参数传递:实参对形参的参数传递是单向的,实参只是把自己的值赋给形参.                      形参的 ...

  5. 用EPPlus导入导出数据到excel

    项目上中要用到将数据库中所有表导出为Excel,以及将Excel数据导入数据库中的操作,使用EPPlus组件,编写以下两个函数. using OfficeOpenXml;using OfficeOpe ...

  6. 添加数据时候获取自增的ID

    create database dbDemo go use dbDemo go create table tdstudent { id int primary key identity(1,1), n ...

  7. php设计模式-------(1)策略模式

    一.为什么我要学习设计模式. 我的上一个项目是做App接口,由于时间紧,老板催的急,所以到最后项目完工时发现居然写了几万行代码,可想而知代码质量有多糟糕.而且很多时候,调用接口的开发人员来找我说某个接 ...

  8. DELPHI关于文件的操作

    caption:= ExtractFileExt(‘带路径,带扩展名的文件名’);//返回的就是扩展名 caption:=  ChangeFileExt(ExtractFileName(‘带路径,带扩 ...

  9. Delphi调用一个外部程序时,如何把外部程序的窗体放在主程序窗体的Panel上?

    uses shellapi; ... procedure TForm1.Button2Click(Sender: TObject); var vh: HWND; begin ShellExecute( ...

  10. puppet 部署 horizon server 所需的参数和部署逻辑

    所需要的参数:   $secret_key,   $bind_address = '127.0.0.1',   $cache_server_ip = '127.0.0.1',   $cache_ser ...