#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0)
#pragma GCC optimize("Ofast")
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define int long long
#define lowbit(x) x&-x
const int maxn = 1e6 + 1;
const int N = 1e6 + 3;
const int INF = 1e18;
int h[maxn], l[maxn], r[maxn], cl[maxn], cr[maxn];
signed main(void){
fastio;
int n;
cin>>n;
for(int i = 0; i < n; i++) cin>>h[i];
for(int i = 0; i < n; i++) r[i] = -1;
for(int i = 0; i < n; i++){
l[i] = r[i] = -1;
for(int j = i - 1; j >= 0; j--){
if(h[j] > h[i]){
l[i] = j;
break;
}
}
for(int j = i + 1; j < n; j++){
if(h[j] >= h[i]){
r[i] = j;
break;
}
}
}
// stack<int> st;
// for(int i = 0; i < n; i++){
// while(!st.empty() && h[st.top()] <= h[i]){
// r[st.top()] = i;
// st.pop();
// }
// if(st.empty()) l[i] = -1;
// else l[i] = st.top();
// st.push(i);
// }
// for(int i = n - 1; i >= 0; i--){
// while(r[i] != -1 && h[r[i]] == h[i]) r[i] = r[r[i]];
// }
int ans = 0;
for(int i = 1; i < n; i++){
if(r[i] == -1 || l[i] == -1) continue;
if(h[r[i]] == h[i]) continue;
int cnt = r[i] - l[i] - 1;
int dif = min(h[r[i]], h[l[i]]) - h[i];
// cout<<i<<" "<<cnt<<" "<<dif<<"\n";
// cout<<h[l[i]]<<" "<<h[r[i]]<<"\n";
ans += dif * (h[l[i]] + h[i] + h[r[i]]) + dif * (dif - 1) / 2;
if(cnt >= 2)
ans += dif * (min(h[l[i]], h[r[i]]) + h[i] * 2) + dif * (dif + 1) - dif;
if(cnt > 2)
ans += (cnt - 2) * (dif * h[i] * 3 + dif * (dif + 1) + dif * (dif - 1) / 2);
// cout<<"ans : "<<ans<<"\n";
}
cout<<ans<<"\n";
return 0;
vector<pll> e;
for(int i = 0; i < n; i++) e.eb(h[i], i);
sort(e.begin(), e.end());
for(auto [hei, id] : e){
// cout<<"cur : "<<id<<" "<<hei<<"\n";
// cout<<l[id]<<" "<<r[id]<<"\n";
if(l[id] == -1 || r[id] == -1) continue;
if(h[r[id]] == h[id]){
cl[r[id]] += cl[id] + cr[id] + 1;
continue;
}
int dif = min(h[l[id]] - h[id], h[r[id]] - h[id]);
// cout<<"dif : "<<dif<<"\n";
// cout<<cl[id]<<" "<<cr[id]<<"\n";
ans += dif * (h[l[id]] + h[id] + h[r[id]]) + dif * (dif - 1) / 2;
// cout<<"tmp : "<<ans<<"\n";
// cout<<dif<<" * "<<min(h[l[id]], h[r[id]])<<" "<<h[id] * 2<<"\n";
ans += (cl[id] + cr[id]) * (dif * (min(h[l[id]], h[r[id]]) + h[id] * 2) + dif * (dif + 1) - dif);
// ans += cr[id] * (dif * (h[r[id]] + h[id] * 2) + dif * (dif + 1) - dif);
if(h[l[id]] < h[r[id]]){
cr[l[id]] += cl[id] + cr[id] + 1;
}
else{
cl[r[id]] += cl[id] + cr[id] + 1;
}
// cout<<"res : "<<ans<<"\n";
}
cout<<ans<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Incorrect |
4 ms |
6492 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Incorrect |
4 ms |
6492 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Incorrect |
4 ms |
6492 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Incorrect |
4 ms |
6492 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Incorrect |
4 ms |
6492 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Incorrect |
4 ms |
6492 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Incorrect |
4 ms |
6492 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |