调的啥啊 题解
E_firework · · 题解
简要题意
求
其中
算法一
我们发现最优的
于是直接对每组
期望得分
算法二
我们发现,当
在上一步处理之后,我们可以保证有
然后直接计算所有的
期望得分
算法三
枚举
期望得分
算法四
当序列中只有不超过
期望得分
算法五
就算有了
我们发现上式可以表示为
这个式子看起来很可做。考虑扫描线从后往前枚举
问题看似解决了,但是还有一个问题,
时间复杂度为
期望得分
std:
#include<bits/stdc++.h>
#define LL long long
#define Maxn 500005
#define Maxn2 1<<20
#define lc i << 1
#define rc i << 1 | 1
#define bmid int mid = (tr[i].l + tr[i].r) >> 1
#define mes(s, x) memset(s, x, sizeof(s))
using namespace std;
inline LL read(){char c;c = getchar();while(!(('0' <= c && c <= '9') || c == '-')) c = getchar();bool flag = 0;if(c == '-'){flag = 1;c = getchar();}LL tot = 0;while('0' <= c && c <= '9'){tot = 10 * tot + c - '0';c = getchar();}return flag ? -tot : tot;}
struct node{
unsigned ls, rs, s, x;
node operator+(const node i)const{
return{
ls + i.ls + s * i.x,
rs + i.rs + x * i.s,
s + i.s,
x + i.x
};
}
void upd(unsigned k){
ls = rs = 0;
s = k;
x = 1;
}
void clear(){
ls = rs = s = x = 0;
}
} tr[Maxn2];
int k;
void upd(int x, unsigned y){
int i;
tr[i = (1 << k) + x - 1].upd(y);
while(i != 1){
i = i >> 1;
tr[i] = tr[lc] + tr[rc];
}
}
node getl(int x){
int i = (1 << k) + x - 1;
node dx = {0, 0, 0, 0};
while(i != 1){
if(i & 1) dx = tr[i ^ 1] + dx;
i >>= 1;
}
return dx;
}
node getr(int x){
int i = (1 << k) + x - 1;
node dx = {0, 0, 0, 0};
while(i != 1){
if(!(i & 1)) dx = dx + tr[i ^ 1];
i >>= 1;
}
return dx;
}
void clear(){
for(int i = 1; i < 1 << (k + 1); i++) tr[i].clear();
}
unsigned a[Maxn], b[Maxn];
struct kid{
unsigned x, id;
bool operator<(const kid i)const{
return x < i.x;
}
} x[Maxn];
int main(){
#ifndef ONLINE_JUDGE
freopen("in", "r", stdin);
freopen("out", "w", stdout);
#endif
unsigned n = read(), A = read(), B = read(), C = read(), ans = 0;
A = min(A, C);
B = min(B, C);
C = min(A + B, C);
for(int i = 1; i <= n; i++){
a[i] = read();
x[i] = {a[i], (unsigned int)i};
}
sort(x + 1, x + n + 1);
for(int i = 1; i <= n; i++) b[x[i].id] = i, ans += ((i - 1) * 1llu * (i - 2) / 2 - (i - 1u) * (n - i)) * x[i].x;
ans *= B;
A -= B;
k = 0;
while(1 << k < n) k++;
node dx;
for(int i = 1; i <= n; i++){
dx = getl(b[i]);
ans -= B * ((dx.x - 1) * 1llu * dx.x / 2 * a[i] - dx.rs);
dx = getr(b[i]);
ans += C * (dx.ls - (dx.x - 1) * 1llu * dx.x / 2 * a[i]);
upd(b[i], a[i]);
}
clear();
for(int i = n; i >= 1; i--){
dx = getl(b[i]);
ans += A * ((dx.x - 1) * 1llu * dx.x / 2 * a[i] - dx.rs);
upd(b[i], a[i]);
}
printf("%u", ans);
return 0;
}