球球辣,真的调了好久qmq
#include<bits/stdc++.h>
#define int long long
#define lb long double
#define ls id<<1
#define rs id<<1|1
using namespace std;
const int M=2e5+110;
const int Mod0=39989;
const int Mod1=1e9;
const double chr=1e-9;
int max_r[M];
bool vis[M];
inline int read(){
int sum=0,k=1;char c=getchar();
while(c>'9'||c<'0'){if(c=='-')k=-1;c=getchar();
}while(c<='9'&&c>='0'){sum=sum*10+c-48;c=getchar();
}return sum*k;
}
int t[M],Tot=0;
struct Line_Node{
lb k,b;
}a[M];
inline Line_Node Get(int x0,int y0,int x1,int y1){
Line_Node ch;
if(x0==x1) {
ch.k=0;
ch.b=max(y1,y0);
}else{
// cout<<y0-y1<<" "<<x0-x1<<endl;
lb k=(lb)(y0-y1)/(x0-x1);
// cout<<k<<endl;
lb b=(lb)(y0-k*x0);
ch.b=b,ch.k=k;
}
return ch;
}
inline lb Get_y(int id,int x){
return a[id].k*x+a[id].b;
}
inline void update(int id,int l,int r,int x,int y,int ne){
// cout<<" id="<<id<<" l="<<l<<" r="<<r<<" x="<<x<<" y="<<y<<" ne="<<ne<<" t[id]="<<t[id]<<endl;
if(l==r){
if(Get_y(ne,l)>Get_y(t[id],l)) t[id]=ne;
return ;
}
if(t[id]==0)
t[id]=ne;
int mid=(l+r)>>1;
if(l>=x&&r<=y){
int ql=Get_y(t[id],l);
int qr=Get_y(t[id],r);
int nl=Get_y(ne,l);
int nr=Get_y(ne,r);
if(nl>ql&&nr>qr){
t[id]=ne;
return ;
}
if(nl<ql&&nr<qr)
return ;
int qk=a[t[id]].k;
int nk=a[ne].k;
int qmid=Get_y(t[id],mid);
int nmid=Get_y(ne,mid);
if(qk>nk){
if(nmid>qmid){
update(ls,l,mid,x,y,t[id]);
t[id]=ne;
}
else update(rs,mid+1,r,x,y,ne);
}
else{
if(nmid>qmid){
update(rs,mid+1,r,x,y,t[id]);
t[id]=ne;
}
else update(ls,l,mid,x,y,ne);
}
return ;
}
if(x<=mid) update(ls,l,mid,x,y,ne);
if(mid<y) update(rs,mid+1,r,x,y,ne);
}
inline int Get_Ans(int id1,int id2,int x){
int y1=Get_y(id1,x),y2=Get_y(id2,x);
if(y1==y2) return min(id1,id2);
if(y1>y2) return id1;
else return id2;
}
inline int Ask(int id,int l,int r,int x){
// cout<<" id="<<id<<" l="<<l<<" r="<<r<<" x="<<x<<" t[id]="<<t[id]<<endl;
if(l==r) return t[id];
int mid=(l+r)>>1;
if(mid>=x) return Get_Ans(t[id],Ask(ls,l,mid,x),x);
else return Get_Ans(t[id],Ask(rs,mid+1,r,x),x);
}
int n=0,lastans=0;
signed main(){
n=read();
for(int i=1;i<=n;i++){
int op=read();
if(op==0){
int k=read();
k=(k+lastans-1+Mod0)%Mod0+1;
if(!vis[k]) lastans=0;
else lastans=Ask(1,1,Mod0,k);
printf("%lld\n",lastans);
}else{
int x0=read(),y0=read(),x1=read(),y1=read();
x0=(x0+lastans-1+Mod0)%Mod0+1;
x1=(x1+lastans-1+Mod0)%Mod0+1;
y0=(y0+lastans-1+Mod1)%Mod1+1;
y1=(y1+lastans-1+Mod1)%Mod1+1;
if(x0>x1){
swap(x0,x1),swap(y0,y1);
}
for(int i=x0;i<=x1;i++)
if(vis[i]) i=max_r[i];
else vis[i]=1,max_r[i]=x1;
a[++Tot]=Get(x0,y0,x1,y1);
// cout<<"k="<<a[Tot].k<<" b="<<a[Tot].b<<endl;
update(1,1,Mod0,x0,x1,Tot);
}
}
return 0;
}
/*
3
1 2 1 1 1
1 1 2 2 1
0 2
*/