【BZOJ1568】【JSOI2008】Blue Mary开公司

【题目链接】

【思路要点】

  • 用李超线段树维护一次函数单点最值,模板题。
  • 时间复杂度\(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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值