// #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 += hs.top().second, 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 |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
444 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 |
428 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 |
23 ms |
1724 KB |
Output is correct |
4 |
Correct |
45 ms |
3016 KB |
Output is correct |
5 |
Correct |
45 ms |
3164 KB |
Output is correct |
6 |
Correct |
45 ms |
3148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
7 ms |
788 KB |
Output is correct |
3 |
Correct |
31 ms |
2128 KB |
Output is correct |
4 |
Correct |
64 ms |
3936 KB |
Output is correct |
5 |
Correct |
68 ms |
3920 KB |
Output is correct |
6 |
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 |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
6 ms |
600 KB |
Output is correct |
4 |
Correct |
31 ms |
2140 KB |
Output is correct |
5 |
Correct |
72 ms |
3772 KB |
Output is correct |
6 |
Correct |
64 ms |
3924 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
7 ms |
964 KB |
Output is correct |
9 |
Correct |
33 ms |
2868 KB |
Output is correct |
10 |
Correct |
59 ms |
6992 KB |
Output is correct |
11 |
Correct |
61 ms |
7112 KB |
Output is correct |
12 |
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 |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
440 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
800 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 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 |
448 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
444 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
23 ms |
1628 KB |
Output is correct |
12 |
Correct |
45 ms |
3156 KB |
Output is correct |
13 |
Correct |
45 ms |
3164 KB |
Output is correct |
14 |
Correct |
45 ms |
3132 KB |
Output is correct |
15 |
Correct |
2 ms |
344 KB |
Output is correct |
16 |
Correct |
7 ms |
604 KB |
Output is correct |
17 |
Correct |
34 ms |
2260 KB |
Output is correct |
18 |
Correct |
66 ms |
3900 KB |
Output is correct |
19 |
Correct |
64 ms |
4040 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
6 ms |
1116 KB |
Output is correct |
22 |
Correct |
32 ms |
2644 KB |
Output is correct |
23 |
Correct |
60 ms |
7104 KB |
Output is correct |
24 |
Correct |
64 ms |
7100 KB |
Output is correct |
25 |
Correct |
0 ms |
600 KB |
Output is correct |
26 |
Correct |
1 ms |
448 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 |
604 KB |
Output is correct |
31 |
Correct |
7 ms |
604 KB |
Output is correct |
32 |
Correct |
29 ms |
1884 KB |
Output is correct |
33 |
Correct |
31 ms |
1976 KB |
Output is correct |
34 |
Correct |
57 ms |
3668 KB |
Output is correct |
35 |
Correct |
62 ms |
3656 KB |
Output is correct |
36 |
Correct |
61 ms |
3768 KB |
Output is correct |
37 |
Correct |
62 ms |
3772 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
61 ms |
3776 KB |
Output is correct |
40 |
Incorrect |
62 ms |
3916 KB |
Output isn't correct |
41 |
Halted |
0 ms |
0 KB |
- |