#include<bits/stdc++.h>
//#include "grader.h"
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#define ld long double
#define st first
#define nd second
#define pb push_back
#define INF INT_MAX
using namespace std;
const int N = 1e5 + 10;
const int M = 1e9+7;
ll h[N], w[N];
stack<pii> s;
ll ans;
ll add(ll a, ll b) {return ((((a%M)+(b%M))%M)+M)%M;}
ll mul(ll a, ll b) {return (a%M*b%M)%M;}
ll power(ll a, ll b) {
if (b==0) return 1;
ll ret = power(a, b/2);
if (b&1) return mul(ret, mul(ret, a));
else return mul(ret, ret);
}
void solve() {
int n; cin >> n;
for (int i = 1; i <= n; ++i) cin >> h[i];
for (int i = 1; i <= n; ++i) cin >> w[i];
s.push({0, 0});
ll inv = power(2, M-2);
for (int i = 1; i <= n; ++i) {
while (s.top().st > h[i]) {
auto [H, W] = s.top(); s.pop();
if (s.top().st > h[i]) {
H -= s.top().st;
auto [HH, WW] = s.top(); s.pop();
s.push({HH, add(WW, W)});
ans = add(ans, mul(mul(H, s.top().st), mul(mul(W, W+1), inv)));
}
else {
H -= h[i];
s.push({h[i], W});
ans = add(ans, mul(mul(H, h[i]), mul(mul(W, W+1), inv)));
}
ans = add(ans, mul(mul(mul(H, H+1), inv), mul(mul(W, W+1), inv)));
}
if (s.top().st == h[i]) {
auto [HH, WW] = s.top(); s.pop();
s.push({HH, add(WW, w[i])});
}
else
s.push({h[i], w[i]});
}
while (s.size() > 1) {
auto [H, W] = s.top(); s.pop();
H -= s.top().st;
auto [HH, WW] = s.top(); s.pop();
s.push({HH, add(WW, W)});
ans = add(ans, mul(mul(H, s.top().st), mul(mul(W, W+1), inv)));
ans = add(ans, mul(mul(mul(H, H+1), inv), mul(mul(W, W+1), inv)));
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(NULL);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}
Compilation message
fancyfence.cpp: In function 'void solve()':
fancyfence.cpp:40:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
40 | auto [H, W] = s.top(); s.pop();
| ^
fancyfence.cpp:43:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
43 | auto [HH, WW] = s.top(); s.pop();
| ^
fancyfence.cpp:55:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
55 | auto [HH, WW] = s.top(); s.pop();
| ^
fancyfence.cpp:62:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
62 | auto [H, W] = s.top(); s.pop();
| ^
fancyfence.cpp:64:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
64 | auto [HH, WW] = s.top(); s.pop();
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
10 ms |
1716 KB |
Output is correct |
4 |
Correct |
20 ms |
3052 KB |
Output is correct |
5 |
Correct |
20 ms |
3148 KB |
Output is correct |
6 |
Correct |
17 ms |
3084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
596 KB |
Output is correct |
3 |
Correct |
12 ms |
2072 KB |
Output is correct |
4 |
Correct |
23 ms |
3776 KB |
Output is correct |
5 |
Correct |
24 ms |
3876 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
560 KB |
Output is correct |
4 |
Correct |
12 ms |
2120 KB |
Output is correct |
5 |
Correct |
22 ms |
3788 KB |
Output is correct |
6 |
Correct |
24 ms |
3804 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
4 ms |
724 KB |
Output is correct |
9 |
Correct |
13 ms |
2284 KB |
Output is correct |
10 |
Correct |
25 ms |
4416 KB |
Output is correct |
11 |
Correct |
26 ms |
4556 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
368 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
324 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 |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
10 ms |
1756 KB |
Output is correct |
12 |
Correct |
18 ms |
3288 KB |
Output is correct |
13 |
Correct |
18 ms |
3156 KB |
Output is correct |
14 |
Correct |
17 ms |
3028 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
600 KB |
Output is correct |
17 |
Correct |
12 ms |
2064 KB |
Output is correct |
18 |
Correct |
22 ms |
3700 KB |
Output is correct |
19 |
Correct |
24 ms |
3884 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
4 ms |
724 KB |
Output is correct |
22 |
Correct |
13 ms |
2248 KB |
Output is correct |
23 |
Correct |
24 ms |
4492 KB |
Output is correct |
24 |
Correct |
30 ms |
4556 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
3 ms |
596 KB |
Output is correct |
31 |
Correct |
3 ms |
600 KB |
Output is correct |
32 |
Correct |
12 ms |
1876 KB |
Output is correct |
33 |
Correct |
13 ms |
1968 KB |
Output is correct |
34 |
Correct |
26 ms |
3680 KB |
Output is correct |
35 |
Correct |
24 ms |
3532 KB |
Output is correct |
36 |
Correct |
24 ms |
3724 KB |
Output is correct |
37 |
Correct |
25 ms |
3800 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
25 ms |
3696 KB |
Output is correct |
40 |
Correct |
26 ms |
3788 KB |
Output is correct |
41 |
Correct |
24 ms |
3788 KB |
Output is correct |
42 |
Correct |
25 ms |
4240 KB |
Output is correct |