#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;
const int mod = 1e9+7;
const int N = 100'010;
int lft[N], rgt[N];
ll h[N], w[N], ps[N];
int n;
ll solve(int l, int r, int i, ll hx)
{
if (l >= r)
return 0;
ll x = h[i] - hx;
ll y = ps[r] - ps[l];
ll ans = 0;
ans += (x*(x+1)/2 % mod) * (y*(y+1)/2 % mod);
ans += (x * hx % mod) * (y*(y+1)/2 % mod);
ans += solve(l, i, lft[i], h[i]);
ans += solve(i+1, r, rgt[i], h[i]);
ans %= mod;
return ans;
}
int main()
{
cin.tie(0) -> sync_with_stdio(false);
cin >> n;
Loop (i,0,n)
cin >> h[i];
Loop (i,0,n) {
cin >> w[i];
ps[i+1] = ps[i] + w[i];
}
vector<int> vec;
Loop (i,0,n) {
int lst = -1;
while (vec.size() && h[vec.back()] > h[i]) {
lst = vec.back();
vec.pop_back();
}
lft[i] = lst;
if (vec.size())
rgt[vec.back()] = i;
vec.push_back(i);
}
cout << solve(0, n, vec[0], 0) << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |