【题目链接】
【思路要点】
- 用李超线段树维护一次函数单点最值,模板题。
- 时间复杂度\(O(NLogN)\)。
【代码】
#include<bits/stdc++.h> using namespace std; const int MAXN = 100005; const int MAXP = 200005; const int MAXL = 15; template <typename T> void chkmax(T &x, T y) {x = max(x, y); } template <typename T> void chkmin(T &x, T y) {x = min(x, y); } template <typename T> void read(T &x) { x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; } template <typename T> void write(T x) { if (x < 0) x = -x, putchar('-'); if (x > 9) write(x / 10); putchar(x % 10 + '0'); } template <typename T> void writeln(T x) { write(x); puts(""); } struct SegmentTree { struct Node { int lc, rc; double k, b; } a[MAXP]; int n, root, size; void build(int &root, int l, int r) { root = ++size; if (l == r) return; int mid = (l + r) / 2; build(a[root].lc, l, mid); build(a[root].rc, mid + 1, r); } void init(int x) { n = x; root = size = 0; build(root, 1, n); } void insert(int root, int l, int r, double k, double b) { if (a[root].k * l + a[root].b >= k * l + b && a[root].k * r + a[root].b >= k * r + b) return; if (a[root].k * l + a[root].b <= k * l + b && a[root].k * r + a[root].b <= k * r + b) { a[root].k = k; a[root].b = b; return; } int mid = (l + r) / 2; double md = (l + r) / 2.0; if (a[root].k * md + a[root].b <= k * md + b) { swap(a[root].k, k); swap(a[root].b, b); } insert(a[root].lc, l, mid, k, b); insert(a[root].rc, mid + 1, r, k, b); } void insert(double k, double b) { insert(root, 1, n, k, b); } double query(int root, int l, int r, int x) { double ans = a[root].k * x + a[root].b; if (l == r) return ans; int mid = (l + r) / 2; if (mid >= x) chkmax(ans, query(a[root].lc, l, mid, x)); else chkmax(ans, query(a[root].rc, mid + 1, r, x)); return ans; } double query(int x) { return query(root, 1, n, x); } } ST; int main() { int n; read(n); ST.init(1e5); for (int i = 1; i <= n; i++) { char s[MAXL]; scanf("\n%s", s); if (s[0] == 'P') { double b, k; scanf("%lf%lf", &b, &k); b -= k; ST.insert(k, b); } else { int x; read(x); int ans = ST.query(x) / 100 + 1e-10; printf("%d\n", ans); } } return 0; }