2019.02.26 bzoj4311: 向量(线段树分治+凸包)
传送门
题意:
支持插入一个向量,删去某一个现有的向量,查询现有的所有向量与给出的一个向量的点积的最大值。
思路:
考虑线段树分治。
先对于每个向量处理出其有效时间放到线段树上面,然后考虑查询:对于两个已有的向量(u1,v1)(u_1,v_1)(u1,v1)和(u2,v2)(u_2,v_2)(u2,v2),假设给出的向量为(x0,y0)(x_0,y_0)(x0,y0)u1>u2&&(u1,v1)⋅(x0,y0)>(u2,v2)⋅(x0,y0)u_1>u_2\&\&(u_1,v_1)\cdot(x_0,y_0)>(u_2,v_2)\cdot(x_0,y_0)u1>u2&&(u1,v1)⋅(x0,y0)>(u2,v2)⋅(x0,y0)
那么展开得知:(u1−u2)x0>−(v1−v2)y0⇒−x0y0>v1−v2u1−u2(u_1-u_2)x_0>-(v_1-v_2)y_0\Rightarrow-\frac{x_0}{y_0}>\frac{v_1-v_2}{u_1-u_2}(u1−u2)x0>−(v1−v2)y0⇒−y0x0>u1−u2v1−v2
说明在凸包上,于是对于每个线段树节点维护一个凸包,查询的时候在上面二分即可。
代码:
#include<bits/stdc++.h>
#define ri register int
#define fi first
#define se second
using namespace std;
inline int read(){
int ans=0;
char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
return ans;
}
typedef long long ll;
const int N=200005;
int n,tot1=0,tot2=0,q[N],top;
struct Node{int l,r;ll x,y;}a[N];
ll ans[N];
struct pot{
ll x,y;
pot(ll _x=0,ll _y=0):x(_x),y(_y){}
friend inline pot operator+(const pot&a,const pot&b){return pot(a.x+b.x,a.y+b.y);}
friend inline pot operator-(const pot&a,const pot&b){return pot(a.x-b.x,a.y-b.y);}
friend inline ll operator^(const pot&a,const pot&b){return a.x*b.y-a.y*b.x;}
friend inline ll operator*(const pot&a,const pot&b){return a.x*b.x+a.y*b.y;}
friend inline bool operator<(const pot&a,const pot&b){return a.x==b.x?a.y<b.y:a.x<b.x;}
}b[N];
typedef pair<int,pot> pii;
#define lc (p<<1)
#define rc (p<<1|1)
#define mid (l+r>>1)
vector<pot>upd[N<<2];
inline void update(int p,int l,int r,int ql,int qr,pot v){
if(ql>r||qr<l)return;
if(ql<=l&&r<=qr)return upd[p].push_back(v);
if(qr<=mid)update(lc,l,mid,ql,qr,v);
else if(ql>mid)update(rc,mid+1,r,ql,qr,v);
else update(lc,l,mid,ql,qr,v),update(rc,mid+1,r,ql,qr,v);
}
inline ll max(const ll&a,const ll&b){return a>b?a:b;}
inline ll max(const ll&a,const ll&b,const ll&c){return a>b?(a>c?a:c):(b>c?b:c);}
inline double slope(pot a,pot b){return a.x==b.x?1e9:(double)(a.y-b.y)/(double)(a.x-b.x);}
inline ll query(int p,pot tmp){
ll ret=0;
if(top<=3){
for(ri i=1;i<=top;++i)ret=max(ret,tmp*upd[p][q[i]]);
return ret;
}
double slop=-(double)tmp.x/(double)tmp.y;
if(slop>slope(upd[p][q[2]],upd[p][q[1]]))return tmp*upd[p][q[1]];
if(slop<slope(upd[p][q[top]],upd[p][q[top-1]]))return tmp*upd[p][q[top]];
ret=max(tmp*upd[p][q[1]],tmp*upd[p][q[top]]);
int l=2,r=top,res=1;
while(l<=r){
int Mid=l+r>>1;
if(slop<slope(upd[p][q[Mid]],upd[p][q[Mid-1]]))l=Mid+1,res=Mid;
else r=Mid-1;
}
return tmp*upd[p][q[res]];
}
inline void calc(int p,int l,int r){
if(!upd[p].size())return;
sort(upd[p].begin(),upd[p].end());
q[top=1]=0;
for(ri i=1,up=upd[p].size()-1;i<=up;++i){
while(top>1&&((upd[p][i]-upd[p][q[top-1]])^(upd[p][q[top]]-upd[p][q[top-1]]))<=0)--top;
q[++top]=i;
}
for(ri i=l;i<=r;++i)ans[i]=max(ans[i],query(p,b[i]));
}
inline void solve(int p,int l,int r){
calc(p,l,r);
if(l==r)return;
solve(lc,l,mid),solve(rc,mid+1,r);
}
#undef lc
#undef rc
#undef mid
int main(){
n=read();
for(ri op,x,y,i=1;i<=n;++i){
op=read();
if(op==1)x=read(),y=read(),a[++tot1]=(Node){tot2+1,-1,x,y};
if(op==2)a[read()].r=tot2;
if(op==3)b[++tot2].x=read(),b[tot2].y=read();
}
for(ri i=1;i<=tot1;++i)update(1,1,tot2,a[i].l,~a[i].r?a[i].r:tot2,pot(a[i].x,a[i].y));
solve(1,1,tot2);
for(ri i=1;i<=tot2;++i)cout<<ans[i]<<'\n';
return 0;
}
2019.02.26 bzoj4311: 向量(线段树分治+凸包)的更多相关文章
- 【BZOJ4311】向量(线段树分治,斜率优化)
[BZOJ4311]向量(线段树分治,斜率优化) 题面 BZOJ 题解 先考虑对于给定的向量集,如何求解和当前向量的最大内积. 设当前向量\((x,y)\),有两个不同的向量\((u1,v1),(u2 ...
- [BZOJ4311]向量(凸包+三分+线段树分治)
可以发现答案一定在所有向量终点形成的上凸壳上,于是在上凸壳上三分即可. 对于删除操作,相当于每个向量有一个作用区间,线段树分治即可.$O(n\log^2 n)$ 同时可以发现,当询问按斜率排序后,每个 ...
- BZOJ4311 向量(线段树分治+三分)
由点积的几何意义(即投影)可以发现答案一定在凸壳上,并且投影的变化是一个单峰函数,可以三分.现在需要处理的只有删除操作,线段树分治即可. #include<iostream> #inclu ...
- bzoj4311向量(线段树分治+斜率优化)
第二道线段树分治. 首先设当前向量是(x,y),剩余有两个不同的向量(u1,v1)(u2,v2),假设u1>u2,则移项可得,若(u1,v1)优于(u2,v2),则-x/y>(v1-v2) ...
- 2019.01.13 loj#6515. 贪玩蓝月(线段树分治+01背包)
传送门 题意简述:有一个初始为空的双端队列,每次可以在队首和队尾插入或弹出一个二元组(wi,vi)(w_i,v_i)(wi,vi),支持询问从当前队列中选取若干个元素是的他们的和对 MODMODM ...
- 2019.01.13 bzoj4137: [FJOI2015]火星商店问题(线段树分治+可持久化01trie)
传送门 题意:序列上有nnn个商店,有两种事件会发生: sss商店上进购标价为vvv的一个物品 求编号为[l,r][l,r][l,r]之间的位置买ddd天内新进购的所有物品与一个数xxx异或值的最大值 ...
- CF1442D Sum (动态规划,线段树分治)
( 宋 体 字 看 起 来 真 舒 服 ) _{_{(宋体字看起来真舒服)}} (宋体字看起来真舒服) 题 面 ( 洛 谷 翻 译 ) 题面_{_{(洛谷翻译)}} 题面(洛谷翻译) 给定 n ...
- loj#2312. 「HAOI2017」八纵八横(线性基 线段树分治)
题意 题目链接 Sol 线性基+线段树分治板子题.. 调起来有点自闭.. #include<bits/stdc++.h> #define fi first #define se secon ...
- BZOJ.4184.shallot(线段树分治 线性基)
BZOJ 裸的线段树分治+线性基,就是跑的巨慢_(:з」∠)_ . 不知道他们都写的什么=-= //41652kb 11920ms #include <map> #include < ...
随机推荐
- docker上部署nginx容器80端口自动转443端口
拉去nginx镜像 # docker pull nginx 运行nginx容器config用于拷贝nginx配置文件 # docker run --name nginxconfig -d docker ...
- 八(第二篇)、主体结构元素——nav元素、aside元素
nav元素 nav元素是一个可以用作页面导航的链接组,其中的导航元素链接到其他页面或当前页面的其他部分. 并不是所有的链接组都要被放进nav元素,只需要将主要的.基本的链接组放进nav元素即可. na ...
- SpringMVC - 运行流程图及原理分析
流程示意图: 代码分析图:
- SpringMVC工作原理示意图
上面的是springMVC的工作原理图: 1.客户端发出一个http请求给web服务器,web服务器对http请求进行解析,如果匹配DispatcherServlet的请求映射路径(在web.xml中 ...
- SpringMVC之DispatcherServlet类图
DispatcherServlet类图 在IntelliJ IDEA 中打开其源码,然后右键查看Diagrams,工具会自动生成其类图. 可是直观的看出,DispatcherServlet本质上是Se ...
- gentoo 图像方面的软件
图像方面的软件一般包括:查看图像,屏幕截图,图像修改. 查看图像简单的可以安装 feh,但是 feh 一般作为墙纸来用.稍微好一些的是 gqview. 屏幕截图可以用 screengrab,使用的时候 ...
- 修改java在进程中的映像名
java小程序用java -jar xxx.jar 启动的进程映像名都是java.exe. 如果启动多个小程序就不好区分,导致监控程序无法定位到具体需要守护的小程序上. 解决办法: 在java安装目 ...
- 常用的stm32库函数
//初始化的方式:先定义初始化机构体.再打开时钟使能.在对每一组GPIO口进行初始化. GPIO_InitTypeDef LED_GPIO; RCC_APB2PeriphClockCmd(RCC_AP ...
- centos 7 一键安装gitlab
# cat /etc/redhat-release CentOS release 6.5 (Final) # strings /lib64/libc.so.6 |grep GLIBC_ 首先升级 如果 ...
- C++中绝对值的运算
首先,输入-42333380005结果取出来的绝对值却是616292955. 开始我以为是long型的取值范围有问题,就把long型全部改为long long型的了,结果还是一样,就觉得绝对值这个函数 ...