雨后的蘑菇题解
wangsiyuan2022 · · 题解
Solution
题目链接。
全场最优解题解
主要记录一下个人的做题路径以及想法。
- 我们不会选取一个地方采两次蘑菇。因为如果我们在一个位置采两次蘑菇,可以只保留最后一次在此处采蘑菇,答案不变。
- 设依次采蘑菇的位置的下标序列为
p ,那么有a_{p_i} \leq a_{p_{i+1}} ,如果不满足这个式子,我们一定可以找到一对下标(i, j) 满足i \lt j ,且a_{p_i} \gt a_{p_j} ,我们交换p_i 和p_j 之后,答案一定不劣。
所以我们可以按照蘑菇地可能的选择顺序排序,也就是
不太一样的做法
接下来我发现了一个结论,假设我们在所有蘑菇地中选
反证:如果最集合不能是上述的方式得到的,那么一定存在转移是删除了
那么其实问题就转化为了每次如何找到这个最优的元素。
显然,第一次的最优解就是
这个复杂度是
考虑用平衡树优化这个过程,有区间加,区间加上自身并查询最大值操作,可以用
标准做法
考虑
先按照
考虑优化这个式子。
根据不太一样的做法中的结论,考虑前
我们可以写出
这里其实已经可以写了,但是因为要维护等差数列,有些难写,而且抢不到最优解。所以一个自然的想法是维护
我们设
这样就只要区间加,单点插入就可以了。
时间复杂度
代码
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <vector>
#include <set>
#include <cstring>
#include <unordered_map>
#include <bitset>
#include <cassert>
#include <random>
using namespace std;
int lastans;
const int maxn = 1 << 20;
char in[maxn],out[maxn],*p1=in,*p2=in,*p3=out;
#define getchar() (p1==p2&&(p2=(p1=in)+fread(in,1,maxn,stdin),p1==p2)?EOF:*p1++)
#define flush() (fwrite(out,1,p3-out,stdout))
#define putchar(x) (p3==out+maxn&&(flush(),p3=out),*p3++=(x))
template<typename type>
void read(type &x)
{
x = 0;
int f = 1;
char c = getchar();
while (c < 48 || c > 57)
{
if (c == '-') f = -1;
c = getchar();
}
while (c >= 48 && c <= 57)
x = x * 10 + c - 48, c = getchar();
x *= f;
x ^= lastans;
}
template<typename type, typename ...T>
void read(type &x, T &...y)
{
read(x), read(y...);
}
template<typename type>
inline void write(type x,bool mode=1)//0为空格,1为换行
{
x < 0 ? x =- x, putchar('-') : 0;
static short Stack[50], top(0);
do Stack[++top]=x%10,x/=10; while(x);
while(top) putchar(Stack[top--]|48);
mode?putchar('\n'):putchar(' ');
}
#define ll long long
#define pll pair<long long, long long>
mt19937 ci(time(0));
const int N = 1000005;
const int INF = 0x3f3f3f3f;
int n, idx, rt;
pll p[N];
struct Node {
int ls, rs, sz, fix;
ll val, tag;
}t[N];
int create(ll v) {
t[++idx] = {0, 0, 1, (int)ci(), v, 0};
return idx;
}
void pushup(int u) {
t[u].sz = t[t[u].ls].sz + t[t[u].rs].sz + 1;
}
void oper(int u, ll x) {
t[u].val += x;
t[u].tag += x;
}
void pushdown(int u) {
if (t[u].tag == 0) return;
if (t[u].ls) oper(t[u].ls, t[u].tag);
if (t[u].rs) oper(t[u].rs, t[u].tag);
t[u].tag = 0;
}
void split(int u, int k, int &x, int &y) {
if (!u) {
x = y = 0;
return;
}
pushdown(u);
if (t[t[u].ls].sz < k) {
x = u;
split(t[u].rs, k - t[t[u].ls].sz - 1, t[x].rs, y);
} else {
y = u;
split(t[u].ls, k, x, t[y].ls);
}
pushup(u);
}
int merge(int x, int y) {
if (!x || !y) return x | y;
if (t[x].fix < t[y].fix) {
pushdown(x);
t[x].rs = merge(t[x].rs, y);
pushup(x);
return x;
} else {
pushdown(y);
t[y].ls = merge(x, t[y].ls);
pushup(y);
return y;
}
}
void binary(int u, pll now, int &x, int &y) {
if (!u) {
x = y = 0;
return;
}
pushdown(u);
if (t[u].val <= t[t[u].ls].sz * now.first + now.second) {
y = u;
binary(t[u].ls, now, x, t[y].ls);
} else {
x = u;
now.second += now.first * (t[t[u].ls].sz + 1);
binary(t[u].rs, now, t[x].rs, y);
}
pushup(u);
}
ll output(int u, ll sum) {
pushdown(u);
ll add = 0;
if (t[u].ls) add += output(t[u].ls, sum);
add += t[u].val;
write(sum + add);
if (t[u].rs) add += output(t[u].rs, sum + add);
return add;
}
int main() {
read(n);
for (int i = 1; i <= n; i++) {
read(p[i].first, p[i].second);
}
sort(p + 1, p + 1 + n);
for (int i = 1; i <= n; i++) {
int x, y;
binary(rt, p[i], x, y);
ll v = t[x].sz * p[i].first + p[i].second;
oper(y, p[i].first);
rt = merge(merge(x, create(v)), y);
}
output(rt, 0);
flush();
return 0;
}