# Bzoj4194 mat

## 单调队列维护半平面交

Posted by yjjr's blog on March 17, 2018

# 题目

## Sample Input

2 3
1 2
-1 1
-2 1


## Sample Output

0
-2


# code

#include<bits/stdc++.h>
#define rep(i,a,b) for(register int i=a;i<=b;i++)
#define dep(i,a,b) for(register int i=a;i>=b;i--)
#define ll long long
#define mem(x,num) memset(x,num,sizeof x)
#define reg(x) for(int i=last[x];i;i=e[i].next)
using namespace std;
ll f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
#define mid ((l+r)>>1)
const int maxn=2e5+6;
const double eps=1e-10,inf=1e18;
int n,m,que[maxn];ll c[maxn],top,ans;
struct node{ll k,b;}y[maxn];
inline bool cmpy(node x,node y){return x.k==y.k?x.b<y.b:x.k<y.k;}
inline double slope(int a,int b){
if(y[b].k-y[a].k<eps)return inf*(y[b].b-y[a].b);
return 1.0*(y[b].b-y[a].b)/(y[b].k-y[a].k);
}
inline ll query(double k,int i){
int l=1,r=top;
if(!top)return(ll)maxn;
while(1){
double lk=slope(que[mid-1],que[mid]),rk=slope(que[mid],que[mid+1]);
if(lk<=eps+k&&rk+eps>=k)return y[que[mid]].b+1ll*i*y[que[mid]].k+c[i];
if(k>rk)l=mid+1;else r=mid-1;
}
}
int main()
{