[HDU 5029] Relief grain
Relief grain
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others)
Total Submission(s): 1559 Accepted Submission(s): 383
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.
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.
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
2
2
3
3
0
2
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}.
这个题大概和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的更多相关文章
- hdu 5029 Relief grain(树链剖分+线段树)
题目链接:hdu 5029 Relief grain 题目大意:给定一棵树,然后每次操作在uv路径上为每一个节点加入一个数w,最后输出每一个节点个数最多的那个数. 解题思路:由于是在树的路径上做操作, ...
- 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 ...
- HDU 5029 Relief grain 树链剖分打标记 线段树区间最大值
Relief grain Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- HDU 5029 Relief grain --树链剖分第一题
题意:给一棵树,每次给两个节点间的所有节点发放第k种东西,问最后每个节点拿到的最多的东西是哪种. 解法:解决树的路径上的修改查询问题一般用到的是树链剖分+线段树,以前不会写,后来学了一下树链剖分,感觉 ...
- 树链剖分+线段树 HDOJ 5029 Relief grain(分配粮食)
题目链接 题意: 分粮食我就当成涂色了.有n个点的一棵树,在a到b的路上都涂上c颜色,颜色可重复叠加,问最后每一个点的最大颜色数量的颜色类型. 思路: 首先这题的输出是每一个点最后的情况,考虑离线做法 ...
- J - Relief grain HDU - 5029
Relief grain Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others)T ...
- 树链剖分处理+线段树解决问题 HDU 5029
http://acm.split.hdu.edu.cn/showproblem.php?pid=5029 题意:n个点的树,m次操作.每次操作输入L,R,V,表示在[L,R]这个区间加上V这个数字.比 ...
- hdu5029 Relief grain
题目链接 树剖+线段树 将区间修改转化为单点修改,因为如果按DFS序进行修改,那么一定会对DFS序更大的点造成影响 #include<iostream> #include<vecto ...
- hdu 5029树链剖分
/* 解:标记区间端点,按深度标记上+下-. 然后用线段树维护求出最小的,再将它映射回来 */ #pragma comment(linker, "/STACK:102400000,10240 ...
随机推荐
- Windows 7 Shortcuts (完整兼具分类有序,想我所想,赞!)
Original Link: http://www.shortcutworld.com/en/win/Windows_7.html Table of Contents: Managing 'Windo ...
- Headfirst设计模式的C++实现——组合模式(Composite)
menu_component.h #ifndef _MENU_COMPONENT_H_ #define _MENU_COMPONENT_H_ #include <string> class ...
- TreeMap 红黑树实现
TreeMap 是一个有序的key-value集合,它是通过 红黑树 实现的. TreeMap 继承于AbstractMap,所以它是一个Map,即一个key-value集合. TreeMap 实现了 ...
- ios中XPath的语法
在XML的读写中,我们知道有Xpath的语法 1.定位节点:(/)代表绝对的路径,代表起始地位置.(//)表示文件中所有符合模式的元素都会被选出来,即使是处于树中不同的层级也会被选出来 2.KissX ...
- svn执行clean up 操作时报错 "Previous operation has not finished; run 'cleanup' if it was interrupted"解决如下!
今天在项目中更新的时候,突然间爆了一个svn的这个错误,当时提示我去clean up操作,结果我执行clean up操作时候,还是报错,后来坚持出来,是因为ios项目中的一个图标出了问题,使svn进入 ...
- 用PHP实现单向链表
供参考,代码还可继续打磨 同时放在了我的github上:https://github.com/hheedat/demo/blob/master/learn_php/58_linked_list.php ...
- Python Socket,How to Create Socket Cilent? - 网络编程实例
文章出自:Python socket – network programming tutorial by Silver Moon 原创译文,如有版权问题请联系删除. Network programin ...
- NSCharacterset
我们在nsstring的分割,查找等操作中,经常会提供两种函数,参数类型分别为NSString 和NSCharacterset,有什么不同呢? NSString 是有序字符串 NSCharacters ...
- python 读写文本文件
本人最近新学python ,用到文本文件的读取,经过一番研究,从网上查找资料,经过测试,总结了一下读取文本文件的方法. 1.在读取文本文件的时无非有两种方法: a.f=open('filename', ...
- C#解压、压缩RAR文件
using System; using System.Collections.Generic; using System.Text; using System.IO; using Microsoft. ...