#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 1000000000000000000
#define MOD 1000000007
struct node {
int s, e, m, mx, sm, lz;
node *l, *r;
node(int _s, int _e) {
s = _s, e = _e, m = (s+e)/2, mx = sm = (int)1e9, lz = -1;
l = nullptr; r = nullptr;
}
void prop() {
if(lz == -1) return;
sm = lz*(e-s+1) % MOD;
mx = lz;
if(s != e) {
if(l == nullptr) l = new node(s, m);
if(r == nullptr) r = new node(m+1, e);
l->lz = lz;
r->lz = lz;
}
lz = -1;
}
void rset(int x, int y, int val) {
if(x > y) return;
prop();
if(x <= s && e <= y) {lz = val; return;}
else if(x > m) {
if(r == nullptr) r = new node(m+1, e);
r->rset(x, y, val);
} else if(y <= m) {
if(l == nullptr) l = new node(s, m);
l->rset(x, y, val);
}
else {
if(l == nullptr) l = new node(s, m);
if(r == nullptr) r = new node(m+1, e);
l->rset(x, y, val), r->rset(x, y, val);
}
if(l != nullptr) l->prop();
if(r != nullptr) r->prop();
if(l == nullptr) sm = r->sm, mx = r->mx;
else if(r == nullptr) sm = l->sm, mx = l->mx;
else sm = (l->sm + r->sm)%MOD, mx = max(l->mx, r->mx);
}
int rsum(int x, int y) {
if(x > y) return 0;
prop();
if(x <= s && e <= y) return sm;
else if(x > m) return r==nullptr?0:r->rsum(x, y);
else if(y <= m) return l==nullptr?0:l->rsum(x, y);
else return ((l==nullptr?0:l->rsum(x, y)) + (r==nullptr?0:r->rsum(x, y))) % MOD;
}
int rmax(int x, int y) {
if(x > y) return -INF;
prop();
if(x <= s && e <= y) return mx;
else if(x > m) return r==nullptr?-INF:r->rmax(x, y);
else if(y <= m) return l==nullptr?-INF:l->rmax(x, y);
else return max((l==nullptr?-INF:l->rmax(x, y)), (r==nullptr?-INF:r->rmax(x, y)));
}
} *root;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
int h[n], w[n], dp[n], dpsm = 0;
for(int i = 0; i < n; i++) cin >> h[i];
for(int i = 0; i < n; i++) cin >> w[i];
root = new node(0, INF);
int cur = 0;
for(int i = 0; i < n; i++) {
int l = 0, r = cur-1, m;
while(l < r) {
m = (l+r)/2;
if(root->rmax(m, i) <= h[i]) l = m+1;
else r = m;
}
root->rset(l, cur-1, h[i]);
dp[i] = (w[i]+1)*w[i]/2 * (h[i]+1)*h[i]/2 + dpsm + root->rsum(0, cur-1);
dpsm += dp[i];
cur += w[i];
root->rset(cur-w[i], cur-1, h[i]);
}
cout << dp[n-1];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
11 ms |
9308 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 |
8 ms |
9308 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
9308 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 |
9 ms |
9308 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
9 ms |
9308 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
11 ms |
9308 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |