#include <bits/stdc++.h>
#define MAX 100001
#define MOD 1000000007
using namespace std;
typedef long long ll;
ll n,x,y,z,w[MAX],h[MAX],a[MAX];
struct DSU{
vector<int> e;
DSU(int N){e.resize(N+1,-1);}
int get(int x){return (e[x]<0) ? x : e[x]=get(e[x]);}
int size(int x){return w[get(x)];}
bool unite(int x,int y){
x=get(x);
y=get(y);
if(x==y) return false;
if(e[x]>e[y]) swap(x,y);
e[x]+=e[y]; w[x]+=w[y]; w[x]%=MOD; e[y]=x;
return true;
}
};
ll powmod(ll x,ll y,ll mod){
if(y==0){
return 1;
}
ll res=1;
x%=mod;
while(y>0){
if(y%2){
res*=x;
res%=mod;
}
y/=2;
x*=x;
x%=mod;
}
return res;
}
ll inv(ll n,ll mod){
return powmod(n,mod-2,mod);
}
ll mul(ll x,ll y){
return (x*y)%MOD;
}
ll f(ll W,ll H){
ll res=1;
res=mul(res,W);
res=mul(res,W+1);
res=mul(res,H);
res=mul(res,H+1);
res=mul(res,z);
return res;
}
void solve(){
cin >> n;
z=inv(4,MOD);
for(int i=0;i<n;i++){
cin >> h[i];
}
for(int i=0;i<n;i++){
cin >> w[i];
}
vector<array<ll,3>> c;
for(int i=0;i<n;i++){
c.push_back({h[i],w[i],(ll)i});
}
sort(c.rbegin(),c.rend());
ll res=0;
DSU dsu(n);
for(auto x:c){
ll cnt=x[1];
a[x[2]]=1;
if(x[2]-1>=0 && a[x[2]-1]){
y=dsu.size(x[2]-1);
res-=f(y,x[0]);
res=(res+MOD)%MOD;
cnt+=y;
cnt%=MOD;
dsu.unite(x[2],x[2]-1);
}
if(x[2]+1<n && a[x[2]+1]){
y=dsu.size(x[2]+1);
res-=f(y,x[0]);
res=(res+MOD)%MOD;
cnt+=y;
cnt%=MOD;
dsu.unite(x[2],x[2]+1);
}
res+=f(cnt,x[0]);
res%=MOD;
}
cout << res << "\n";
}
int main(){
//freopen("input1.txt","r",stdin);
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2488 KB |
Output is correct |
2 |
Correct |
2 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2484 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
28 ms |
4644 KB |
Output is correct |
4 |
Correct |
56 ms |
6924 KB |
Output is correct |
5 |
Correct |
58 ms |
8388 KB |
Output is correct |
6 |
Correct |
57 ms |
6800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
8 ms |
3100 KB |
Output is correct |
3 |
Correct |
42 ms |
5096 KB |
Output is correct |
4 |
Correct |
76 ms |
9220 KB |
Output is correct |
5 |
Correct |
76 ms |
7832 KB |
Output is correct |
6 |
Correct |
0 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2512 KB |
Output is correct |
3 |
Correct |
9 ms |
3116 KB |
Output is correct |
4 |
Correct |
38 ms |
5072 KB |
Output is correct |
5 |
Correct |
76 ms |
7624 KB |
Output is correct |
6 |
Correct |
76 ms |
7624 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
7 ms |
3100 KB |
Output is correct |
9 |
Correct |
36 ms |
5076 KB |
Output is correct |
10 |
Correct |
61 ms |
7624 KB |
Output is correct |
11 |
Correct |
62 ms |
8648 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2492 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
2 ms |
2392 KB |
Output is correct |
10 |
Correct |
2 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
2 ms |
2500 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2488 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
2 ms |
2392 KB |
Output is correct |
11 |
Correct |
28 ms |
4564 KB |
Output is correct |
12 |
Correct |
56 ms |
8388 KB |
Output is correct |
13 |
Correct |
57 ms |
8136 KB |
Output is correct |
14 |
Correct |
56 ms |
7876 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
8 ms |
3136 KB |
Output is correct |
17 |
Correct |
36 ms |
5120 KB |
Output is correct |
18 |
Correct |
75 ms |
7624 KB |
Output is correct |
19 |
Correct |
76 ms |
8904 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
7 ms |
3116 KB |
Output is correct |
22 |
Correct |
36 ms |
5316 KB |
Output is correct |
23 |
Correct |
61 ms |
8904 KB |
Output is correct |
24 |
Correct |
63 ms |
8132 KB |
Output is correct |
25 |
Correct |
1 ms |
2392 KB |
Output is correct |
26 |
Correct |
2 ms |
2392 KB |
Output is correct |
27 |
Correct |
1 ms |
2512 KB |
Output is correct |
28 |
Correct |
1 ms |
2392 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
8 ms |
3140 KB |
Output is correct |
31 |
Correct |
7 ms |
3100 KB |
Output is correct |
32 |
Correct |
36 ms |
4812 KB |
Output is correct |
33 |
Correct |
36 ms |
5076 KB |
Output is correct |
34 |
Correct |
69 ms |
7316 KB |
Output is correct |
35 |
Correct |
69 ms |
8136 KB |
Output is correct |
36 |
Correct |
74 ms |
8476 KB |
Output is correct |
37 |
Correct |
73 ms |
8972 KB |
Output is correct |
38 |
Correct |
1 ms |
2392 KB |
Output is correct |
39 |
Correct |
81 ms |
8712 KB |
Output is correct |
40 |
Correct |
87 ms |
8840 KB |
Output is correct |
41 |
Correct |
73 ms |
8476 KB |
Output is correct |
42 |
Correct |
69 ms |
7700 KB |
Output is correct |