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;
}
struct Segtree{
int n;
vector<int> t;
Segtree(){}
Segtree(int _n) {
n = _n;
int offset = 1;
while(offset <= n) offset *= 2;
t.resize(offset * 2 + 7, inf);
}
void update(int v, int tl, int tr, int pos, int val) {
if(tl == tr) {
t[v] = val;
}
else {
int tm = (tl + tr) / 2;
if(pos <= tm) update(v * 2, tl, tm, pos, val);
else update(v * 2 + 1, tm + 1, tr, pos, val);
t[v] = min(t[v * 2], t[v * 2 + 1]);
}
}
int getmin(int v, int tl, int tr, int l, int r) {
if(l > r) return inf;
if(tl == l && tr == r) return t[v];
int tm = (tl + tr) / 2;
int m1 = getmin(v * 2, tl, tm, l, min(r, tm));
int m2 = getmin(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
return min(m1, m2);
}
void update(int pos, int val) {
update(1, 1, n, pos, val);
}
int getmin(int l, int r) {
return getmin(1, 1, n, l, r);
}
} seg;
int n;
int a[mxN];
int b[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];
vector<pair<int, int>> order;
for(int i = 1; i <= n; i++) order.pb({a[i], i});
sort(all(order));
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;
}
vector<pair<int, int>> order2;
for(int i = 1; i <= n; i++) if(a[i] != b[i]) {
order2.pb({b[i], i});
}
sort(all(order2));
seg = Segtree(n);
for(int i = 1; i <= n; i++) seg.update(i, a[i]);
int res = 0;
int j = 0;
p = 0;
set<int> s;
while(p < sz(order)) {
int nxt = p;
while(nxt < sz(order) && order[p].fi == order[nxt].fi) {
seg.update(order[nxt].se, inf);
if(a[order[nxt].se] != b[order[nxt].se]) s.insert(order[nxt].se);
++nxt;
}
int curval = order[p].fi;
p = nxt;
// for(auto it : s) cout << it << ' ';
// cout << endl;
while(j < sz(order2) && order2[j].fi == curval) {
s.erase(s.find(order2[j].se));
++j;
}
if(sz(s) == 0) continue;
if(sz(s) > 1) {
int p1 = *s.begin();
int p2 = *--s.end();
int l1 = seg.getmin(1, p1 - 1);
int r1 = seg.getmin(p1 + 1, n);
int l2 = seg.getmin(1, p2 - 1);
int r2 = seg.getmin(p2 + 1, n);
// cout << l1 << ' ' << r1 << ' ' << l2 << ' ' << r2 << endl;
int minval = min({l1, r1, l2, r2});
if(r1 <= l2) {
add(res, getsum(curval, minval - 1));
add(res, 1LL * l1 * (minval - curval) % mod);
add(res, 1LL * r1 * (minval - curval) % mod);
add(res, getsum(curval, minval - 1));
add(res, getsum(curval + 1, minval));
add(res, 1LL * r2 * (minval - curval) % mod);
}
else {
add(res, getsum(curval, minval - 1));
add(res, 1LL * l2 * (minval - curval) % mod);
add(res, 1LL * r2 * (minval - curval) % mod);
// cout << res << endl;
add(res, getsum(curval, minval - 1));
add(res, 1LL * l1 * (minval - curval) % mod);
add(res, getsum(curval + 1, minval));
}
add(res, 2LL * getsum(curval + 1, minval) % mod * (sz(s) - 2) % mod);
add(res, 1LL * (sz(s) - 2) * getsum(curval, minval - 1) % mod);
}
else {
int p1 = *s.begin();
int l1 = seg.getmin(1, p1 - 1);
int r1 = seg.getmin(p1 + 1, n);
// cout << l1 << ' ' << r1 << endl;
int minval = min(l1, r1);
add(res, getsum(curval, minval - 1));
add(res, 1LL * l1 * (minval - curval) % mod);
add(res, 1LL * r1 * (minval - curval) % mod);
}
}
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... |