#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll nx=1e5+5, mod=1e9+7;
ll n, ans, h, w, sm, H[nx], W[nx];
stack<pair<pair<ll, ll>, ll>> s;
ll cal(ll x)
{
return (x*(x+1)/2)%mod;
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n;
for (int i=0; i<n; i++) cin>>H[i];
for (int i=0; i<n; i++) cin>>W[i];
for (int i=0; i<n; i++)
{
ll h=H[i], w=W[i];
ans=(ans+(cal(h)*cal(w))%mod)%mod;
ll sw=0;
while (!s.empty()&&s.top().first.first>h)
{
sw=(sw+s.top().first.second)%mod;
sm=((sm-s.top().second)%mod+mod)%mod;
s.pop();
}
ll tmp=(cal(h)*sw)%mod;
s.push({{h, sw}, tmp});
sm=(sm+tmp)%mod;
ans=(ans+sm*w)%mod;
tmp=(cal(h)*w)%mod;
s.push({{h, w}, tmp});
sm=(sm+tmp)%mod;
}
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
9 ms |
1984 KB |
Output is correct |
4 |
Correct |
15 ms |
5716 KB |
Output is correct |
5 |
Correct |
22 ms |
3764 KB |
Output is correct |
6 |
Correct |
22 ms |
2280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
980 KB |
Output is correct |
3 |
Correct |
10 ms |
3484 KB |
Output is correct |
4 |
Correct |
20 ms |
6732 KB |
Output is correct |
5 |
Correct |
19 ms |
6640 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
980 KB |
Output is correct |
4 |
Correct |
10 ms |
3496 KB |
Output is correct |
5 |
Correct |
19 ms |
6740 KB |
Output is correct |
6 |
Correct |
30 ms |
6652 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
980 KB |
Output is correct |
9 |
Correct |
10 ms |
3464 KB |
Output is correct |
10 |
Correct |
19 ms |
6688 KB |
Output is correct |
11 |
Correct |
27 ms |
6664 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
8 ms |
2644 KB |
Output is correct |
12 |
Correct |
18 ms |
6852 KB |
Output is correct |
13 |
Correct |
19 ms |
4940 KB |
Output is correct |
14 |
Correct |
24 ms |
3508 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
4 ms |
1108 KB |
Output is correct |
17 |
Correct |
15 ms |
4504 KB |
Output is correct |
18 |
Correct |
20 ms |
8640 KB |
Output is correct |
19 |
Correct |
25 ms |
8716 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
1108 KB |
Output is correct |
22 |
Correct |
10 ms |
4436 KB |
Output is correct |
23 |
Correct |
19 ms |
8492 KB |
Output is correct |
24 |
Correct |
20 ms |
8556 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
360 KB |
Output is correct |
28 |
Correct |
1 ms |
360 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
3 ms |
616 KB |
Output is correct |
31 |
Correct |
2 ms |
604 KB |
Output is correct |
32 |
Correct |
10 ms |
1988 KB |
Output is correct |
33 |
Correct |
13 ms |
2004 KB |
Output is correct |
34 |
Correct |
28 ms |
3608 KB |
Output is correct |
35 |
Correct |
28 ms |
3624 KB |
Output is correct |
36 |
Correct |
20 ms |
3728 KB |
Output is correct |
37 |
Correct |
22 ms |
3780 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
20 ms |
3784 KB |
Output is correct |
40 |
Correct |
29 ms |
3760 KB |
Output is correct |
41 |
Correct |
20 ms |
4276 KB |
Output is correct |
42 |
Correct |
29 ms |
6220 KB |
Output is correct |