對於區間[l,r],若詢問的直線與該區間的凸殼有交,則有解。
線上段樹每個區間上維護凸殼,然後查詢時在相應凸殼上二分斜率即可,時間複雜度$O(n\log^2n)$。
#include<cstdio> #define N 262145 typedef long long ll; int T,n,i,c,l[N],r[N],t; struct P{ int x,y; P(){} P(int _x,int _y){x=_x,y=_y;} inline P operator-(P b){return P(x-b.x,y-b.y);} inline ll operator*(P b){return(ll)x*b.y-(ll)y*b.x;} }p[100010],A,B,q[1900000]; void build(int x,int a,int b){ q[l[x]=++t]=p[a]; for(int i=a+1;i<=b+1;q[++t]=p[i++])while(t>l[x]&&(p[i]-q[t])*(q[t]-q[t-1])<=0)t--; r[x]=t; if(a==b)return; int mid=(a+b)>>1; build(x<<1,a,mid),build(x<<1|1,mid+1,b); } inline bool ex(int l,int r){ int mid,t=l++; while(l<=r){ mid=(l+r)>>1; if((q[mid]-A)*(B-q[mid])<(q[mid-1]-A)*(B-q[mid-1]))l=(t=mid)+1;else r=mid-1; } return(q[t]-A)*(B-q[t])<0; } int ask(int x,int a,int b){ if(c<=a){ if(!ex(l[x],r[x]))return 0; if(a==b)return a; } int mid=(a+b)>>1; if(c<=mid){ int t=ask(x<<1,a,mid); if(t)return t; } return ask(x<<1|1,mid+1,b); } 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';} int main(){ for(read(T);T--;puts("0")){ for(read(n),i=1;i<=n;i++)read(p[i].x),read(p[i].y); for(t=0,build(1,1,n-1),i=1;i<n-1;i++)A=p[i],B=p[i+1],c=i+1,printf("%d ",ask(1,1,n-1)); } return 0; }