CF1019E Raining season
https://www.luogu.org/problemnew/show/CF1019E
题解
\]
\]
然后就变成了斜率优化。
考虑边分治,每次把两边的凸包求出来。
然后再把两边的凸包做闵可夫斯基和求出新的凸包。
最后把分治求的的所有凸包上的点再做一次凸包即可得到答案凸包。
代码
#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;
}
CF1019E Raining season的更多相关文章
- Codeforces Round #503 (by SIS, Div. 1)E. Raining season
题意:给一棵树每条边有a,b两个值,给你一个m,表示从0到m-1,假设当前为i,那么每条边的权值是a*i+b,求该树任意两点的最大权值 题解:首先我们需要维护出(a,b)的凸壳,对于每个i在上面三分即 ...
- ural 1250. Sea Burial
1250. Sea Burial Time limit: 1.0 secondMemory limit: 64 MB There is Archipelago in the middle of a s ...
- 越狱Season 1- Episode 22: Flight
Season 1, Episode 22: Flight -Franklin: You know you got a couple of foxes in your henhouse, right? ...
- 越狱Season 1-Episode 21: Go
Season 1, Episode 21: Go -Michael: I need you to let me get us out of here. 我需要你帮我出去 -Patoshik: If y ...
- 越狱Season 1-Episode 20: Tonight
Season 1, Episode 20: Tonight -Pope: I want him under 24hour surveillance. surveillance: 监视 保证24小时监视 ...
- 越狱Season 1-Episode 19: The Key
Season 1, Episode 19: The Key -Kellerman: WeusedtohaveaGreatDane, Dane: 丹麦大狗 我们以前有一只大丹犬 bigandwild. ...
- 越狱Season 1- Episode 18: Bluff
Season 1, Episode 18: Bluff -Michael: Scofield Scofield Michael Scofield Michael Scofield -Patoshik: ...
- 越狱Season 1-Episode 17: J-Cat
Season 1, Episode 17: J-Cat -Pope: Hey, that's looking good. 嗨,看起来真棒 You're making some real progres ...
- 越狱Season 1- Episode 16
Season 1, Episode 16 -Burrows:Don't be. It's not your fault. 不要,不是你的错 -Fernando: Know what I like? 知 ...
随机推荐
- linux之网卡绑定
1 什么是网卡绑定 将多块网卡绑定同一IP地址对外提供服务,可以实现高可用或者负载均衡.直接给两块网卡设置同一IP地址是不可以的.通过bonding,虚拟一块网卡对外提供连接,物理网卡的被修改为相同的 ...
- adb,aapt等命令使用
adb install/uninstall:安装/卸载手机中的应用. devices:查看当前连接到电脑中的设备. adb shell 首先运行adb ...
- SQL查询结果列拼接成逗号分隔的字符串:group_concat
转自:SQL查询结果列拼接成逗号分隔的字符串 背景:做SQL查询时会经常需要,把查询的结果拼接成一个字符串. 解决方法: 通过 group_concat 函数 拼接的结果很长,导致拼接结果显示不全,可 ...
- ubuntu 新建root用户
1. sudo passwd :设置root用户密码 2. 切换用户 方式一:su 方式二: su root 3. 新增普通用户
- 数位DP 计划
通常的数位dp可以写成如下形式: [cpp] view plain copy int dfs(int i, int s, bool e) { if (i==-1) return s==target_s ...
- [LeetCode] 107. 二叉树的层次遍历 II
题目链接 : https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/ 题目描述: 给定一个二叉树,返回其节点值自底 ...
- TCP/IP四层协议
1.数据链路层 数据链路层实现了网卡接口的网络驱动程序,处理数据在物理媒介(以太网,令牌环)上的传输,常用协议包含ARP(地址解析协议),RARP(逆地址解析协议)两个协议,他们实现了IP地址和物理地 ...
- 纯手写实现ajax分页功能
前言 最近用到了这个功能,百度大半天,网上的不是有各种问题就是需要引入其他的插件,无奈,只能自己写,大致功能已经完成.前端页面用bootstrap做样式,分页功能用ajax实现,没用其他插件哦,只引入 ...
- SQL常见面试题-行列互换
有一个SQL题在面试中出现的概率极高,最近有学生出去面试仍然会遇到这样的题目,在这里跟大家分享一下. 题目:数据库中有一张如下所示的表,表名为sales. 年 季度 销售量 1991 1 11 1 ...
- ie10兼容问题 -- 将div定位absolute在图片img上面,导致div点击事件无效
ie10兼容问题: 将div定位absolute在图片img上面,发现div若不加背景色,导致div点击事件(任何事件)无效. <div class="paper-box"& ...