#include <bits/stdc++.h>
#define ll long long
#define ar array
#define db double
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rint(l, r) uniform_int_distribution<int>(l, r)(rng)
template<typename T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
const int mod = 1e9+7, nax = 1e5 + 5;
int tp = -1;
ar<int, 2> stk[nax];
int f(int x) {
return ((ll)x*(x+1)/2)%mod;
}
int mul(int a, int b) {
return (ll)a*b%mod;
}
void fadd(int &a, int b) {
a += b;
if (a >= mod) a -= mod;
}
void fsub(int &a, int b) {
a -= b;
if (a < 0) a += mod;
}
void test_case() {
int n; cin >> n;
vector<int> h(n), w(n);
for (int &x : h) cin >> x;
for (int &x : w) cin >> x;
int ans = 0, t = 0;
for (int i = 0; i < n; i++) {
fadd(ans, mul(f(h[i]), f(w[i])));
int L = 0;
while (tp >= 0 && stk[tp][0] >= h[i]) {
fsub(t, mul(f(stk[tp][0]), stk[tp][1]));
fadd(L, stk[tp][1]);
tp--;
}
fadd(ans, mul(t, w[i]));
fadd(ans, mul(mul(f(h[i]), L), w[i]));
fadd(L, w[i]);
stk[++tp] = {h[i], L};
fadd(t, mul(f(h[i]), L));
}
cout << ans << '\n';
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1;
// cin >> t;
while (t--) test_case();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
6 ms |
860 KB |
Output is correct |
4 |
Correct |
15 ms |
2396 KB |
Output is correct |
5 |
Correct |
13 ms |
2396 KB |
Output is correct |
6 |
Correct |
13 ms |
2272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
7 ms |
860 KB |
Output is correct |
4 |
Correct |
16 ms |
3164 KB |
Output is correct |
5 |
Correct |
17 ms |
3312 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
7 ms |
860 KB |
Output is correct |
5 |
Correct |
15 ms |
3164 KB |
Output is correct |
6 |
Correct |
15 ms |
3164 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
8 ms |
1884 KB |
Output is correct |
10 |
Correct |
14 ms |
3672 KB |
Output is correct |
11 |
Correct |
14 ms |
3940 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 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 |
600 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 |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 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 |
1 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 |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
5 ms |
860 KB |
Output is correct |
12 |
Correct |
10 ms |
2404 KB |
Output is correct |
13 |
Correct |
11 ms |
2272 KB |
Output is correct |
14 |
Correct |
12 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
8 ms |
1628 KB |
Output is correct |
18 |
Correct |
14 ms |
3164 KB |
Output is correct |
19 |
Correct |
17 ms |
3296 KB |
Output is correct |
20 |
Correct |
1 ms |
504 KB |
Output is correct |
21 |
Correct |
2 ms |
604 KB |
Output is correct |
22 |
Correct |
9 ms |
1884 KB |
Output is correct |
23 |
Correct |
18 ms |
3676 KB |
Output is correct |
24 |
Correct |
17 ms |
3804 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
480 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
2 ms |
604 KB |
Output is correct |
31 |
Correct |
2 ms |
604 KB |
Output is correct |
32 |
Correct |
8 ms |
1628 KB |
Output is correct |
33 |
Correct |
8 ms |
1852 KB |
Output is correct |
34 |
Correct |
14 ms |
3016 KB |
Output is correct |
35 |
Correct |
14 ms |
2920 KB |
Output is correct |
36 |
Correct |
18 ms |
3040 KB |
Output is correct |
37 |
Correct |
18 ms |
3404 KB |
Output is correct |
38 |
Correct |
1 ms |
344 KB |
Output is correct |
39 |
Correct |
16 ms |
3212 KB |
Output is correct |
40 |
Correct |
16 ms |
3160 KB |
Output is correct |
41 |
Correct |
17 ms |
3164 KB |
Output is correct |
42 |
Correct |
16 ms |
3420 KB |
Output is correct |