思路:这个树链剖分其实还是比较明显的。将边按权值排序后插入线段树,然后用线段树查找区间中比某个数小的数和,以及这样的数的个数。当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 树链剖分的更多相关文章

  1. hdu 5893 (树链剖分+合并)

    List wants to travel Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/O ...

  2. hdu 5052 树链剖分

    Yaoge’s maximum profit Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  3. hdu 4897 树链剖分(重轻链)

    Little Devil I Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  4. hdu 5274 树链剖分

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  5. HDU 3966 (树链剖分+线段树)

    Problem Aragorn's Story (HDU 3966) 题目大意 给定一颗树,有点权. 要求支持两种操作,将一条路径上的所有点权值增加或减少ai,询问某点的权值. 解题分析 树链剖分模板 ...

  6. hdu 3966(树链剖分+线段树区间更新)

    传送门:Problem 3966 https://www.cnblogs.com/violet-acmer/p/9711441.html 学习资料: [1]线段树区间更新:https://blog.c ...

  7. HDU 3966 /// 树链剖分+树状数组

    题意: http://acm.hdu.edu.cn/showproblem.php?pid=3966 给一棵树,并给定各个点权的值,然后有3种操作: I x y z : 把x到y的路径上的所有点权值加 ...

  8. hdu 3966 树链剖分

    思路:树链剖分入门题,我这门入得好苦啊,程序很快写出来了,可是在LCA过程中把update函数里的左右边界位置写反了,一直RE到死. #pragma comment(linker, "/ST ...

  9. HDU 5274(树链剖分)

    树链剖分第一题QAQ,纪念下 #pragma comment(linker, "/STACK:102400000,102400000") #include <iostream ...

随机推荐

  1. 新浪SAE数据库信息wordpress设置(用户&密码&主地址)

    新浪SAE数据库信息wordpress设置(用户&密码&主地址) 此账号仅能在SAE平台上使用,不能从外部连接我们建议开发者使用SaeMysql操作数据库如果您想自己实现数据库相关操作 ...

  2. MES总结:CBF.Common 文件Net下的有类型转换

    MES总结:CBF.Common 文件Net下的有类型转换. using System.Text;using System.Data;using System.ComponentModel;using ...

  3. reactor设计模式

    reactor介绍 reactor的工作模式就像它的名字一样,是一种反射模式,当事件发生时,根据发生的事件调用注册的处理器. Reactor的优点和应用 Reactor最常用于非阻塞的socket 传 ...

  4. css ie7中overflow:hidden失效问题及解决方法

    css兼容ie7: 做页面的时候用负边距居中的时候在IE7下面,父节点中的overflow:hiden失效的问题,查阅了一些资料,总结一下解决方法. 问题原因: 当父元素的直接子元素或者下级子元素的样 ...

  5. 用ICSharpCode.SharpZipLib进行压缩

    今天过中秋节,当地时间(2013-09-08),公司也放假了,正好也闲着没事,就在网上学习学习,找找资料什么的.最近项目上可能会用到压缩的功能,所以自己就先在网上学习了,发现一个不错的用于压缩的DLL ...

  6. Uva 10167 - Birthday Cake 暴力枚举 随机

      Problem G. Birthday Cake Background Lucy and Lily are twins. Today is their birthday. Mother buys ...

  7. Codeforces Round #340 (Div. 2) C. Watering Flowers 暴力

    C. Watering Flowers 题目连接: http://www.codeforces.com/contest/617/problem/C Descriptionww.co A flowerb ...

  8. Codeforces Gym 100015G Guessing Game 差分约束

    Guessing Game 题目连接: http://codeforces.com/gym/100015/attachments Description Jaehyun has two lists o ...

  9. android之多媒体篇(二)

    管理音频焦点 情景:当你的app隐退到后台,而其他也有播放能力的app浮现在前台,这个时候,你可能要暂停你原有app的播放功能,和解除监听Media Button,把控制权交给前台的APP. 这就需要 ...

  10. UICollectionViewController

    UICollectionViewController 目录 概述 UICollectionView UICollectionViewCell 代理方法 详细细节 概述 UICollectionView ...