This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
int mn = 0;
for(int i = 0; i < n; i++){
cin>>h[i];
if(h[i] < h[mn]) mn = 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;
}
}
}
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<<"\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |