1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| #include<bits/stdc++.h> #define LL long long #define PII pair<double, double> #define x first #define y second using namespace std; const int N = 1e5 + 5, M = 4e4; const double eps = 1e-8; struct Line{ double k, b; }line[N]; int tr[M * 4 + 5]; int cnt, last; double calc(Line x, int d){ return x.b + d * x.k; } int dcmp(double x, double y) { if(fabs(x - y) < eps) return 0; if(x < y) return -1; return 1; } void modify(int root, int l, int r, int st, int ed, int x) { int mid = l + r >> 1; if(st <= l && r <= ed) { if(dcmp(calc(line[x], mid), calc(line[tr[root]], mid)) > 0) swap(tr[root], x); if(dcmp(calc(line[x], l), calc(line[tr[root]], l)) > 0) modify(root << 1, l, mid, st, ed, x); if(dcmp(calc(line[x], r), calc(line[tr[root]], r)) > 0) modify(root << 1 | 1, mid + 1, r, st, ed, x); return ; } if(st <= mid) modify(root << 1, l, mid, st, ed, x); if(ed > mid) modify(root << 1 | 1, mid + 1, r, st, ed, x); } int query(int root, int l, int r, int x) { if(l == r) return tr[root]; int mid = l + r >> 1; if(x <= mid) { int tem = query(root << 1, l, mid, x); return dcmp(calc(line[tem], x), calc(line[tr[root]], x)) > 0 ? tem : tr[root]; } else { int tem = query(root << 1 | 1, mid + 1, r, x); return dcmp(calc(line[tem], x), calc(line[tr[root]], x)) > 0 ? tem : tr[root]; } } int T;
void make(int &x) { x = (x + last - 1) % 39989 + 1; } void ma(int &y) { y = (y + last - 1) % 1000000000 + 1; } void add(int x, int y, int o, int u) { if(x == o) line[++ cnt] = {0, max(y, u)}; else line[++ cnt] = {1.0 * (u - y) / (o - x), y - 1.0 * (u - y) / (o - x) * x}; } int main() { scanf("%d", &T); while(T --) { int op; scanf("%d", &op); if(!op) { int x; scanf("%d", &x); x = (x + last - 1) % 39989 + 1; printf("%d\n", last = query(1, 1, M, x)); } if(op) { int x, y, o, u; scanf("%d%d%d%d", &x, &y, &o, &u); make(x), ma(y), make(o), ma(u); if(x > o) swap(x, o), swap(y, u); add(x, y, o, u); modify(1, 1, M, x, o, cnt); } } return 0; }
|