// #include <bits/stdc++.h>
#include <vector>
#include <stack>
#include <iostream>
using namespace std;
#define int long long
#define pii pair<int, int>
const int MOD = 1e9 + 7;
const int INV2 = 500000004;
// ifstream cin("a");
// ofstream cout("b");
int32_t main() {
int n; cin >> n;
vector<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];
stack<int> hsum, area;
stack<pii> hs;
hsum.push(0), area.push(0);
hs.push({0, 0});
int ans = 0;
for (int i = 0; i < n; ++i) {
// add indiv to answer:
ans += (h[i] * (h[i] + 1) / 2 % MOD) * (w[i] * (w[i] + 1) / 2 % MOD) % MOD;
// cout << ans << ' ';
// remove unneccesary:
int nw = 0;
while (hs.top().first >= h[i]) nw = (nw + hs.top().second) % MOD, hsum.pop(), area.pop(), hs.pop();
// insert (h[i], nw):
// cout << "nw " << nw << ' ';
int lh = hs.top().first + 1;
if (nw > 0) {
hsum.push((hsum.top() + (h[i] + lh) % MOD * (h[i] - lh + 1) % MOD * INV2 % MOD) % MOD);
area.push((area.top() + hsum.top() * nw % MOD) % MOD);
hs.push({h[i], w[i]});
}
// add rest to answer:
ans = (ans + w[i] * area.top() % MOD) % MOD;
// insert (h[i], w[i]):
if (nw > 0) hsum.pop(), area.pop(), hs.pop();
lh = hs.top().first + 1;
w[i] = (w[i] + nw) % MOD;
// cout << lh << ' ' << h[i] << ' ';
hsum.push((hsum.top() + (h[i] + lh) % MOD * (h[i] - lh + 1) % MOD * INV2 % MOD) % MOD);
// cout << hsum.top() << ' ';
area.push((area.top() + hsum.top() * w[i] % MOD) % MOD);
hs.push({h[i], w[i]});
// cout << ans << '\n';
}
cout << ans << '\n';
return 0;
}
/*
System.out.println("Hello World");
public static double yamom(String "s"){
}
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
22 ms |
1116 KB |
Output is correct |
4 |
Correct |
44 ms |
1884 KB |
Output is correct |
5 |
Correct |
44 ms |
1880 KB |
Output is correct |
6 |
Correct |
60 ms |
1884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
7 ms |
348 KB |
Output is correct |
3 |
Correct |
32 ms |
1116 KB |
Output is correct |
4 |
Correct |
67 ms |
1880 KB |
Output is correct |
5 |
Correct |
63 ms |
2136 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
7 ms |
348 KB |
Output is correct |
4 |
Correct |
31 ms |
1232 KB |
Output is correct |
5 |
Correct |
62 ms |
1884 KB |
Output is correct |
6 |
Correct |
63 ms |
1884 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
7 ms |
856 KB |
Output is correct |
9 |
Correct |
31 ms |
1744 KB |
Output is correct |
10 |
Correct |
64 ms |
5068 KB |
Output is correct |
11 |
Correct |
60 ms |
5204 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
532 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
448 KB |
Output is correct |
11 |
Correct |
22 ms |
1116 KB |
Output is correct |
12 |
Correct |
45 ms |
1884 KB |
Output is correct |
13 |
Correct |
44 ms |
1884 KB |
Output is correct |
14 |
Correct |
45 ms |
1880 KB |
Output is correct |
15 |
Correct |
1 ms |
444 KB |
Output is correct |
16 |
Correct |
8 ms |
348 KB |
Output is correct |
17 |
Correct |
31 ms |
1116 KB |
Output is correct |
18 |
Correct |
61 ms |
1992 KB |
Output is correct |
19 |
Correct |
63 ms |
1992 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
7 ms |
856 KB |
Output is correct |
22 |
Correct |
31 ms |
1708 KB |
Output is correct |
23 |
Correct |
58 ms |
5060 KB |
Output is correct |
24 |
Correct |
60 ms |
5216 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
6 ms |
348 KB |
Output is correct |
31 |
Correct |
6 ms |
348 KB |
Output is correct |
32 |
Correct |
29 ms |
1116 KB |
Output is correct |
33 |
Correct |
30 ms |
1116 KB |
Output is correct |
34 |
Correct |
76 ms |
2132 KB |
Output is correct |
35 |
Correct |
57 ms |
1884 KB |
Output is correct |
36 |
Correct |
60 ms |
1992 KB |
Output is correct |
37 |
Correct |
62 ms |
1884 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
60 ms |
1992 KB |
Output is correct |
40 |
Correct |
67 ms |
1992 KB |
Output is correct |
41 |
Correct |
61 ms |
4028 KB |
Output is correct |
42 |
Correct |
62 ms |
5460 KB |
Output is correct |