#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
const ll m = 1e9 + 7;
ll ml(ll x){
ll y = x+1;
if(y%2 == 0) y/=2;
else x/=2;
x%=m;
y%=m;
return (x*y)%m;
}
ll sth(ll a, ll b){
ll x = b-a;
ll s1 = x+2*a+1;
if(x%2==0) x/=2;
else s1/=2;
return (x*s1)%m;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n; cin >> n;
vector <ll> h(n), w(n);
for(ll &x: h) cin >> x;
for(ll &x: w) cin >> x;
h.pb(0);
vector <int> in(n+1), in2(n+1);
stack <int> st;
vector <ll> sum(n+1);
for(int i = 0; i <= n; i++){
if(st.empty()){
st.push(i);
continue;
}
ll s = 0;
while(!st.empty() && h[st.top()] > h[i]){
int x = st.top();
st.pop();
in[x] = i;
}
st.push(i);
}
while(!st.empty()) st.pop();
h.pop_back();
h.insert(h.begin(), 0);
for(int i = n; i >= 0; i--){
if(st.empty()){
st.push(i);
continue;
}
ll s = 0;
while(!st.empty() && h[st.top()] > h[i]){
int x = st.top();
st.pop();
in2[x - 1] = i - 1;
}
st.push(i);
}
while(!st.empty()) st.pop();
h.erase(h.begin());
ll ss = 0;
sum[0] = 0;
for(int i = 0; i < n; i++){
ss+=w[i];
sum[i+1] = ss;
}
ll ans = 0;
vector <tuple<ll, ll, ll>> v;
set<tuple<ll, int, int>> vs;
for(int i = 0; i < n; i++){
vs.insert({h[i], in2[i]+1, in[i]});
}
for(auto tp: vs){
ll ls = 0;
ll hh, inx1, inx2;
tie(hh, inx1, inx2) = tp;
if(inx1 != 0) ls = max(ls, h[inx1-1]);
if(inx2 < n) ls = max(ls, h[inx2]);
v.pb({hh, sum[inx2] - sum[inx1], ls});
}
ll ls = 0;
for(auto tp: v){
ll a, b, c;
tie(a, b, c) = tp;
ans+=(ml(b)*sth(c, a));
ans%=m;
}
cout << ans << "\n";
}
Compilation message
fancyfence.cpp: In function 'int main()':
fancyfence.cpp:40:12: warning: unused variable 's' [-Wunused-variable]
40 | ll s = 0;
| ^
fancyfence.cpp:56:12: warning: unused variable 's' [-Wunused-variable]
56 | ll s = 0;
| ^
fancyfence.cpp:87:8: warning: unused variable 'ls' [-Wunused-variable]
87 | ll ls = 0;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
21 ms |
3972 KB |
Output is correct |
4 |
Correct |
30 ms |
6376 KB |
Output is correct |
5 |
Correct |
37 ms |
7528 KB |
Output is correct |
6 |
Correct |
34 ms |
5736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
896 KB |
Output is correct |
3 |
Correct |
15 ms |
3064 KB |
Output is correct |
4 |
Correct |
28 ms |
5864 KB |
Output is correct |
5 |
Correct |
31 ms |
5868 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
896 KB |
Output is correct |
4 |
Correct |
18 ms |
3080 KB |
Output is correct |
5 |
Correct |
28 ms |
5864 KB |
Output is correct |
6 |
Correct |
31 ms |
6000 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
6 ms |
1984 KB |
Output is correct |
9 |
Correct |
25 ms |
5368 KB |
Output is correct |
10 |
Correct |
60 ms |
14824 KB |
Output is correct |
11 |
Correct |
60 ms |
14820 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 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 |
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 |
19 ms |
3960 KB |
Output is correct |
12 |
Correct |
30 ms |
6376 KB |
Output is correct |
13 |
Correct |
36 ms |
7528 KB |
Output is correct |
14 |
Correct |
34 ms |
5736 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
3 ms |
896 KB |
Output is correct |
17 |
Correct |
14 ms |
3064 KB |
Output is correct |
18 |
Correct |
28 ms |
6000 KB |
Output is correct |
19 |
Correct |
31 ms |
5872 KB |
Output is correct |
20 |
Correct |
1 ms |
512 KB |
Output is correct |
21 |
Correct |
6 ms |
1984 KB |
Output is correct |
22 |
Correct |
26 ms |
5360 KB |
Output is correct |
23 |
Correct |
60 ms |
14824 KB |
Output is correct |
24 |
Correct |
60 ms |
14820 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
512 KB |
Output is correct |
28 |
Correct |
1 ms |
512 KB |
Output is correct |
29 |
Correct |
1 ms |
512 KB |
Output is correct |
30 |
Correct |
7 ms |
1984 KB |
Output is correct |
31 |
Correct |
7 ms |
1984 KB |
Output is correct |
32 |
Correct |
36 ms |
7412 KB |
Output is correct |
33 |
Correct |
38 ms |
7664 KB |
Output is correct |
34 |
Correct |
93 ms |
14696 KB |
Output is correct |
35 |
Correct |
82 ms |
14436 KB |
Output is correct |
36 |
Correct |
92 ms |
14824 KB |
Output is correct |
37 |
Correct |
94 ms |
14952 KB |
Output is correct |
38 |
Correct |
1 ms |
384 KB |
Output is correct |
39 |
Correct |
90 ms |
14568 KB |
Output is correct |
40 |
Correct |
74 ms |
14824 KB |
Output is correct |
41 |
Correct |
59 ms |
14952 KB |
Output is correct |
42 |
Correct |
59 ms |
14948 KB |
Output is correct |