#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> pii;
const LL MAXN=1e5+5,MOD=1e9+7;
LL h[MAXN],w[MAXN],tree[4*MAXN],par[MAXN],sz[MAXN];
bool ada[MAXN];
pii arr[MAXN];
LL find(LL x){
if(par[x]==x)return par[x];
return par[x]=find(par[x]);
}
void join(LL u,LL v){
LL repu=find(u),repv=find(v);
par[repu]=repv;
sz[repv]+=sz[repu];
}
void build(LL now,LL L,LL R){
if(L==R){
tree[now]=w[L];
return;
}
LL mid=(L+R)/2;
build(2*now,L,mid);
build(2*now+1,mid+1,R);
tree[now]=tree[2*now]+tree[2*now+1];
}
LL query(LL now,LL L,LL R,LL x,LL y){
if(L>=x && R<=y)return tree[now];
if(L>y || R<x)return 0;
LL mid=(L+R)/2;
return query(2*now,L,mid,x,y)+query(2*now+1,mid+1,R,x,y);
}
LL C(LL x){
LL p=x%MOD,q=(x-1)%MOD;
return (((p*q)%MOD)*500000004)%MOD;
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
LL n,ans=0;
cin >> n;
for(LL i=1;i<=n;i++)cin >> h[i];
for(LL i=1;i<=n;i++){
cin >> w[i];
LL ret=(C(h[i]+1)*C(w[i]+1))%MOD;
ans=(ans+ret)%MOD;
}
build(1,1,n);
for(LL i=1;i<=n;i++){
h[i]++;
arr[i]={h[i],i};
par[i]=i;
sz[i]=1;
}
sort(arr+1,arr+n+1);
reverse(arr+1,arr+n+1);
for(LL i=1;i<=n;i++){
pii isi=arr[i];
LL kiri=0,kanan=0;
if(ada[isi.second-1]){
kiri=sz[find(isi.second-1)];
join(isi.second,isi.second-1);
}
if(ada[isi.second+1]){
kanan=sz[find(isi.second+1)];
join(isi.second,isi.second+1);
}
LL totL=query(1,1,n,isi.second-kiri,isi.second)%MOD,totR=query(1,1,n,isi.second,isi.second+kanan)%MOD;
LL sum=(((totL*totR)%MOD)-((w[isi.second]*w[isi.second])%MOD)+MOD)%MOD;
ans=(ans+((sum*C(isi.first))%MOD))%MOD;
ada[isi.second]=true;
}
cout << ans << '\n';
return 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 |
384 KB |
Output is correct |
2 |
Correct |
0 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 |
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 |
31 ms |
4352 KB |
Output is correct |
4 |
Correct |
60 ms |
8312 KB |
Output is correct |
5 |
Correct |
65 ms |
8448 KB |
Output is correct |
6 |
Correct |
74 ms |
8320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
7 ms |
1280 KB |
Output is correct |
3 |
Correct |
30 ms |
4736 KB |
Output is correct |
4 |
Correct |
61 ms |
9080 KB |
Output is correct |
5 |
Correct |
69 ms |
9216 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 |
512 KB |
Output is correct |
3 |
Correct |
8 ms |
1280 KB |
Output is correct |
4 |
Correct |
30 ms |
4736 KB |
Output is correct |
5 |
Correct |
60 ms |
9084 KB |
Output is correct |
6 |
Correct |
65 ms |
9336 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
6 ms |
1280 KB |
Output is correct |
9 |
Correct |
33 ms |
4856 KB |
Output is correct |
10 |
Correct |
59 ms |
8952 KB |
Output is correct |
11 |
Correct |
60 ms |
9080 KB |
Output is correct |
12 |
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 |
2 ms |
512 KB |
Output is correct |
3 |
Correct |
0 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 |
512 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 |
1 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 |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
32 ms |
4352 KB |
Output is correct |
12 |
Correct |
60 ms |
8320 KB |
Output is correct |
13 |
Correct |
63 ms |
8312 KB |
Output is correct |
14 |
Correct |
63 ms |
8316 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
6 ms |
1280 KB |
Output is correct |
17 |
Correct |
30 ms |
4736 KB |
Output is correct |
18 |
Correct |
60 ms |
9208 KB |
Output is correct |
19 |
Correct |
63 ms |
9208 KB |
Output is correct |
20 |
Correct |
1 ms |
512 KB |
Output is correct |
21 |
Correct |
6 ms |
1280 KB |
Output is correct |
22 |
Correct |
31 ms |
4736 KB |
Output is correct |
23 |
Correct |
62 ms |
8956 KB |
Output is correct |
24 |
Correct |
60 ms |
9088 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 |
8 ms |
1280 KB |
Output is correct |
31 |
Correct |
8 ms |
1408 KB |
Output is correct |
32 |
Correct |
42 ms |
4736 KB |
Output is correct |
33 |
Correct |
42 ms |
4736 KB |
Output is correct |
34 |
Correct |
93 ms |
8952 KB |
Output is correct |
35 |
Correct |
93 ms |
8952 KB |
Output is correct |
36 |
Correct |
99 ms |
9208 KB |
Output is correct |
37 |
Correct |
99 ms |
9208 KB |
Output is correct |
38 |
Correct |
0 ms |
384 KB |
Output is correct |
39 |
Correct |
96 ms |
9196 KB |
Output is correct |
40 |
Correct |
84 ms |
9080 KB |
Output is correct |
41 |
Correct |
77 ms |
9208 KB |
Output is correct |
42 |
Correct |
62 ms |
9208 KB |
Output is correct |