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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| #include <bits/stdc++.h> #define ls k << 1 #define rs k << 1 | 1 #define lson k << 1, l, mid #define rson k << 1 | 1, mid + 1, r using namespace std;
const double eps = 1e-8; const int MOD1 = 39989; const int MOD2 = 1000000000; const int MAX = 4e4 + 5; struct Line { int id; double k, b; Line() {} Line(int _id, double _k, double _b) : id(_id), k(_k), b(_b) {} }; struct Node { bool have; Line line; }tree[MAX << 2]; int n;
double calc(Line l, int x) { return l.k * x + l.b; }
void build(int k, int l, int r) { tree[k].have = false; if (l == r) return; int mid = (l + r) >> 1; build(lson); build(rson); }
void modify(int k, int l, int r, int L, int R, Line x) { if (r < L || l > R) return; if (L <= l && r <= R) { if (!tree[k].have) { tree[k].line = x; tree[k].have = true; } else if (calc(x, l) > calc(tree[k].line, l) && calc(x, r) > calc(tree[k].line, r)) { tree[k].line = x; } else if (calc(x, l) > calc(tree[k].line, l) || calc(x, r) > calc(tree[k].line, r)) { int mid = (l + r) >> 1; if (calc(x, mid) > calc(tree[k].line, mid)) swap(tree[k].line, x); if (calc(x, l) > calc(tree[k].line, l)) modify(lson, L, R, x); else modify(rson, L, R, x); } return; } int mid = (l + r) >> 1; modify(lson, L, R, x); modify(rson, L, R, x); }
Line query(int k, int l, int r, int x) { if (l == r) return tree[k].line; Line ans = tree[k].line, nw; int mid = (l + r) >> 1; if (x <= mid) nw = query(lson, x); else nw = query(rson, x); if (nw.id) { if (!ans.id) ans = nw; else if (calc(nw, x) > calc(ans, x)) swap(ans, nw); else if (fabs(calc(nw, x) - calc(ans, x)) <= eps && nw.id < ans.id) { swap(ans, nw); } } return ans; }
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n; build(1, 1, 40000); int lastans = 0, id = 0; while (n--) { int op; cin >> op; if (op == 0) { int k; cin >> k; int x = (k + lastans - 1) % MOD1 + 1; Line ans = query(1, 1, 40000, x); lastans = ans.id; cout << ans.id << "\n"; } else { int x, y, xx, yy; cin >> x >> y >> xx >> yy; x = (x + lastans - 1) % MOD1 + 1; xx = (xx + lastans - 1) % MOD1 + 1; y = (y + lastans - 1) % MOD2 + 1; yy = (yy + lastans - 1) % MOD2 + 1; double k, b; if (x != xx) { if (x > xx) { swap(x, xx); swap(y, yy); } k = (double)(yy - y) / (xx - x); b = (double)y - k * x; } else { k = 0; b = max(yy, y); } modify(1, 1, 40000, x, xx, Line(++id, k, b)); } } return 0; }
|