#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include "../Library/debug.h"
#else
#define dbg(x...)
// #define cin fin
// #define cout fout
// const string FILE_NAME = ""; ifstream fin(FILE_NAME + ".in"); ofstream fout(FILE_NAME + ".out");
#endif
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) for (int i = 0; i < (a); ++i)
#define FORd(i, a, b) for (int i = (b) - 1; i >= (a); --i)
#define F0Rd(i, a) for (int i = (a) - 1; i >= 0; --i)
#define trav(a, x) for (auto& a : x)
#define f first
#define s second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
const char nl = '\n';
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const int mxN = 1e6 + 1;
ll mul(ll a, ll b){
return (ll) a * b % MOD;
}
const ll INV_TWO = (MOD + 1) / 2;
ll half(ll a){
return mul(a, INV_TWO);
}
void add_self(ll& a, ll b){
a += b;
if(a >= MOD)
a -= MOD;
while(a < 0){
a += MOD;
}
}
ll f(ll a){
return half(mul(a, a + 1));
}
void solve(){
int n;
cin >> n;
vl h(n + 1), w(n + 1);
F0R(i, n)
cin >> h[i];
F0R(i, n)
cin >> w[i];
h[n] = 0;
w[n] = 0;
// h, totalw;
stack<array<ll, 2>> st;
ll ans = 0;
FOR(i, 0, n + 1){
ll w_left = 0;
while(!st.empty() && st.top()[0] >= h[i]){
add_self(w_left, st.top()[1]);
add_self(ans, mul(f(st.top()[0]), f(w_left)));
add_self(ans, -mul(f(st.top()[0]), f(w_left - st.top()[1])));
st.pop();
}
/* dbg(i, w_left, h[i], w[i]); */
/* dbg(ans); */
if(w_left != 0){
add_self(ans, -mul(f(h[i]), f(w_left)));
}
/* dbg(ans); */
st.push({h[i], (w[i] + w_left) % MOD});
}
cout << ans << nl;
}
int32_t main(){
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int TC = 1;
/* cin >> TC; */
while(TC--){
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
8 ms |
1836 KB |
Output is correct |
4 |
Correct |
15 ms |
3164 KB |
Output is correct |
5 |
Correct |
15 ms |
3164 KB |
Output is correct |
6 |
Correct |
15 ms |
3164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
604 KB |
Output is correct |
3 |
Correct |
9 ms |
1248 KB |
Output is correct |
4 |
Correct |
17 ms |
2036 KB |
Output is correct |
5 |
Correct |
18 ms |
2036 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 |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
604 KB |
Output is correct |
4 |
Correct |
9 ms |
1116 KB |
Output is correct |
5 |
Correct |
17 ms |
1884 KB |
Output is correct |
6 |
Correct |
18 ms |
1884 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
976 KB |
Output is correct |
9 |
Correct |
10 ms |
2396 KB |
Output is correct |
10 |
Correct |
18 ms |
5468 KB |
Output is correct |
11 |
Correct |
19 ms |
5592 KB |
Output is correct |
12 |
Correct |
0 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 |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 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 |
460 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
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 |
348 KB |
Output is correct |
11 |
Correct |
8 ms |
1628 KB |
Output is correct |
12 |
Correct |
15 ms |
3160 KB |
Output is correct |
13 |
Correct |
15 ms |
3360 KB |
Output is correct |
14 |
Correct |
16 ms |
3164 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
9 ms |
2140 KB |
Output is correct |
18 |
Correct |
19 ms |
3792 KB |
Output is correct |
19 |
Correct |
19 ms |
3944 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
860 KB |
Output is correct |
22 |
Correct |
9 ms |
2396 KB |
Output is correct |
23 |
Correct |
19 ms |
5352 KB |
Output is correct |
24 |
Correct |
19 ms |
5468 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
464 KB |
Output is correct |
27 |
Correct |
1 ms |
600 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
600 KB |
Output is correct |
30 |
Correct |
4 ms |
808 KB |
Output is correct |
31 |
Correct |
3 ms |
604 KB |
Output is correct |
32 |
Correct |
11 ms |
1876 KB |
Output is correct |
33 |
Correct |
11 ms |
2424 KB |
Output is correct |
34 |
Correct |
18 ms |
3676 KB |
Output is correct |
35 |
Correct |
18 ms |
3676 KB |
Output is correct |
36 |
Correct |
19 ms |
3932 KB |
Output is correct |
37 |
Correct |
19 ms |
3932 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
18 ms |
4184 KB |
Output is correct |
40 |
Correct |
19 ms |
3936 KB |
Output is correct |
41 |
Correct |
19 ms |
3928 KB |
Output is correct |
42 |
Correct |
25 ms |
4704 KB |
Output is correct |