https://www.luogu.org/problemnew/show/CF1019E

题解

\[ dis=day*a+b \]

\[ b=-day*a+dis \]

然后就变成了斜率优化。

考虑边分治,每次把两边的凸包求出来。

然后再把两边的凸包做闵可夫斯基和求出新的凸包。

最后把分治求的的所有凸包上的点再做一次凸包即可得到答案凸包。

代码

#include<bits/stdc++.h>
#define N 200007
#define M 1000009
using namespace std;
typedef long long ll;
int head[N],tot=1,dp[N],sum,size[N],root,rootval,_rootval,top,n,m,num,cuu[2];
bool vis[N],jin[N<<1];
ll ans[M];
inline ll rd(){ll x=0;char c=getchar();bool f=0;while(!isdigit(c)){if(c=='-')f=1;c=getchar();}while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();}return f?-x:x;
}
struct point{double x,y;inline point operator +(const point &b)const{return point{x+b.x,y+b.y};}inline point operator -(const point &b)const{return point{x-b.x,y-b.y};}inline double operator *(const point &b)const{return x*b.y-y*b.x;}
}cu[2][N],st[N*20];
inline double get_k(point y,point x){return (y.y-x.y)/(y.x-x.x);
}
inline bool cmp(point a,point b){if(a.x!=b.x)return a.x<b.x;return a.y>b.y;
}
struct nd{int to,a,b;};
vector<nd>vec[N];
vector<point>pt,tp[2];
struct edge{int n,to;ll a,b;
}e[N<<1];
inline void add(int u,int v,int a,int b){e[++tot].n=head[u];e[tot].to=v;head[u]=tot;e[tot].a=a;e[tot].b=b;e[++tot].n=head[v];e[tot].to=u;head[v]=tot;e[tot].a=a;e[tot].b=b;
}
inline void get_tb(int tag,int num){sort(cu[tag]+1,cu[tag]+num+1,cmp);tp[tag].clear();tp[tag].push_back(cu[tag][1]);int top=1;for(int i=2;i<=num;++i){if(cu[tag][i].x==tp[tag][top-1].x)continue;while(top>1&&get_k(cu[tag][i],tp[tag][top-2])>get_k(tp[tag][top-1],tp[tag][top-2]))tp[tag].pop_back(),top--;tp[tag].push_back(cu[tag][i]);top++;}
}
inline void merge(){point now=tp[0][0]+tp[1][0];pt.push_back(now);int sm=tp[0].size()+tp[1].size()-2,now1=0,now2=0;for(int i=1;i<=sm;++i){point xx,yy;if(now1+1<tp[0].size())xx=tp[0][now1+1]-tp[0][now1];if(now2+1<tp[1].size())yy=tp[1][now2+1]-tp[1][now2];if(now2+1>=tp[1].size()||((now1+1<tp[0].size())&&xx*yy<0))now=now+xx,now1++;else now=now+yy,now2++;pt.push_back(now);}
}
void dfs1(int u,int fa){int now=0;for(vector<nd>::iterator it=vec[u].begin();it!=vec[u].end();++it){int v=it->to,a=it->a,b=it->b;if(v==fa)continue;if(!now){add(u,v,a,b);now=u;}else{++num;add(now,num,0,0);add(num,v,a,b);now=num;} dfs1(v,u);}
}
void getroot(int u,int fa){size[u]=1;dp[u]=0;for(int i=head[u];i;i=e[i].n)if(e[i].to!=fa&&!jin[i]){int v=e[i].to;getroot(v,u);size[u]+=size[v];if(max(size[v],sum-size[v])<rootval){root=i;rootval=max(size[v],sum-size[v]);_rootval=size[v];}}
}
void getdeep(int u,int fa,int tag,ll suma,ll sumb){bool tg=0;for(int i=head[u];i;i=e[i].n)if(e[i].to!=fa&&!jin[i]){int v=e[i].to;tg=1;getdeep(v,u,tag,suma+e[i].a,sumb+e[i].b);}if(!tg){cu[tag][++cuu[tag]]=point{suma,sumb};}
}
void solve(int rot,int S){int u=e[rot].to,v=e[rot^1].to;jin[rot]=jin[rot^1]=1;int sm1=_rootval,sm2=S-_rootval;cuu[0]=cuu[1]=0;getdeep(u,0,0,e[rot].a,e[rot].b);getdeep(v,0,1,0ll,0ll);get_tb(0,cuu[0]);get_tb(1,cuu[1]);merge();if(sm1!=1){sum=sm1;rootval=num;getroot(u,0);solve(root,sm1);}if(sm2!=1){sum=sm2;rootval=num;getroot(v,0);solve(root,sm2);}
}
int main(){n=rd();m=rd(); if(n==1){for(int i=1;i<=m;++i)printf("0 ");return 0;}   int x,y,a,b;for(int i=1;i<n;++i){x=rd();y=rd();a=rd();b=rd();vec[x].push_back(nd{y,a,b});vec[y].push_back(nd{x,a,b});}num=n;dfs1(1,0);root=0;sum=num;rootval=num;getroot(1,0);solve(root,num);sort(pt.begin(),pt.end(),cmp);for(vector<point>::iterator it=pt.begin();it!=pt.end();++it){point now=*it;if(!top){st[top=1]=now;continue;}if(now.x==st[top].x)continue;while(top>1&&get_k(now,st[top-1])>get_k(st[top],st[top-1]))top--;st[++top]=now;}for(int i=m-1;i>=0;--i){while(top>1&&get_k(st[top],st[top-1])<-i)top--;ans[i]=1ll*i*st[top].x+st[top].y;} for(int i=0;i<m;++i)printf("%I64d ",ans[i]);return 0;
}

转载于:https://www.cnblogs.com/ZH-comld/p/10975982.html

CF1019E Raining season相关推荐

  1. codeforces1019E Raining season 边分治+闵可夫斯基和+凸包

    题目分析 假设你准备把所有"可能"成为最长路径的路径都提取出来,显然是用树分治啦,这题中,边分治比点分治更方便. 边分治教学->here 边分治的套路,第一步将多叉树转为二叉 ...

  2. [学习笔记]闵可夫斯基和

    定义p+q=(p.x+q.x,p.y+q.y),给定两个点集,求{pi+qj}的凸包(凸壳)的问题 以求凸壳为例(凸包可以通过求上下凸壳然后拼凑): 显而易见的结论是: 新凸壳上的点一定是由p和q的凸 ...

  3. Sixth season fifteenth and sixteenth episode,things that could have been......(没有如果)

    [Scene: Central Perk, everyone is there as Rachel enters.] Rachel: Hey, you guys! Guess what? Barry ...

  4. 【构造】CF12E Start of the season(神奇的构造)难度⭐⭐⭐

    CF12E Start of the season 题目描述 在伯兰的足球节开幕式中有一个奇怪的魔术秀.最有经验的魔术师会找一个n×nn×nn×n的矩阵(n为偶数).足球节没有这个魔术秀绝对不会开始. ...

  5. Happy Holidays and Season's Greetings Everyone!

    "Between stimulus and response, there is a space. In that space lies our freedom and power to c ...

  6. 越狱Season 1-Episode 15: By the Skin and the Teeth

    Season 1, Episode 15: By the Skin and the Teeth -Pope: doctor...you can leave. 医生你得离开 -Burrows: It's ...

  7. 越狱Season 1- Episode 16

    Season 1, Episode 16 -Burrows:Don't be. It's not your fault. 不要,不是你的错 -Fernando: Know what I like? 知 ...

  8. 越狱Season 1-Episode 9: Tweener

    Season 1, Episode 9: Tweener - Seth: You have got to help me. 你一定要帮我 -Burrows:You've got to help me. ...

  9. Modern Family Season 1#01

    ** [英语学习]Modern Family-Season 1 ** 01 Why are you guys yelling at us when we are way upstairs?& ...

最新文章

  1. c# 逆转数组元素的排序
  2. 二叉树先序遍历递归算法(图解)
  3. Java 集合系列03之 ArrayList详细介绍(源码解析)和使用示例
  4. 苹果将于 2025 年推出的 Apple Car 长什么样?
  5. apache ab压力测试报错
  6. jq 点击按钮跳转到微信_小程序 web-view组件跳转到小程序页面
  7. Google Reader 使用技巧及OPML订阅源分享
  8. 2011年浙江计算机二级vfp是什么,2011年计算机二级VFP考试基础教程(21)
  9. 如何使用Matlab实现数据的拟合
  10. redis配置文件中常用配置详解
  11. 百度OCR图片内容识别
  12. 初出茅庐的小李第23篇博客之WiFi模块建立TCP通信并获取B站粉丝数据
  13. 从整理看视觉设计(网易云课堂我的学习中心-微专业视觉优化)
  14. python 求特征值特征向量 numpy.linalg.eig()
  15. Rank Scores(分数排序)
  16. Python自学笔记1(think python)
  17. 第21章、系统设定工具(网络与打印机)与硬件侦测
  18. 安装APK 免输入vivo、oppo密码
  19. GoogleEarth--全球卫星地图浏览
  20. 野火A7学习第十次(状态机相关)

热门文章

  1. JVM 1.类的加载、连接、初始化
  2. 【Nginx】epoll事件驱动模块
  3. Android 开发资源
  4. angler前端框架_2019几大主流的前端框架,几款目前最热门的前端框架
  5. 如何找出电脑里的流氓软件_啥拦截软件都挡不住?教你一键揪出乱弹窗的流氓软件...
  6. 电脑无线网络显示红叉_不能播放视频怎么办?小编教你电脑不能播放视频如何解决...
  7. kettle mysql 配置_Kettle数据库配置抽离
  8. 在Oracle中exception关键字,Oracle表字段有Oracle关键字出现异常解决方案
  9. 943602cs 蓝牙驱动 win10_英特尔升级WiFi 和蓝牙驱动,Win10 更新5月版稳了
  10. code vs 把所有行拼接成一行_关于SQL Server将一列的多行内容拼接成一行的问题讨论...