#include <iostream>
#include <vector>
#include <algorithm>
#define ff first
#define ss second
#define int long long
using namespace std;
struct BinaryIndexedTree {
vector<int> bit;
int n;
void build(int _n) {
n = _n;
bit.assign(n+1, 0);
}
int query(int ql, int qr) {
return query(qr) - query(ql-1);
}
int query(int ql) {
int ans = 0;
for (int i=ql; i; i-=i&-i)
ans += bit[i];
return ans;
}
void modify(int ql, int qx) {
for (int i=ql; i<=n; i+=i&-i)
bit[i] += qx;
}
};
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<long long> h(n);
for (long long i=0; i<n; i++) cin >> h[i];
vector<pair<int, int>> hh(n);
for (int i=0; i<n; i++) hh[i] = {h[i], i};
sort(hh.begin(), hh.end());
vector<long long> a(n);
long long mx = 0;
for (long long i=0; i<n; i++) {
mx = max(mx, h[i]);
a[i] = mx;
}
long long mx2 = 0;
for (long long i=n-1; i>0; i--) {
mx2 = max(mx2, h[i]);
if (mx2 == mx) break;
a[i] = mx2;
}
BinaryIndexedTree BIT;
BIT.build(n);
long long ans = 0;
int l = 0, r = n-1;
long long lh = 0;
for (auto [x, j]:hh) {
while (a[l] < x)
l++;
while (a[r] < x)
r--;
int cnt = BIT.query(l+1, j+1);
int hehe = a[l];
if (cnt) ans += (x-lh) * ((x-1)*cnt+hehe) + (x+lh+1)*(x-lh) / 2 * (2*cnt-1);
cnt = BIT.query(j+1, r+1);
hehe = a[r];
if (cnt) ans += (x-lh) * ((x-1)*cnt+hehe) + (x+lh+1)*(x-lh) / 2 * (2*cnt-1);
BIT.modify(j+1, 1);
lh = x;
// cout << l << " " << j << " " << r << ":" << ans << "\n";
}
cout << ans << "\n";
}
/*
8
3 2 4 5 4 1 2 1
12
5 4 4 4 3 3 3 4 4 4 5 5
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |