hdu 4729 树链剖分
思路:这个树链剖分其实还是比较明显的。将边按权值排序后插入线段树,然后用线段树查找区间中比某个数小的数和,以及这样的数的个数。当A<=B时,就全部建新的管子。
对于A>B的情况比较 建一条由S->T的管子后将这根管子容量扩到最大能得到的容量 与 将所有预算都用来扩大管子容量不建新管子得到的最大容量 做比较 ,选最大的。
扩容量能得到的最大权值可以同过二分枚举答案,用树链剖分判断。
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pb push_back
#define mp make_pair
#define Maxn 200010
#define Maxm 400010
#define LL __int64
#define Abs(x) ((x)>0?(x):(-x))
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define inf 100000
#define lowbit(x) (x&(-x))
#define clr(x,y) memset(x,y,sizeof(x))
#define Mod 1000000007
using namespace std;
int head[Maxn],vi[Maxn],dep[Maxn],w[Maxn],top[Maxn],son[Maxn],sz[Maxn],fa[Maxn],e,id;
int val[Maxn];
int num[Maxn],cnt;
struct Point{
int val,i;
int operator <(const Point &temp) const{
return val<temp.val;
}
}lis[Maxn];
struct Edge{
int u,v,next;
int val;
}edge[Maxn*];
struct Tree{
int l,r,c;
int *p;
int *q;
int mid(){
return (l+r)>>;
}
}tree[Maxn*];
void init()
{
clr(head,-);clr(vi,);
e=;id=;
}
void add(int u,int v,int val)
{
edge[e].u=u,edge[e].v=v,edge[e].val=val,edge[e].next=head[u],head[u]=e++;
}
void BuildTree(int l,int r,int po)
{
tree[po].l=l,tree[po].r=r,tree[po].c=;
tree[po].p=new int[r-l+];
tree[po].q=new int[r-l+];
tree[po].q[]=;
if(l==r)
return ;
int mid=tree[po].mid();
BuildTree(l,mid,lson(po));
BuildTree(mid+,r,rson(po));
}
void update(int i,int c,int po)
{
if(tree[po].l==tree[po].r){
tree[po].p[++tree[po].c]=c;
tree[po].q[tree[po].c]=tree[po].q[tree[po].c-]+c;
return ;
}
tree[po].p[++tree[po].c]=c;
tree[po].q[tree[po].c]=tree[po].q[tree[po].c-]+c;
int mid=tree[po].mid();
if(i<=mid)
update(i,c,lson(po));
else
update(i,c,rson(po));
}
int getmin(int l,int r,int po)
{
if(l<=tree[po].l&&tree[po].r<=r){
return tree[po].p[];
}
int mid=tree[po].mid();
if(r<=mid)
return getmin(l,r,lson(po));
else if(l>=mid+)
return getmin(l,r,rson(po));
else{
return min(getmin(l,mid,lson(po)),getmin(mid+,r,rson(po)));
}
}
LL getans(int l,int r,LL val,int po)
{
if(l<=tree[po].l&&tree[po].r<=r){
int pos=lower_bound(tree[po].p+,tree[po].p+tree[po].c+,val)-tree[po].p;
if(pos>tree[po].c){
cnt+=tree[po].c;
return tree[po].q[tree[po].c];
}
if(pos>){
cnt+=pos-;
return tree[po].q[pos-];
}
return ;
}
int mid=tree[po].mid();
if(r<=mid)
return getans(l,r,val,lson(po));
else if(l>=mid+)
return getans(l,r,val,rson(po));
else{
return getans(l,mid,val,lson(po))+getans(mid+,r,val,rson(po));
}
}
void dfs(int u,int val)
{
vi[u]=;
int i,v;
son[u]=,sz[u]=;
num[u]=val;
for(i=head[u];i!=-;i=edge[i].next){
v=edge[i].v;
if(vi[v]) continue;
dep[v]=dep[u]+;
fa[v]=u;
dfs(v,edge[i].val);
if(sz[v]>sz[son[u]])son[u]=v;
sz[u]+=sz[v];
}
}
void build(int u,int ti)
{
int i,v;
w[u]=++id;top[u]=ti;vi[u]=;
lis[id].i=id,lis[id].val=num[u];
if(son[u]) build(son[u],ti);
for(i=head[u];i!=-;i=edge[i].next){
v=edge[i].v;
if(vi[v]||v==son[u]) continue;
build(v,v);
}
}
LL need(LL val,int u,int v)
{
int f1=top[u],f2=top[v];
cnt=;
LL ans=;
// cout<<u<<" "<<f1<<" "<<v<<" "<<f2<<" "<<w[f1]<<" "<<w[u]<<endl;
while(f1!=f2){
if(dep[f1]<dep[f2]){
swap(f1,f2),swap(u,v);
}
ans+=getans(w[f1],w[u],val,);
u=fa[f1];f1=top[u];
}
if(dep[u]>dep[v])
swap(u,v);
if(u!=v)
ans+=getans(w[son[u]],w[v],val,);
//cout<<u<<" "<<v<<" "<<val<<" "<<cnt<<" "<<ans<<" "<<val*cnt-ans<<endl;
ans=val*(LL)cnt-ans;
return ans;
}
void calc(int u,int v,LL k,LL a,LL b)
{
int x=u,y=v;
int f1=top[u],f2=top[v];
LL ans=;
while(f1!=f2){
if(dep[f1]<dep[f2]){
swap(f1,f2),swap(u,v);
}
ans=min(ans,(LL)getmin(w[f1],w[u],));
u=fa[f1];f1=top[u];
}
if(dep[u]>dep[v])
swap(u,v);
if(u!=v)
ans=min(ans,(LL)getmin(w[son[u]],w[v],));
LL capacity=;
if(k<min(a,b)){
printf("%I64d\n",ans);
return ;
}
if(a<=b){
printf("%I64d\n",k/a+ans);
return ;
}
if(k>=a)
capacity=(k-a)/b++ans;
LL l,r,mid,temp;
l=,r=k/b+ans+;
while(l+<r){
mid=(l+r)>>;
temp=need(mid,x,y);
temp*=b;
if(temp<=k)
l=mid;
else
r=mid-;
}
if(r>=)
if(need(r,x,y)*b<=k&&r>l)
l=r;
printf("%I64d\n",max(capacity,l));
return ;
}
int main()
{
int t,n,m,i,j,u,v,val,q,a,b,k,Case=;
scanf("%d",&t);
while(t--){
init();
scanf("%d%d",&n,&m);
for(i=;i<n;i++){
scanf("%d%d%d",&u,&v,&val);
add(u,v,val);
add(v,u,val);
}
dfs(,);
memset(vi,,sizeof(vi));
build(,);
sort(lis+,lis+id+);
BuildTree(,n,);
for(i=;i<=n;i++){
update(lis[i].i,lis[i].val,);
}
printf("Case #%d:\n",++Case);
for(i=;i<=m;i++){
scanf("%d%d%d%d%d",&u,&v,&k,&a,&b);
calc(u,v,k,a,b);
}
}
return ;
}
hdu 4729 树链剖分的更多相关文章
- hdu 5893 (树链剖分+合并)
List wants to travel Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/O ...
- hdu 5052 树链剖分
Yaoge’s maximum profit Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- hdu 4897 树链剖分(重轻链)
Little Devil I Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu 5274 树链剖分
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- HDU 3966 (树链剖分+线段树)
Problem Aragorn's Story (HDU 3966) 题目大意 给定一颗树,有点权. 要求支持两种操作,将一条路径上的所有点权值增加或减少ai,询问某点的权值. 解题分析 树链剖分模板 ...
- hdu 3966(树链剖分+线段树区间更新)
传送门:Problem 3966 https://www.cnblogs.com/violet-acmer/p/9711441.html 学习资料: [1]线段树区间更新:https://blog.c ...
- HDU 3966 /// 树链剖分+树状数组
题意: http://acm.hdu.edu.cn/showproblem.php?pid=3966 给一棵树,并给定各个点权的值,然后有3种操作: I x y z : 把x到y的路径上的所有点权值加 ...
- hdu 3966 树链剖分
思路:树链剖分入门题,我这门入得好苦啊,程序很快写出来了,可是在LCA过程中把update函数里的左右边界位置写反了,一直RE到死. #pragma comment(linker, "/ST ...
- HDU 5274(树链剖分)
树链剖分第一题QAQ,纪念下 #pragma comment(linker, "/STACK:102400000,102400000") #include <iostream ...
随机推荐
- 获取某几个分类下的前N条数据 mssql语句
方案1: (SELECT top 10 * FROM 表 where type=3 ) UNION ALL (SELECT top 10 * FROM 表 where type=4 ) ...
- 【css hack】正是我所找的,帮了大忙啊
(从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期2014-03-05) 各个浏览器单独设置属性 IE6:能识别下划线 “_” 和 星号 “*“,不能识别 “!important” ...
- HTTP状态码通常分为5种类型
HTTP状态码通常分为5种类型,分别以1-5五个数字开头,由3位整数组成: -------------------------------------------------------------- ...
- UVA1099----Sharing Chocolate----在集合上的DP
题目地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- windows 32位系统中进程最大可用内存空间为3GB (转)
http://msdn.microsoft.com/zh-cn/library/ms189334.aspx 进程地址空间 所有 32 位应用程序都有 4 GB 的进程地址空间(32 位地址最多可以映射 ...
- 浅谈Androidclient项目框架
写Android也有些时间了,一边工作,一边学习,一边积累.仅仅有遇到问题了,花时间去研究,自己的能力才干提升.刀假设不用.慢慢的就会生锈应该也是这个道理吧!上个月公司项目server框架进行的一些调 ...
- 排版系统Latex傻瓜方式使用(论文排版)
0. 什么是Latex? LaTEX(英语发音:/ˈleɪtɛk/ lay-tek或英语发音:/ˈlɑːtɛk/ lah-tek,音译"拉泰赫").文字形式写作LaTeX.是一种基 ...
- Dojo系列教程
Dojo学习笔记一: 认识Dojo http://blog.csdn.net/lfsfxy9/article/details/8623897 <dojo 边学边用> http://www. ...
- TP复习14
## ThinkPHP 3.1.2 控制器的模块和操作#讲师:赵桐正微博:http://weibo.com/zhaotongzheng 本节课大纲:一.空模块和空操作 1.空操作 function _ ...
- 浏览器中打开IOS应用并传参
原创文章,转载请注明 开发中遇到这么一个问题,就是动态地指定联接服务器地址,或其它数据.如果是其它数据还好说一些,可以通过在服务器上获得的方式来弄.但如果服务器地址都需要动态指定的话.那就得另想办法了 ...