#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 > y) return -INF;
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 += num2 * rw->rsum(idx, i-1) % MOD;
ans %= MOD;
l = i; 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-1;
num2 = num * w[i] % MOD;
ans += num2 * rw->rsum(i+1, idx) % MOD;
ans %= MOD;
}
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
2 ms |
604 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
2 ms |
604 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
600 KB |
Output is correct |
2 |
Correct |
25 ms |
3164 KB |
Output is correct |
3 |
Correct |
175 ms |
14452 KB |
Output is correct |
4 |
Correct |
391 ms |
28480 KB |
Output is correct |
5 |
Correct |
394 ms |
28572 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
25 ms |
3164 KB |
Output is correct |
4 |
Correct |
173 ms |
14660 KB |
Output is correct |
5 |
Correct |
386 ms |
28216 KB |
Output is correct |
6 |
Correct |
391 ms |
28244 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
25 ms |
3156 KB |
Output is correct |
9 |
Correct |
176 ms |
14500 KB |
Output is correct |
10 |
Incorrect |
398 ms |
28104 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
2 ms |
604 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
2 ms |
604 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |