hdu5029 Relief grain
树剖+线段树
将区间修改转化为单点修改,因为如果按DFS序进行修改,那么一定会对DFS序更大的点造成影响
#include<iostream>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#define re(i,l,r) for(int i=(l);i<=(r);i++)
using namespace std;
template <typename Q>
void inin(Q &ret)
{
ret=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='')f=;ch=getchar();}
while(ch>=''&&ch<='')ret=(ret<<)+(ret<<)+ch-'',ch=getchar();
ret=f?-ret:ret;
}
const int xxx=;
int head[xxx],Next[xxx<<],zhi[xxx<<],n,m,dui[xxx],ans[xxx];
int top[xxx],shen[xxx],fa[xxx],ed,dfn[xxx],son[xxx];
vector<int>in[xxx],out[xxx];
void add(int a,int b)
{
Next[++ed]=head[a],head[a]=ed,zhi[ed]=b;
Next[++ed]=head[b],head[b]=ed,zhi[ed]=a;
}
int dfs(int x)
{
int ret=,Max=,temp;
for(int i=head[x];i;i=Next[i])if(zhi[i]!=fa[x])
{
fa[zhi[i]]=x,shen[zhi[i]]=shen[x]+;
temp=dfs(zhi[i]);
ret+=temp;
if(Max<temp)Max=temp,son[x]=zhi[i];
}
return ret;
}
int tot;
void dfs(int x,int t)
{
if(!x)return ;
top[x]=t;dfn[x]=++tot,dui[tot]=x;
dfs(son[x],t);
for(int i=head[x];i;i=Next[i])if(zhi[i]!=fa[x]&&zhi[i]!=son[x])
dfs(zhi[i],zhi[i]);
}
void change(int x,int y,int z)
{
while(top[x]!=top[y])
if(shen[top[x]]>shen[top[y]])
in[dfn[top[x]]].push_back(z),
out[dfn[x]+].push_back(z),
x=fa[top[x]];
else in[dfn[top[y]]].push_back(z),
out[dfn[y]+].push_back(z),
y=fa[top[y]];
if(shen[x]>shen[y])swap(x,y);
in[dfn[x]].push_back(z),out[dfn[y]+].push_back(z);
}
struct tree
{
int l,r,Max,o;
}t[];
void UP(int k)
{
int p1=k<<,p2=p1|;
if(t[p1].Max>=t[p2].Max)t[k].Max=t[p1].Max,t[k].o=t[p1].o;
else t[k].Max=t[p2].Max,t[k].o=t[p2].o;
}
void build(int k,int l,int r)
{
t[k].l=l,t[k].r=r;t[k].Max=,t[k].o=l;
if(l==r)return ;
int mid=(l+r)>>,p1=k<<,p2=p1|;
build(p1,l,mid),build(p2,mid+,r);
UP(k);
}
void update(int k,int se,int x)
{
if(t[k].l==t[k].r){t[k].Max+=x;return ;}
int mid=(t[k].l+t[k].r)>>,p1=k<<,p2=p1|;
if(se<=mid)update(p1,se,x);
else update(p2,se,x);
UP(k);
}
int main()
{
while(scanf("%d%d",&n,&m),n)
{
tot=;
ed=;memset(head,,sizeof head);
memset(son,,sizeof son);
re(i,,n)
{
int a,b;inin(a),inin(b);
add(a,b);
}
dfs();
dfs(,);int Max=;
// re(i,1,n)
// {
// printf("%d : ",i);
// cout<<"in:";re(j,0,(int)in[i].size()-1)printf("%d ",in[i][j]);cout<<"\n";
// cout<<"out:";re(j,0,(int)out[i].size()-1)printf("%d ",out[i][j]);cout<<"\n";
// }
re(i,,m)
{
int x,y,z;
inin(x),inin(y),inin(z);
change(x,y,z),Max=max(Max,z);
}
build(,,Max+);
re(i,,n)
{
re(j,,(int)in[i].size()-)
update(,in[i][j],);
re(j,,(int)out[i].size()-)
update(,out[i][j],-);
ans[dui[i]]=t[].Max?t[].o:;
}
re(i,,n)printf("%d\n",ans[i]);
re(i,,n+)in[i].clear(),out[i].clear();
}
return ;
}
hdu5029 Relief grain的更多相关文章
- 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(树链剖分+线段树)
题目链接:hdu 5029 Relief grain 题目大意:给定一棵树,然后每次操作在uv路径上为每一个节点加入一个数w,最后输出每一个节点个数最多的那个数. 解题思路:由于是在树的路径上做操作, ...
- 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
Relief grain Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others)T ...
- J - Relief grain HDU - 5029
Relief grain Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others)T ...
- 树链剖分+线段树 HDOJ 5029 Relief grain(分配粮食)
题目链接 题意: 分粮食我就当成涂色了.有n个点的一棵树,在a到b的路上都涂上c颜色,颜色可重复叠加,问最后每一个点的最大颜色数量的颜色类型. 思路: 首先这题的输出是每一个点最后的情况,考虑离线做法 ...
- HDU 5029 Relief grain --树链剖分第一题
题意:给一棵树,每次给两个节点间的所有节点发放第k种东西,问最后每个节点拿到的最多的东西是哪种. 解法:解决树的路径上的修改查询问题一般用到的是树链剖分+线段树,以前不会写,后来学了一下树链剖分,感觉 ...
- 2014 ICPC---Relief grain(树链剖分)
原题链接 Problem Description The soil is cracking up because of the drought and the rabbit kingdom is fa ...
- hdu_5029_relief grain(树链剖分)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5029 题意:给你一个树,然后给你两点,将这两点之间的点涂上颜色,问涂色最多的那个颜色是什么,如果数量相 ...
随机推荐
- Python学习笔记(三)
组合数据类型 5种内置的序列类型:bytearray,bytes,list,str,tuple 元组 元组:固定,有序,索引从0开始,分片,步距语法支持 不能替换或者删除其中的任意数据项,使用list ...
- 扁平数组构建DOM树
interface IOrganizationNode { id: string; code: string; name: string; localName: string; localNameLo ...
- Chrome Google浏览器下载
https://support.google.com/chrome/answer/95346?co=GENIE.Platform%3DDesktop&hl=zh-Hans 下载和安装 G ...
- 1.4 flask request和session
2019-1-4 18:13:57 越努力,越幸运! 还有121天,flask讲完,还有4天,总时长130天 还有13天就讲完了 一月争取看完!!! 永远不要高估自己! 今天学了request和ses ...
- trajan
模板 const int N=10005; struct Edge { int v,next; }edge[5*N]; int dfn[N],low[N]; int stack[N],node[N], ...
- 8 Oracle语句
1.select name from v$datafile; 用sys方式登陆,查询所有表空间存放的物理路径 2.create tablespace DEMO_TBS datafile 'D:/TBS ...
- centOS 安装gitlab-runner
1. curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sud ...
- Tensorflow一些常用基本概念与函数(1)
为了快速的熟悉TensorFlow编程,下面从一段简单的代码开始: import tensorflow as tf #定义‘符号’变量,也称为占位符 a = tf.placeholder(" ...
- django cookies与session
1. cookiies # cookies def login(request): print('COOKIES',request.COOKIES) print('SESSION',request.s ...
- python语法_input
input:与用户的交互,返回用户输入的值 注意:input接受的所有数据都为字符串,即便输入的为数字,依然会被当成字符串