zhangxiao666 @ 2024-02-22 22:27:12
#include<bits/stdc++.h>
using namespace std;
const int N = 4e5 + 10;
const int M = 40010;
const int mod1 =1e9;
const int mod2 = 39989;
const double INF = 1e9;
const double eps = 1e-10;
int n, cnt, lastans;
int tr[N];
struct line
{
double k, b;
int lmax, rmax;
void getline(int x0, int y0, int x1, int y1)
{
if(x0 == x1)
{
k = 0;
b = max(y1, y0);
lmax = rmax = x0;
}
else
{
k = (double)(y1 - y0) / (x1 - x0);
b = y1 - k * x1;
lmax = min(x0, x1);
rmax = max(x0, x1);
}
}
double posval(int x)
{
if(lmax <= x && x <= rmax) return k * x + b;
return -INF;
}
}f[N];
int cmp(double a, double b)
{
if(a - b >= eps) return 1;
if(b - a >= eps) return -1;
return 0;
}
void update(int x, int l, int r, int ql, int qr, int id)
{
int mid = l + r >> 1;
if(ql <= l && r <= qr)
{
if(!tr[x]) {tr[x] = id; return;}
if(cmp(f[id].posval(mid), f[tr[x]].posval(mid)) == 1) swap(id, tr[x]);
int cmpl = cmp(f[id].posval(l), f[tr[x]].posval(l));
int cmpr = cmp(f[id].posval(r), f[tr[x]].posval(r));
if(cmpl == 1 || (cmpl == 0 && id < tr[x])) update(x << 1, l, mid, ql, qr, id);
if(cmpr == 1 || (cmpr == 0 && id < tr[x])) update(x << 1 | 1, mid + 1, r, ql, qr, id);
return;
}
if(ql <= mid) update(x << 1, l, mid, ql, qr, id);
if(qr > mid) update(x << 1 | 1, mid + 1, r, ql, qr, id);
}
int query(int x, int l, int r, int pos)
{
if(l == r) return tr[x];
int mid = l + r >> 1, ans = tr[x];
if(pos <= mid)
{
int res = query(x << 1, l, mid, pos);
int cmpx = cmp(f[ans].posval(pos), f[res].posval(pos));
if(cmpx == 1 || (cmpx == 0 && res < tr[x])) ans = res;
}
else
{
int res = query(x << 1 | 1, mid + 1, r, pos);
int cmpx = cmp(f[ans].posval(pos), f[res].posval(pos));
if(cmpx == 1 || (cmpx == 0 && res < tr[x])) ans = res;
}
return ans;
}
int main()
{
std::ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++)
{
int op; cin >> op;
if(op == 0)
{
int k; cin >> k; k = (k + lastans - 1) % mod2;
cout << (lastans = query(1, 1, M, k)) << "\n";
}
else
{
int x0, y0, x1, y1; cin >> x0 >> y0 >> x1 >> y1;
x0 = (x0 + lastans - 1) % mod2 + 1;
x1 = (x1 + lastans - 1) % mod2 + 1;
y0 = (y0 + lastans - 1) % mod1 + 1;
y1 = (y1 + lastans - 1) % mod1 + 1;
f[++cnt].getline(x0, y0, x1, y1);
update(1, 1, M, min(x0, x1), max(x0, x1), cnt);
}
}
return 0;
}