#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define int long long
using namespace std;
const int mod = (int)1e6 + 3;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n), srt(n);
for(int i = 0; i < n; ++i){
cin >> a[i];
srt[i] = a[i];
}
sort(srt.begin(), srt.end());
srt.erase(unique(srt.begin(), srt.end()), srt.end());
int ans = 0;
for(int i = 0; i + 1 < (int)srt.size(); ++i){
vector<int> lmn(n, -1), rmn(n, -1);
set<int> st;
for(int j = 0; j < n; ++j){
auto p = st.lower_bound(a[j] + 1);
if(p != st.end()){
lmn[j] = *p;
}
st.insert(a[j]);
}
st.clear();
for(int j = n - 1; j >= 0; --j){
auto p = st.lower_bound(a[j] + 1);
if(p != st.end()){
rmn[j] = *p;
}
st.insert(a[j]);
}
int fir = -1, lst = -1, cnt = 0;
for(int j = 0; j < n; ++j){
if(lmn[j] != -1 && rmn[j] != -1 && a[j] == srt[i]){
if(fir == -1){
fir = lmn[j] + rmn[j];
}
lst = rmn[j];
++cnt;
a[j] = srt[i + 1];
}
}
if(cnt == 1){
(ans += fir * (srt[i + 1] - srt[i]) % mod) %= mod;
}
else{
(ans += (fir + lst) * (srt[i + 1] - srt[i]) % mod + (srt[i] + srt[i + 1] + 1) * (srt[i + 1] - srt[i]) / 2 % mod * (cnt * 2 - 3) % mod) %= mod;
}
(ans += cnt * (srt[i] + srt[i + 1] - 1) % mod * (srt[i + 1] - srt[i]) / 2 % mod) %= mod;
// cout << fir << ' ' << lst << ' ' << cnt << ' ' << ans << endl;
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |