#include<bits/stdc++.h>
#define pii pair <int, int>
#define fr first
#define sc second
#define N 100010
#define inf 1e18
#define int long long
using namespace std;
int n;
int h[N], w[N];
int pref[N];
int mod = 1e9+7;
struct Segtree{
pair <int,int> tree[4*N];
pair <int,int> join(pii a, pii b){
if(a.fr < b.fr) return a;
if(a.fr > b.fr) return b;
if(a.sc < b.sc) return a;
return b;
}
void build(int l, int r, int node){
if(l == r){
tree[node] = {h[l], l};
return;
}
int mid = (l+r)/2;
build(l, mid, 2*node);
build(mid+1, r, 2*node+1);
tree[node] = join(tree[2*node], tree[2*node+1]);
return;
}
pair <int, int> query(int l, int r, int tl, int tr, int node){
if(tl > r or tr < l) return {inf, inf};
if(l <= tl and tr <= r) return tree[node];
int mid = (tl+tr)/2;
return join(query(l, r, tl, mid, 2*node), query(l, r, mid+1, tr, 2*node+1));
}
} seg;
long long binpow(long long a, long long b, long long m) {
a %= m;
long long res = 1;
while (b > 0) {
if (b & (1ll))
res = res * a % m;
a = a * a % m;
b >>= (1ll);
}
return res;
}
int query(int l, int r, int minant){
int c = pref[r] - pref[l-1];
c %= mod;
c += mod;
c %= mod;
int a = seg.query(l, r, 1, n, 1).fr;
a %= mod;
int res = (c-1)*(a-1);
res %= mod;
res *= c;
res %= mod;
res *= a;
res %= mod;
res *= binpow(4, mod-2, mod);
res %= mod;
int aux = (a+c-2)*a;
aux %= mod;
aux *= c;
aux %= mod;
aux *= binpow(2, mod-2, mod);
aux %= mod;
res += aux;
res %= mod;
res += ((a*c) % mod);
res %= mod;
a = minant % mod;
int res2 = (c-1)*(a-1);
res2 %= mod;
res2 *= c;
res2 %= mod;
res2 *= a;
res2 %= mod;
res2 *= binpow(4, mod-2, mod);
res2 %= mod;
aux = (a+c-2)*a;
aux %= mod;
aux *= c;
aux %= mod;
aux *= binpow(2, mod-2, mod);
aux %= mod;
res2 += aux;
res2 %= mod;
res2 += ((a*c) % mod);
res2 %= mod;
// cout << res2 << "\n";
res = res-res2;
res %= mod;
res += mod;
res %= mod;
// cout << l << ' ' << r << ' ' << minant << ": " << res << "\n";
return res % mod;
}
int32_t main(){
cin >> n;
for(int i = 1;i <= n;i++){
cin >> h[i];
}
for(int i = 1;i <= n;i++){
cin >> w[i];
}
seg.build(1, n, 1);
queue <array <int,4>> q;
q.push({1, n, 0, 0});
for(int i = 1;i <= n;i++){
pref[i] = pref[i-1] + w[i];
pref[i] %= mod;
}
int res = 0;
while(!q.empty()){
auto [l, r, minant, aux] = q.front();
q.pop();
if(aux == 0){
res += query(l, r, minant); res %= mod;
auto [minn, pos] = seg.query(l, r, 1, n, 1);
if(pos != l) q.push({l, pos-1, minn, 0});
if(pos != r) q.push({pos+1, r, minn, 1});
}
else{
auto [minn, pos] = seg.query(l, r, 1, n, 1);
if(minant == minn){
if(pos != l) q.push({l, pos-1, minn, 0});
if(pos != r) q.push({pos+1, r, minn, 1});
}
else{
q.push({l, r, minant, 0});
}
}
}
// cout << binpow(4, mod-1, mod);
cout << res << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
3 ms |
380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
224 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
47 ms |
3468 KB |
Output is correct |
4 |
Correct |
94 ms |
6664 KB |
Output is correct |
5 |
Correct |
93 ms |
6740 KB |
Output is correct |
6 |
Correct |
70 ms |
6640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
7 ms |
980 KB |
Output is correct |
3 |
Correct |
35 ms |
3532 KB |
Output is correct |
4 |
Correct |
67 ms |
6740 KB |
Output is correct |
5 |
Correct |
69 ms |
6696 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
7 ms |
980 KB |
Output is correct |
4 |
Correct |
35 ms |
3524 KB |
Output is correct |
5 |
Correct |
68 ms |
6644 KB |
Output is correct |
6 |
Correct |
70 ms |
6688 KB |
Output is correct |
7 |
Correct |
4 ms |
340 KB |
Output is correct |
8 |
Correct |
24 ms |
1264 KB |
Output is correct |
9 |
Correct |
73 ms |
4536 KB |
Output is correct |
10 |
Correct |
245 ms |
8572 KB |
Output is correct |
11 |
Correct |
247 ms |
8676 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
3 ms |
380 KB |
Output is correct |
3 |
Correct |
0 ms |
308 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
312 KB |
Output is correct |
10 |
Correct |
3 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
3 ms |
340 KB |
Output is correct |
14 |
Correct |
3 ms |
340 KB |
Output is correct |
15 |
Correct |
3 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
3 ms |
380 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
3 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
304 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 |
55 ms |
4116 KB |
Output is correct |
12 |
Correct |
78 ms |
7908 KB |
Output is correct |
13 |
Correct |
93 ms |
8008 KB |
Output is correct |
14 |
Correct |
71 ms |
7904 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
9 ms |
1236 KB |
Output is correct |
17 |
Correct |
34 ms |
4504 KB |
Output is correct |
18 |
Correct |
69 ms |
8620 KB |
Output is correct |
19 |
Correct |
71 ms |
8784 KB |
Output is correct |
20 |
Correct |
3 ms |
340 KB |
Output is correct |
21 |
Correct |
24 ms |
1248 KB |
Output is correct |
22 |
Correct |
71 ms |
4504 KB |
Output is correct |
23 |
Correct |
241 ms |
8468 KB |
Output is correct |
24 |
Correct |
245 ms |
8568 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
3 ms |
340 KB |
Output is correct |
28 |
Correct |
3 ms |
340 KB |
Output is correct |
29 |
Correct |
3 ms |
340 KB |
Output is correct |
30 |
Correct |
25 ms |
1296 KB |
Output is correct |
31 |
Correct |
26 ms |
1276 KB |
Output is correct |
32 |
Correct |
118 ms |
4412 KB |
Output is correct |
33 |
Correct |
127 ms |
4528 KB |
Output is correct |
34 |
Correct |
254 ms |
8792 KB |
Output is correct |
35 |
Correct |
238 ms |
8500 KB |
Output is correct |
36 |
Correct |
257 ms |
8992 KB |
Output is correct |
37 |
Correct |
259 ms |
8964 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
251 ms |
8860 KB |
Output is correct |
40 |
Correct |
262 ms |
8744 KB |
Output is correct |
41 |
Correct |
255 ms |
8692 KB |
Output is correct |
42 |
Correct |
240 ms |
8664 KB |
Output is correct |