考虑离线操作,求出每个向量存在的时间区间,用时间线段树来进行分治,在每个节点求出凸壳后,询问时在凸壳上三分答案。时间复杂度$O(nlog^2n)$。
#include<cstdio> #include<algorithm> typedef long long ll; const int N=200010,M=4000000; int n,m,i,op,x,y,g[524300],v[M],nxt[M],ed,t;ll ans[N]; inline void read(int&a){char c;while(!(((c=getchar())>='0')&&(c<='9')));a=c-'0';while(((c=getchar())>='0')&&(c<='9'))(a*=10)+=c-'0';} struct P{int x,y;P(){}P(int _x,int _y){x=_x,y=_y;}}a[N],b[N],c[N],d[N],q[N]; inline bool cmp(const P&a,const P&b){return a.x==b.x?a.y>b.y:a.x<b.x;} void ins(int x,int a,int b,int c,int d,int p){ if(c<=a&&b<=d){v[++ed]=p;nxt[ed]=g[x];g[x]=ed;return;} int mid=(a+b)>>1; if(c<=mid)ins(x<<1,a,mid,c,d,p); if(d>mid)ins(x<<1|1,mid+1,b,c,d,p); } inline void ask(int x){ for(int l=0,r=t;l<=r;){ int len=(r-l)/3,m1=l+len,m2=r-len; ll s1=(ll)c[x].x*q[m1].x+(ll)c[x].y*q[m1].y,s2=(ll)c[x].x*q[m2].x+(ll)c[x].y*q[m2].y; if(s1>s2){ r=m2-1; if(ans[x]<s1)ans[x]=s1; }else{ l=m1+1; if(ans[x]<s2)ans[x]=s2; } } } void dfs(int x,int a,int b){ if(a<b){ int mid=(a+b)>>1; dfs(x<<1,a,mid),dfs(x<<1|1,mid+1,b); } int i,j=0; for(i=g[x];i;i=nxt[i])d[j++]=::a[v[i]]; if(!j)return; std::sort(d,d+j,cmp); for(q[t=0]=d[0],i=1;i<j;i++)if(d[i].x!=d[i-1].x){ while(t&&(ll)(q[t].y-q[t-1].y)*(d[i].x-q[t].x)<=(ll)(d[i].y-q[t].y)*(q[t].x-q[t-1].x))t--; q[++t]=d[i]; } for(i=a;i<=b;i++)if(c[i].x)ask(i); } int main(){ read(n); for(i=1;i<=n;i++){ read(op),read(x); if(op==1)read(y),a[++m]=P(x,y),b[m]=P(i,n); if(op==2)b[x].y=i; if(op==3)read(y),c[i]=P(x,y); } for(i=1;i<=m;i++)ins(1,1,n,b[i].x,b[i].y,i); dfs(1,1,n); for(i=1;i<=n;i++)if(c[i].x)printf("%lld ",ans[i]); return 0; }