#include <bits/stdc++.h>
#define int long long
#define double long double
#define x first
#define y second
#define pb push_back
using namespace std;
using pii=pair <int,int>;
using tii=pair <pii,int>;
using qii=pair <pii,pii>;
const int mod=1e9+7LL;
int h[100010],w[100010];
pii sp[20][100010];
int lg[100010];
pii qry(int ll,int rr){
int g=lg[rr-ll+1];
return min(sp[g][ll],sp[g][rr-(1<<g)+1]);
}
int l[100010],r[100010];
int idx[100010],rgl[100010],rgr[100010],curidx;
int dfs1(int tl,int tr){
if (tl>tr) return 0;
curidx++;
int nd=curidx;
idx[curidx]=qry(tl,tr).y;
rgl[curidx]=tl; rgr[curidx]=tr;
l[nd]=dfs1(tl,idx[nd]-1);
r[nd]=dfs1(idx[nd]+1,tr);
return nd;
}
int psw[100010];
int ans;
void dfs2(int cur){
int ch=(h[idx[cur]]+1)%mod*h[idx[cur]]%mod*(mod+1)/2%mod;
int cw=(psw[rgr[cur]]-psw[rgl[cur]-1]+1+mod)%mod*((psw[rgr[cur]]-psw[rgl[cur]-1]+mod)%mod)%mod*(mod+1)/2%mod;
if (l[cur]){
dfs2(l[cur]);
cw-=(psw[rgr[l[cur]]]-psw[rgl[l[cur]]-1]+1+mod)%mod*((psw[rgr[l[cur]]]-psw[rgl[l[cur]]-1]+mod)%mod)%mod*(mod+1)/2%mod;
cw%=mod;
cw+=mod; cw%=mod;
}
if (r[cur]){
dfs2(r[cur]);
cw-=(psw[rgr[r[cur]]]-psw[rgl[r[cur]]-1]+1+mod)%mod*((psw[rgr[r[cur]]]-psw[rgl[r[cur]]-1]+mod)%mod)%mod*(mod+1)/2%mod;
cw%=mod;
cw+=mod; cw%=mod;
}
ans+=ch*cw%mod;
ans%=mod;
}
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];
for (int i=1; i<=n; i++) sp[0][i]={h[i],i};
for (int j=1; (1<<j)<=n; j++){
for (int i=1; i+(1<<j)-1<=n; i++) sp[j][i]=min(sp[j-1][i],sp[j-1][i+(1<<(j-1))]);
}
lg[1]=0;
for (int i=2; i<=n; i++) lg[i]=lg[i/2]+1;
psw[0]=0;
for (int i=1; i<=n; i++) psw[i]=(psw[i-1]+w[i])%mod;
dfs1(1,n);
dfs2(1);
cout<<ans<<'\n';
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int t=1;
//cin>>t;
while (t--) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8528 KB |
Output is correct |
2 |
Correct |
3 ms |
20816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10576 KB |
Output is correct |
2 |
Correct |
2 ms |
12624 KB |
Output is correct |
3 |
Correct |
2 ms |
14672 KB |
Output is correct |
4 |
Correct |
2 ms |
10576 KB |
Output is correct |
5 |
Correct |
2 ms |
14672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8528 KB |
Output is correct |
2 |
Correct |
3 ms |
20956 KB |
Output is correct |
3 |
Correct |
15 ms |
35664 KB |
Output is correct |
4 |
Correct |
33 ms |
42896 KB |
Output is correct |
5 |
Correct |
28 ms |
40268 KB |
Output is correct |
6 |
Correct |
25 ms |
38536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
21072 KB |
Output is correct |
2 |
Correct |
6 ms |
28168 KB |
Output is correct |
3 |
Correct |
18 ms |
37980 KB |
Output is correct |
4 |
Correct |
35 ms |
44848 KB |
Output is correct |
5 |
Correct |
29 ms |
44880 KB |
Output is correct |
6 |
Correct |
1 ms |
6480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8528 KB |
Output is correct |
2 |
Correct |
3 ms |
21072 KB |
Output is correct |
3 |
Correct |
5 ms |
28240 KB |
Output is correct |
4 |
Correct |
17 ms |
38036 KB |
Output is correct |
5 |
Correct |
38 ms |
44888 KB |
Output is correct |
6 |
Correct |
31 ms |
45040 KB |
Output is correct |
7 |
Correct |
3 ms |
21072 KB |
Output is correct |
8 |
Correct |
6 ms |
28156 KB |
Output is correct |
9 |
Correct |
19 ms |
37960 KB |
Output is correct |
10 |
Correct |
30 ms |
44640 KB |
Output is correct |
11 |
Correct |
28 ms |
44880 KB |
Output is correct |
12 |
Correct |
1 ms |
6736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8528 KB |
Output is correct |
2 |
Correct |
3 ms |
20816 KB |
Output is correct |
3 |
Correct |
2 ms |
10712 KB |
Output is correct |
4 |
Correct |
2 ms |
12624 KB |
Output is correct |
5 |
Correct |
2 ms |
14672 KB |
Output is correct |
6 |
Correct |
2 ms |
10576 KB |
Output is correct |
7 |
Correct |
2 ms |
14992 KB |
Output is correct |
8 |
Correct |
3 ms |
21072 KB |
Output is correct |
9 |
Correct |
3 ms |
21072 KB |
Output is correct |
10 |
Correct |
3 ms |
21072 KB |
Output is correct |
11 |
Correct |
3 ms |
16720 KB |
Output is correct |
12 |
Correct |
3 ms |
18908 KB |
Output is correct |
13 |
Correct |
4 ms |
20816 KB |
Output is correct |
14 |
Correct |
4 ms |
20816 KB |
Output is correct |
15 |
Correct |
3 ms |
20816 KB |
Output is correct |
16 |
Correct |
1 ms |
6480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8528 KB |
Output is correct |
2 |
Correct |
3 ms |
20816 KB |
Output is correct |
3 |
Correct |
2 ms |
8528 KB |
Output is correct |
4 |
Correct |
3 ms |
20816 KB |
Output is correct |
5 |
Correct |
2 ms |
10576 KB |
Output is correct |
6 |
Correct |
2 ms |
12624 KB |
Output is correct |
7 |
Correct |
2 ms |
14672 KB |
Output is correct |
8 |
Correct |
2 ms |
10576 KB |
Output is correct |
9 |
Correct |
2 ms |
14672 KB |
Output is correct |
10 |
Correct |
3 ms |
20816 KB |
Output is correct |
11 |
Correct |
13 ms |
35576 KB |
Output is correct |
12 |
Correct |
25 ms |
42824 KB |
Output is correct |
13 |
Correct |
30 ms |
40264 KB |
Output is correct |
14 |
Correct |
22 ms |
38480 KB |
Output is correct |
15 |
Correct |
3 ms |
21072 KB |
Output is correct |
16 |
Correct |
5 ms |
28240 KB |
Output is correct |
17 |
Correct |
17 ms |
37984 KB |
Output is correct |
18 |
Correct |
29 ms |
44880 KB |
Output is correct |
19 |
Correct |
32 ms |
44872 KB |
Output is correct |
20 |
Correct |
3 ms |
21072 KB |
Output is correct |
21 |
Correct |
6 ms |
28240 KB |
Output is correct |
22 |
Correct |
17 ms |
37968 KB |
Output is correct |
23 |
Correct |
37 ms |
44840 KB |
Output is correct |
24 |
Correct |
31 ms |
44880 KB |
Output is correct |
25 |
Correct |
2 ms |
16904 KB |
Output is correct |
26 |
Correct |
2 ms |
18948 KB |
Output is correct |
27 |
Correct |
3 ms |
20816 KB |
Output is correct |
28 |
Correct |
3 ms |
20816 KB |
Output is correct |
29 |
Correct |
3 ms |
20816 KB |
Output is correct |
30 |
Correct |
5 ms |
27472 KB |
Output is correct |
31 |
Correct |
5 ms |
27484 KB |
Output is correct |
32 |
Correct |
14 ms |
34640 KB |
Output is correct |
33 |
Correct |
14 ms |
35064 KB |
Output is correct |
34 |
Correct |
25 ms |
38480 KB |
Output is correct |
35 |
Correct |
26 ms |
38480 KB |
Output is correct |
36 |
Correct |
25 ms |
38480 KB |
Output is correct |
37 |
Correct |
25 ms |
38480 KB |
Output is correct |
38 |
Correct |
1 ms |
6480 KB |
Output is correct |
39 |
Correct |
27 ms |
38480 KB |
Output is correct |
40 |
Correct |
26 ms |
38736 KB |
Output is correct |
41 |
Correct |
29 ms |
39760 KB |
Output is correct |
42 |
Correct |
31 ms |
44872 KB |
Output is correct |