# bzoj3745 [COCI2015]Norma

bzoj3745 [COCI2015]Norma

$n\leq5\times10^5,\ a_i\in[0,\ 10^8]$

$mx=\max[p,\ mid],\ mn=\min[p,\ mid]$ 。逆序枚举左端点 $p$ ，令 $x$ 为满足 $mn\ge\min[mid+1,\ x]$ 的最大的 $x$$y$$mx\le\max[mid+1,\ y]$ 的最大的 $y$ 。假设 $x<y$ ，右端点为 $q$

1. $q\leq x$ 时，贡献为 $mx\times mn\times\displaystyle\sum(q-p+1)$
2. $x<q\leq y$ 时，贡献为 $mx\times(\displaystyle\sum((q-p+1)\times\min[x+1,\ q]))$
3. $y<q$ 时，贡献为 $\displaystyle\sum((q-p+1)\times\min[x+1,\ q]\times\max[y+1,\ q])$

$x\ge y$ 的情况同理

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn = 5e5 + 10, P = 1e9, inf = 1e8;
int n, ans, a[maxn], f1[maxn], f2[maxn], g1[maxn], g2[maxn], s1[maxn], s2[maxn];

inline void chkmax(int &x, int y) {
if (x < y) x = y;
}

inline void chkmin(int &x, int y) {
if (x > y) x = y;
}

inline int inc(int x, int y) {
x += y;
return x < P ? x : x - P;
}

inline int dec(int x, int y) {
x -= y;
return x < 0 ? x + P : x;
}

inline int mul(int x, int y) {
return 1ll * x * y % P;
}

inline int add(int &x, int y) {
x += y;
return x < P ? x : x -= P;
}

void cdq(int l, int r) {
if (l == r) {
return;
}
int mid = (l + r) >> 1;
cdq(l, mid), cdq(mid + 1, r);
f1[mid] = f2[mid] = g1[mid] = g2[mid] = s1[mid] = s2[mid] = 0;
for (int i = mid + 1, mx = 0, mn = inf; i <= r; i++) {
chkmax(mx, a[i]), chkmin(mn, a[i]);
f1[i] = inc(f1[i - 1], mx);
g1[i] = inc(g1[i - 1], mn);
f2[i] = inc(f2[i - 1], mul(mx, i));
g2[i] = inc(g2[i - 1], mul(mn, i));
s1[i] = inc(s1[i - 1], mul(mx, mn));
s2[i] = inc(s2[i - 1], mul(mul(mx, mn), i));
}
for (int i = mid, mx = 0, mn = inf, x = mid, y = mid; i >= l; i--) {
chkmax(mx, a[i]), chkmin(mn, a[i]);
while (x < r && a[x] >= mn && a[x + 1] >= mn) x++;
while (y < r && a[y] <= mx && a[y + 1] <= mx) y++;
int p = min(x, y), q = max(x, y);
add(ans, dec(dec(s2[r], s2[q]), mul(i - 1, dec(s1[r], s1[q]))));
add(ans, mul(mul(mx, mn), dec(1ll * (p - mid) * (p + mid + 1) / 2 % P, mul(i - 1, p - mid))));
if (x < y) {
add(ans, mul(mx, dec(dec(g2[y], g2[x]), mul(i - 1, dec(g1[y], g1[x])))));
} else {
add(ans, mul(mn, dec(dec(f2[x], f2[y]), mul(i - 1, dec(f1[x], f1[y])))));
}
}
}

int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
}
cdq(1, n);
printf("%d", ans);
return 0;
}

posted @ 2019-06-08 15:31  cnJuanzhang  阅读(139)  评论(0编辑  收藏  举报