#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e5;
const ll INF = 1e18;
const ll MOD = 1e9+7;
int N;
ll W[MAXN+10], H[MAXN+10];
pll tree[MAXN*4+10];
void init(int node, int tl, int tr)
{
if(tl==tr)
{
tree[node]={H[tl], tl};
return;
}
int mid=tl+tr>>1;
init(node*2, tl, mid);
init(node*2+1, mid+1, tr);
tree[node]=min(tree[node*2], tree[node*2+1]);
}
pll query(int node, int tl, int tr, int l, int r)
{
if(l<=tl && tr<=r) return tree[node];
if(r<tl || tr<l) return {INF, -1};
int mid=tl+tr>>1;
return min(query(node*2, tl, mid, l, r), query(node*2+1, mid+1, tr, l, r));
}
ll ans=0;
ll f(ll x)
{
x%=MOD;
return x*(x+1)%MOD*((MOD+1)/2)%MOD;
}
ll f(ll l, ll r)
{
return ((f(r)-f(l))%MOD+MOD)%MOD;
}
void solve(int l, int r, ll h)
{
if(l>r) return;
pll p=query(1, 1, N, l, r);
ans+=f(h, p.first)*f(0, W[r]-W[l-1])%MOD;
ans%=MOD;
solve(l, p.second-1, p.first);
solve(p.second+1, r, p.first);
}
int main()
{
scanf("%d", &N);
for(int i=1; i<=N; i++) scanf("%lld", &H[i]);
for(int i=1; i<=N; i++) scanf("%lld", &W[i]), W[i]+=W[i-1];
init(1, 1, N);
solve(1, N, 0);
printf("%lld\n", ans);
}
Compilation message
fancyfence.cpp: In function 'void init(int, int, int)':
fancyfence.cpp:24:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
24 | int mid=tl+tr>>1;
| ~~^~~
fancyfence.cpp: In function 'pll query(int, int, int, int, int)':
fancyfence.cpp:34:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
34 | int mid=tl+tr>>1;
| ~~^~~
fancyfence.cpp: In function 'int main()':
fancyfence.cpp:65:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
65 | scanf("%d", &N);
| ~~~~~^~~~~~~~~~
fancyfence.cpp:66:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
66 | for(int i=1; i<=N; i++) scanf("%lld", &H[i]);
| ~~~~~^~~~~~~~~~~~~~~
fancyfence.cpp:67:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
67 | for(int i=1; i<=N; i++) scanf("%lld", &W[i]), W[i]+=W[i-1];
| ~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
30 ms |
3200 KB |
Output is correct |
4 |
Correct |
58 ms |
6008 KB |
Output is correct |
5 |
Correct |
57 ms |
6008 KB |
Output is correct |
6 |
Correct |
59 ms |
6008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
6 ms |
1024 KB |
Output is correct |
3 |
Correct |
41 ms |
3200 KB |
Output is correct |
4 |
Correct |
59 ms |
6008 KB |
Output is correct |
5 |
Correct |
86 ms |
6008 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
12 ms |
1024 KB |
Output is correct |
4 |
Correct |
30 ms |
3200 KB |
Output is correct |
5 |
Correct |
59 ms |
6008 KB |
Output is correct |
6 |
Correct |
61 ms |
6008 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
6 ms |
1024 KB |
Output is correct |
9 |
Correct |
29 ms |
3196 KB |
Output is correct |
10 |
Correct |
61 ms |
5976 KB |
Output is correct |
11 |
Correct |
76 ms |
5924 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
288 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
28 ms |
3208 KB |
Output is correct |
12 |
Correct |
60 ms |
6008 KB |
Output is correct |
13 |
Correct |
57 ms |
6020 KB |
Output is correct |
14 |
Correct |
59 ms |
6016 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
6 ms |
1024 KB |
Output is correct |
17 |
Correct |
29 ms |
3200 KB |
Output is correct |
18 |
Correct |
60 ms |
6008 KB |
Output is correct |
19 |
Correct |
60 ms |
6016 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
6 ms |
1024 KB |
Output is correct |
22 |
Correct |
29 ms |
3200 KB |
Output is correct |
23 |
Correct |
60 ms |
6008 KB |
Output is correct |
24 |
Correct |
63 ms |
6008 KB |
Output is correct |
25 |
Correct |
0 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
6 ms |
1024 KB |
Output is correct |
31 |
Correct |
7 ms |
1024 KB |
Output is correct |
32 |
Correct |
32 ms |
3196 KB |
Output is correct |
33 |
Correct |
33 ms |
3192 KB |
Output is correct |
34 |
Correct |
67 ms |
6016 KB |
Output is correct |
35 |
Correct |
64 ms |
6008 KB |
Output is correct |
36 |
Correct |
69 ms |
6032 KB |
Output is correct |
37 |
Correct |
65 ms |
6008 KB |
Output is correct |
38 |
Correct |
0 ms |
384 KB |
Output is correct |
39 |
Correct |
67 ms |
6012 KB |
Output is correct |
40 |
Correct |
71 ms |
6136 KB |
Output is correct |
41 |
Correct |
72 ms |
6272 KB |
Output is correct |
42 |
Correct |
64 ms |
7544 KB |
Output is correct |