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>
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define MASK(i) (1LL << (i))
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define BIT(x , i) ((x >> (i)) & 1)
#define TASK "task"
#define sz(s) (int) (s).size()
using namespace std;
const int mxN = 1e6 + 227;
const int inf = 1e9 + 277;
const int mod = 1e6 + 3;
const ll infll = 1e18 + 7;
const int base = 307;
const int LOG = 20;
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
int n;
int a[mxN];
int b[mxN];
bool vis[mxN];
pair<int, int> seg[mxN];
int getsum(int l, int r) {
if(l > r) return 0;
return (1LL * (r + l) * (r - l + 1) / 2) % mod;
}
void add(int &a, int b) {
a += b;
if(a >= mod)
a -= mod;
}
void solve()
{
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
int mx = *max_element(a + 1, a + n + 1);
int p = -1;
for(int i = 1; i <= n; i++) if(a[i] == mx) p = i;
int curmax = -1;
for(int i = 1; i <= p; i++) {
maximize(curmax, a[i]);
b[i] = curmax;
}
curmax = -1;
for(int i = n; i >= p; i--) {
maximize(curmax, a[i]);
b[i] = curmax;
}
int res = 0;
int timer = 0;
while(true) {
++timer;
bool ok = true;
for(int i = 1; i <= n; i++) if(a[i] != b[i]) ok = false;
if(ok == true) break;
// for(int i = 1; i <= n; i++) cout << a[i] << ' ';
// cout << endl;
int minval = inf;
for(int i = 1; i <= n; i++) {
if(a[i] != b[i]) {
minval = min(minval, a[i]);
}
}
vector<int> pos;
for(int i = 1; i <= n; i++) if(a[i] != b[i]) {
if(a[i] == minval) pos.pb(i);
}
// for(auto it : pos) cout << it << ' ';
// cout << endl;
if(sz(pos) > 1) {
int p1 = pos[0];
int p2 = pos.back();
int l1 = inf;
int r1 = inf;
for(int i = 1; i < p1; i++) if(a[i] > a[p1]) {
minimize(l1, a[i]);
}
for(int i = p1 + 1; i <= n; i++) if(a[i] > a[p1]) {
minimize(r1, a[i]);
}
int l2 = inf;
int r2 = inf;
for(int i = 1; i < p2; i++) if(a[i] > a[p2]) {
minimize(l2, a[i]);
}
for(int i = p2 + 1; i <= n; i++) if(a[i] > a[p2]) {
minimize(r2, a[i]);
}
// cout << l1 << ' ' << r1 << ' ' << l2 << ' ' << r2 << endl;
int minval = min({l1, r1, l2, r2});
if(r1 <= l2) {
add(res, getsum(a[p1], minval - 1));
add(res, 1LL * l1 * (minval - a[p1]) % mod);
add(res, 1LL * r1 * (minval - a[p1]) % mod);
add(res, getsum(a[p2], minval - 1));
add(res, getsum(a[p2] + 1, minval));
add(res, 1LL * r2 * (minval - a[p2]) % mod);
}
else {
add(res, getsum(a[p2], minval - 1));
add(res, 1LL * l2 * (minval - a[p2]) % mod);
add(res, 1LL * r2 * (minval - a[p2]) % mod);
// cout << res << endl;
add(res, getsum(a[p1], minval - 1));
add(res, 1LL * l1 * (minval - a[p1]) % mod);
add(res, getsum(a[p1] + 1, minval));
}
for(int i = 1; i + 1 < sz(pos); i++) {
int x = pos[i];
add(res, 2LL * getsum(a[x] + 1, minval) % mod);
add(res, getsum(a[x], minval - 1));
}
for(auto it : pos) a[it] = minval;
}
else {
int p1 = pos[0];
int l1 = inf;
int r1 = inf;
for(int i = 1; i < p1; i++) if(a[i] > a[p1]) {
minimize(l1, a[i]);
}
for(int i = p1 + 1; i <= n; i++) if(a[i] > a[p1]) {
minimize(r1, a[i]);
}
int minval = min(l1, r1);
add(res, getsum(a[p1], minval - 1));
add(res, 1LL * l1 * (minval - a[p1]) % mod);
add(res, 1LL * r1 * (minval - a[p1]) % mod);
for(auto it : pos) a[it] = minval;
}
}
cout << res;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tc = 1;
//cin >> tc;
while(tc--) {
solve();
}
return 0;
}
# | 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... |