/**********************GOD**********************/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <iterator>
#include <map>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll , ll> pll;
#define len length()
#define MP make_pair
#define fs first
#define sc second
#define pb push_back
#define all(x) x.begin() , x.end()
#define kill(x) cout << x , exit(0)
const ll mod = 1e9 + 7;
const int MXN = 2e5 + 4;
ll n, ans;
ll h[MXN], w[MXN], ps[MXN];
ll l[MXN], r[MXN];
ll C(ll x)
{
ll y = x * (x - 1) / 2;
return y % mod;
}
ll Sm(ll a, ll b)
{
return (ps[b] - ps[a] + mod) % mod;
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for(ll i = 1; i <= n; i ++)
cin >> h[i];
for(ll i = 1; i <= n; i ++) {
cin >> w[i];
ps[i] = (ps[i - 1] + w[i]) % mod;
}
ps[n + 1] = ps[n];
stack<ll> cnd; cnd.push(0);
for(ll i = 1; i <= n; i ++) {
while(cnd.size() && h[i] < h[cnd.top()])
cnd.pop();
if(cnd.size())
l[i] = cnd.top();
cnd.push(i);
}
stack<ll> cdn; cdn.push(0);
for(ll i = n; i > 0; i --) {
while(cdn.size() && h[i] <= h[cdn.top()])
cdn.pop();
if(cdn.size())
r[i] = cdn.top();
cdn.push(i);
}
for(ll i = 1; i <= n; i ++)
if(!r[i])
r[i] = n + 1;
for(ll i = 1; i <= n; i ++) {
ll hs = C(h[i] + 1);
ll x = (Sm(i, r[i] - 1) + 1) % mod;
ll y = (Sm(l[i], i - 1) + 1) % mod;
ll tmp = x * y % mod;
ll z = (w[i] - 1) * x % mod;
ll t = (w[i] - 1) * y % mod;
ll o = C(w[i] - 1);
tmp = (tmp + z + t + o) % mod;
ans = (ans + tmp * hs % mod) % mod;
//cout << i << ' ' << x << '\n';
}
cout << ans;
return 0;
}
/*!
ahkh
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
9 ms |
5980 KB |
Output is correct |
4 |
Correct |
16 ms |
8132 KB |
Output is correct |
5 |
Correct |
17 ms |
7836 KB |
Output is correct |
6 |
Correct |
16 ms |
7516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
4 ms |
4956 KB |
Output is correct |
3 |
Correct |
13 ms |
6748 KB |
Output is correct |
4 |
Correct |
23 ms |
8920 KB |
Output is correct |
5 |
Correct |
19 ms |
9040 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
3 ms |
4956 KB |
Output is correct |
4 |
Correct |
10 ms |
6620 KB |
Output is correct |
5 |
Correct |
19 ms |
9084 KB |
Output is correct |
6 |
Correct |
19 ms |
9052 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
3 ms |
4956 KB |
Output is correct |
9 |
Correct |
12 ms |
6748 KB |
Output is correct |
10 |
Correct |
19 ms |
8988 KB |
Output is correct |
11 |
Correct |
19 ms |
8904 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4572 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4440 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4568 KB |
Output is correct |
3 |
Correct |
1 ms |
4640 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4560 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
9 ms |
5976 KB |
Output is correct |
12 |
Correct |
18 ms |
8028 KB |
Output is correct |
13 |
Correct |
20 ms |
7656 KB |
Output is correct |
14 |
Correct |
19 ms |
7516 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
3 ms |
4956 KB |
Output is correct |
17 |
Correct |
10 ms |
6796 KB |
Output is correct |
18 |
Correct |
22 ms |
9044 KB |
Output is correct |
19 |
Correct |
26 ms |
8972 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
2 ms |
4828 KB |
Output is correct |
22 |
Correct |
11 ms |
6692 KB |
Output is correct |
23 |
Correct |
20 ms |
9020 KB |
Output is correct |
24 |
Correct |
19 ms |
9052 KB |
Output is correct |
25 |
Correct |
1 ms |
4440 KB |
Output is correct |
26 |
Correct |
1 ms |
4700 KB |
Output is correct |
27 |
Correct |
1 ms |
4600 KB |
Output is correct |
28 |
Correct |
1 ms |
4440 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
3 ms |
4700 KB |
Output is correct |
31 |
Correct |
3 ms |
4828 KB |
Output is correct |
32 |
Correct |
10 ms |
6232 KB |
Output is correct |
33 |
Correct |
11 ms |
6236 KB |
Output is correct |
34 |
Correct |
19 ms |
7924 KB |
Output is correct |
35 |
Correct |
22 ms |
8016 KB |
Output is correct |
36 |
Correct |
20 ms |
8116 KB |
Output is correct |
37 |
Correct |
25 ms |
8284 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
19 ms |
8212 KB |
Output is correct |
40 |
Correct |
19 ms |
8220 KB |
Output is correct |
41 |
Correct |
19 ms |
8244 KB |
Output is correct |
42 |
Correct |
19 ms |
8540 KB |
Output is correct |