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 < ...
随机推荐
- oracle体系结构理解
体系结构相关内容每次看遍书,过段时间就忘了..无奈用自己理解的方式记录之. 1.commit与写盘与否没有关系,也就是说修改数据(insert update delete)后并提交数据,这条被修改的数 ...
- 一针见血tomcat
一针见血tomcat 一.Tomcat各组件认知 1 Tomcat架构说明 Tomcat是一个基于JAVA的WEB容器,其实现了JAVA EE中的 Servlet 与 jsp 规范,与Nginx ap ...
- Merge Into 语句代替Insert/Update在Oracle中的应用实战
动机: 想在Oracle中用一条SQL语句直接进行Insert/Update的操作. 说明: 在进行SQL语句编写时,我们经常会遇到大量的同时进行Insert/Update的语句 ,也就是说当存在记录 ...
- python3 互译无线短信接口
#!/usr/local/bin/python#-*- coding:utf-8 -*-import http.clientimport urllibimport random host = &quo ...
- CentOS开机自动运行自己的脚本详解
一.root权限编辑/etc/rc.d/rc.local su cd /etc/rc.d/ vi rc.local 二.在这个文件加上你要执行的脚本,全部内容如下: #!/bin/sh # # Thi ...
- ABAP-加密解密
report zco_test. data:o_encryptor type ref to cl_hard_wired_encryptor, o_cx_encrypt_error type ref t ...
- 【Noip模拟 20161005】运货
问题描述 小ww开了一家快递公司,在nn个城市之间进行货物运输工作,一共雇了mm个快递员. 每个快递员性格很奇特,第ii号快递员只愿意将货物从城市sisi运送到titi(甚至不愿意将货物 从titi运 ...
- C语言实现的反转字符串
这段代码大约是10年前写的了,一直收藏在自己的代码仓库里面,贴出来分享下. 网上也有很多类似的代码,学生们用的比较多,工作中用的很少,权做参考. char* ReverseString(char* s ...
- MySQL的安装流程与入门
MySQl是一种关系型数据库,存放的是文字数据,它是以“表”的形式进行存储的.由于MySQl的实用性和不收费,它在世界上是应用最多的数据库,但是,它不支持大量数据写入.接下来,我将为大家分享一下我学习 ...
- 十、Strategy 策略模式
需求:使用不同的算法解决相同的问题 设计原理: 代码清单: 接口 Strategy public interface Strategy { public abstract Hand nextHand( ...