题解:超哥线段树
先考虑直线L。
首先,假设当前线段树上区间的直线是L2,如果L完全在L2的上面或者L2完全在L的上面
那么把其中一个舍弃,递归结束。
否则求一下两直线交点。为了方便起见,把左端点比较小的记作L1,另一条记作L2。
求一下两条直线的交点。如果它小于等于mid,说明L1在右面的区间上完全覆盖了L2,因此当前区间保存L1,把L2下传到左区间。
否则要把L1下放到右区间。
然后由于某一个点的值不一定是这个点的函数值,因此询问时把所有祖先取max。
注意特判一个点不存在斜率的情况。
代码写的炒鸡难看。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define mod 39989
#define INF 2000000000.0
#define inf -2000000000.0
#define getx(x,mod) ((x+last_ans-1+mod)%mod+1)
#define debug(x) cerr<<#x<<"="<<x
#define sp <<" "
#define ln <<endl
using namespace std;
const double eps=0.0000001;
inline double gabs(double x)
{
return (x>0.0)?x:-x;
}
inline int dcmp(double x)
{
if(gabs(x)<eps) return 0;
else return (x>0)?1:-1;
}
struct line{
double k,b;int id;
line(int _id=0,double _k=0.0,double _b=0.0)
{
id=_id;k=_k,b=_k;
}
line operator=(const line &L)
{
id=L.id;k=L.k;b=L.b;return *this;
}
inline double v(int x)const
{
return k*x+b;
}
};
struct segment{
int l,r;
segment *ch[2];
line L;
}*rt;
inline double getInter(const line &l1,const line &l2)
{
return (l2.b-l1.b)/(l1.k-l2.k);
}
int build(segment* &rt,int l,int r)
{
rt=new segment;rt->l=l,rt->r=r;
rt->ch[0]=rt->ch[1]=NULL;
rt->L.id=0,rt->L.k=0.0,rt->L.b=inf;
if(l==r) return 0;
int mid=(l+r)>>1;
build(rt->ch[0],l,mid);
build(rt->ch[1],mid+1,r);
return 0;
}
int update(segment* &rt,int s,int t,line L)
{
int l=rt->l,r=rt->r,mid=(l+r)>>1;
if(s<=l&&r<=t)
{
// debug(l)sp,debug(r)ln;
// cout<<L.v(l)<<" "<<L.v(r)<<endl;
if(dcmp(L.v(l)-rt->L.v(l))==1) swap(L,rt->L);
// cout<<L.v(l)<<" "<<L.v(r)<<endl;
if(dcmp(L.v(r)-rt->L.v(r))<=0) return 0;
// cout<<L.v(l)<<" "<<L.v(r)<<endl;
double x=getInter(L,rt->L);
if(x>mid) update(rt->ch[1],s,t,L);
else swap(rt->L,L),update(rt->ch[0],s,t,L);
return 0;
}
if(s<=mid) update(rt->ch[0],s,t,L);
if(mid<t) update(rt->ch[1],s,t,L);
return 0;
}
inline line better(const line &l1,const line &l2,int x)
{
if(dcmp(l1.v(x)-l2.v(x))==0)
if(l1.id<l2.id) return l1;
else return l2;
if(dcmp(l1.v(x)-l2.v(x))==1) return l1;
else return l2;
}
line query(segment* &rt,int x)
{
int l=rt->l,r=rt->r;
if(l==r) return rt->L;
int mid=(l+r)>>1;line L;
if(x<=mid) L=query(rt->ch[0],x);
else L=query(rt->ch[1],x);
return better(rt->L,L,x);
}
int main()
{
int n;scanf("%d",&n);
build(rt,1,mod+10);line L;
int last_ans=0,line_cnt=0;
while(n--)
{
int opt;scanf("%d",&opt);
if(!opt)
{
int x;scanf("%d",&x);x=getx(x,mod);
printf("%d\n",last_ans=query(rt,x).id);
}
else{
int a,b,c,d;scanf("%d%d%d%d",&a,&b,&c,&d);
a=getx(a,mod),b=getx(b,1000000000),c=getx(c,mod),d=getx(d,1000000000);
// cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;
L.id=++line_cnt;L.k=0.0,L.b=double(max(b,d));
if(a==c) /*debug(L.id)sp,debug(L.k)ln,debug(L.b)ln,*/update(rt,a,c,L);
else{
if(a>c) swap(a,c),swap(b,d);
// cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;
L.k=((double)d-b)/((double)c-a),L.b=b-a*L.k;
// debug(L.id)sp,debug(L.k)sp,debug(L.b)ln;
update(rt,a,c,L);
}
}
}
}