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;
using ll = long long;
const ll mod = 1e6+3;
const ll big = 1e18;
struct l{
ll x;
l(const ll &x):x((x+mod)%mod){}
l(const int &x):x((x+mod)%mod){}
l operator+(const l& o) const{
return l(x+o.x);
}
l operator-(const l& o) const{
return l(x-o.x);
}
l operator*(const l& o) const{
return l(x*o.x);
}
l operator+(const ll& o) const{
return l(x+o);
}
l operator-(const ll& o) const{
return l(x-o);
}
l operator*(const ll& o) const{
return l(x*o);
}
l operator+(const int& o) const{
return l(x+o);
}
l operator-(const int& o) const{
return l(x-o);
}
l operator*(const int& o) const{
return l(x*o);
}
operator ll() const{
return (x%mod+mod)%mod;
}
};
const l rev2 = l(500002);
int main(){
ios::sync_with_stdio(0);cin.tie(0);
ll n;
cin >> n;
vector<ll> a(n);
ll mxa = 0;
for(ll &i : a) cin >> i,mxa = max(mxa,i);
vector<ll> b = a;
//for(ll i = 1;i<=mxa;i++) b.push_back(i);
sort(b.begin(),b.end());
b.erase(unique(b.begin(),b.end()),b.end());
l ans = 0;
vector<ll> li = a;
vector<ll> ri = a;
for(ll i = 1;i<n;i++) li[i] = max(li[i],li[i-1]);
for(ll i = n-2;i>=0;i--) ri[i] = max(ri[i],ri[i+1]);
for(ll j = 0;j<b.size()-1;j++){
l de = b[j+1]-b[j];
vector<ll> v;
for(ll i = 0;i<n;i++){
if (a[i]<=b[j]&&li[i]>b[j]&&ri[i]>b[j]) v.push_back(i);
}
if(v.empty()) continue;
//cout << b[j] << ":";
//for(ll i : v) cout << " " << i;
//cout << "\n";
vector<ll> L(n,big),R(n,big);
for(ll i = 0;i<n;i++) if(a[i]>b[j]) L[i] = R[i] = a[i];
for(ll i = 1;i<n;i++) L[i] = min(L[i],L[i-1]);
for(ll i = n-2;i>=0;i--) R[i] = min(R[i],R[i+1]);
//for(ll i = 0;i<n;i++) cout << L[i] << " \n"[i+1==n];
//for(ll i = 0;i<n;i++) cout << R[i] << " \n"[i+1==n];
if (v.size()==1){
ll i = v[0];
ans = ans+l(L[i]+R[i]+b[j])*de+de*(de-1)*rev2;
}else{
l base = (L[v.front()]+R[v.back()]+min(R[v.front()],L[v.back()]));
l cnt0 = (ll)v.size();
l cnt = (cnt0*2-3);
//cout << base << " " << cnt << " " << de << "\n";
l bj = b[j];
ans = ans+base*de+cnt*(de*bj+de*(de+1)*rev2)+cnt0*(bj*de+de*(de-1)*rev2);
}
}
cout << ans << "\n";
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:58:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for(ll j = 0;j<b.size()-1;j++){
| ~^~~~~~~~~~~
# | 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... |