#include<bits/stdc++.h>
#define endl '\n'
#define fr first
#define sc second
#define ll long long int
const int MAXN = 1e5;
const int MAXL = 20;
const ll MOD = 1e9 + 7;
inline std::pair<int, int> my_min(const std::pair<int, int> &a, const std::pair<int, int> &b){
if(a.fr < b.fr) return a;
if(a.fr > b.fr) return b;
if(a.sc < b.sc) return a;
return b;
}
inline ll mult(ll a, ll b){
return (a * b) % MOD;
}
inline ll fp(ll b, ll p){
if(p == 0) return 1LL;
if(p == 1) return b;
if(p & 1) return mult(b, fp(b, p - 1));
ll aux = fp(b, p / 2);
return mult(aux, aux);
}
inline ll divi(ll a, ll b){
return mult(a, fp(b, MOD - 2));
}
inline ll f(ll n){
n = (n + MOD) % MOD;
return divi(mult(n, n + 1), 2);
}
int lg2[MAXN + 5];
ll N, h[MAXN + 5], w[MAXN + 5], prefw[MAXN + 5];
std::pair<int, int> sparse[MAXN + 5][MAXL + 1];
std::pair<int, int> get(int l, int r){
int j = lg2[r - l + 1];
return my_min(sparse[l][j], sparse[r - (1 << j) + 1][j]);
}
ll rec(int l = 1, int r = N){
if(l > r) return 0;
int minh = get(l, r).fr, id = l;
ll my_ans = 0, other_ans = 0;
my_ans = mult(f(minh), f(prefw[r] - prefw[l - 1]));
while(true){
auto [myh, myid] = get(id, r);
if(myh != minh){
my_ans = (my_ans - mult(f(minh), f(prefw[r] - prefw[id - 1])) + MOD) % MOD;
other_ans += rec(id, r);
break;
}
else{
my_ans = (my_ans - mult(f(minh), f(prefw[myid - 1] - prefw[id - 1])) + MOD) % MOD;
other_ans += rec(id, myid - 1);
id = myid + 1;
}
}
my_ans %= MOD;
other_ans %= MOD;
return (my_ans + other_ans) % MOD;
}
inline void init(){
lg2[1] = 0;
for(int i = 2; i <= MAXN; ++ i){
lg2[i] = lg2[i / 2] + 1;
}
}
int main(){
std::ios_base::sync_with_stdio(false); std::cin.tie(NULL);
init();
std::cin >> N;
for(int i = 1; i <= N; ++ i){
std::cin >> h[i];
sparse[i][0] = {h[i], i};
}
for(int l = 1; l < MAXL; ++ l){
for(int i = 1; i + (1 << l) - 1 <= N; ++ i){
sparse[i][l] = my_min(sparse[i][l - 1], sparse[i + (1 << (l - 1))][l - 1]);
}
}
for(int i = 1; i <= N; ++ i){
std::cin >> w[i];
prefw[i] = prefw[i - 1] + w[i];
}
std::cout << rec() << endl;
}
/**
*/
Compilation message
fancyfence.cpp: In function 'long long int rec(int, int)':
fancyfence.cpp:55:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
55 | auto [myh, myid] = get(id, r);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
716 KB |
Output is correct |
2 |
Correct |
2 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
708 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
852 KB |
Output is correct |
3 |
Correct |
29 ms |
10704 KB |
Output is correct |
4 |
Correct |
57 ms |
20612 KB |
Output is correct |
5 |
Correct |
60 ms |
20648 KB |
Output is correct |
6 |
Correct |
56 ms |
20612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
852 KB |
Output is correct |
2 |
Correct |
6 ms |
2780 KB |
Output is correct |
3 |
Correct |
24 ms |
10980 KB |
Output is correct |
4 |
Correct |
53 ms |
21380 KB |
Output is correct |
5 |
Correct |
53 ms |
21532 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
720 KB |
Output is correct |
2 |
Correct |
1 ms |
860 KB |
Output is correct |
3 |
Correct |
5 ms |
2776 KB |
Output is correct |
4 |
Correct |
25 ms |
10988 KB |
Output is correct |
5 |
Correct |
57 ms |
21440 KB |
Output is correct |
6 |
Correct |
53 ms |
21476 KB |
Output is correct |
7 |
Correct |
2 ms |
980 KB |
Output is correct |
8 |
Correct |
10 ms |
3676 KB |
Output is correct |
9 |
Correct |
33 ms |
12892 KB |
Output is correct |
10 |
Correct |
100 ms |
30580 KB |
Output is correct |
11 |
Correct |
95 ms |
30728 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
728 KB |
Output is correct |
2 |
Correct |
2 ms |
852 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
720 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
852 KB |
Output is correct |
10 |
Correct |
2 ms |
984 KB |
Output is correct |
11 |
Correct |
1 ms |
724 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
2 ms |
852 KB |
Output is correct |
14 |
Correct |
1 ms |
852 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
716 KB |
Output is correct |
2 |
Correct |
2 ms |
852 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
716 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
720 KB |
Output is correct |
10 |
Correct |
1 ms |
852 KB |
Output is correct |
11 |
Correct |
29 ms |
10712 KB |
Output is correct |
12 |
Correct |
55 ms |
20544 KB |
Output is correct |
13 |
Correct |
60 ms |
20616 KB |
Output is correct |
14 |
Correct |
53 ms |
20556 KB |
Output is correct |
15 |
Correct |
2 ms |
980 KB |
Output is correct |
16 |
Correct |
5 ms |
2768 KB |
Output is correct |
17 |
Correct |
24 ms |
11092 KB |
Output is correct |
18 |
Correct |
52 ms |
21372 KB |
Output is correct |
19 |
Correct |
53 ms |
21532 KB |
Output is correct |
20 |
Correct |
2 ms |
980 KB |
Output is correct |
21 |
Correct |
9 ms |
3668 KB |
Output is correct |
22 |
Correct |
35 ms |
12988 KB |
Output is correct |
23 |
Correct |
96 ms |
30588 KB |
Output is correct |
24 |
Correct |
107 ms |
30796 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
2 ms |
856 KB |
Output is correct |
28 |
Correct |
1 ms |
852 KB |
Output is correct |
29 |
Correct |
2 ms |
852 KB |
Output is correct |
30 |
Correct |
9 ms |
2792 KB |
Output is correct |
31 |
Correct |
9 ms |
2772 KB |
Output is correct |
32 |
Correct |
44 ms |
11004 KB |
Output is correct |
33 |
Correct |
47 ms |
11092 KB |
Output is correct |
34 |
Correct |
94 ms |
21240 KB |
Output is correct |
35 |
Correct |
94 ms |
21236 KB |
Output is correct |
36 |
Correct |
108 ms |
21384 KB |
Output is correct |
37 |
Correct |
100 ms |
21472 KB |
Output is correct |
38 |
Correct |
1 ms |
724 KB |
Output is correct |
39 |
Correct |
91 ms |
21324 KB |
Output is correct |
40 |
Correct |
92 ms |
21524 KB |
Output is correct |
41 |
Correct |
96 ms |
23316 KB |
Output is correct |
42 |
Correct |
98 ms |
30796 KB |
Output is correct |