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;
#define int long long
#define INF 1000000000000000000
#define MOD 1000000007
struct node {
int s, e, m, sm, mn;
node *l, *r;
node(int _s, int _e) {
s = _s, e = _e, m = (s+e)/2, sm = mn = 0;
if(s != e) {
l = new node(s, m);
r = new node(m+1, e);
}
}
void update(int x, int val) {
if(s == e) {sm = mn = val; return;}
else if(x > m) r->update(x, val);
else l->update(x, val);
sm = (l->sm + r->sm) % MOD;
mn = min(l->mn, r->mn);
}
int rsum(int x, int y) {
if(x > y) return 0;
if(x <= s && e <= y) return sm;
else if(x > m) return r->rsum(x, y);
else if(y <= m) return l->rsum(x, y);
else return (l->rsum(x, y) + r->rsum(x, y)) % MOD;
}
int rmin(int x, int y) {
if(x <= s && e <= y) return mn;
else if(x > m) return r->rmin(x, y);
else if(y <= m) return l->rmin(x, y);
else return min(l->rmin(x, y), r->rmin(x, y));
}
} *rh, *rw;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, ans = 0;
cin >> n;
int h[n], w[n];
for(int i = 0; i < n; i++) cin >> h[i];
for(int i = 0; i < n; i++) cin >> w[i];
rh = new node(0, n);
rw = new node(0, n);
for(int i = 0; i < n; i++) {
rh->update(i, h[i]);
rw->update(i, w[i]);
}
rh->update(n, INF);
for(int i = 0; i < n; i++) {
int num = h[i]*(h[i]+1)/2 % MOD, num2 = w[i]*(w[i]+1)/2 % MOD;
ans += num*num2 % MOD;
ans %= MOD;
int l = 0, r = i, m;
while(l < r) {
m = (l+r)/2;
if(rh->rmin(m, i) < h[i]) l = m+1;
else r = m;
}
int idx = l;
num2 = num * w[i] % MOD;
ans += num * rw->rsum(idx, i-1) % MOD;
ans %= MOD;
l = i+1; r = n;
while(l < r) {
m = (l+r)/2;
if(rh->rmin(i+1, m) <= h[i]) r = m;
else l = m+1;
}
idx = l;
num2 = num * w[i] % MOD;
ans += num * rw->rsum(i+1, idx-1) % MOD;
ans %= MOD;
}
cout << ans;
}
# | 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... |