This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
const long long maxn=100000+10;
long long h[maxn],w[maxn],n,vis[maxn],mod=1e9+7;
void vorod(){
cin>>n;
for(long long i=0;i<n;i++){
cin>>h[i];
}
for(long long i=0;i<n;i++){
cin>>w[i];
}
}
long long mainres=0;
long long mypow(long long m,long long y){
if(y==0){
return 1;
}
long long p=mypow(m,(y>>1));
p*=p;
p%=mod;
if(y&1){
p*=m;
p%=mod;
}
return p;
}
long long c2(long long a){
return (a*(a-1)/2)%mod;
}
struct dsu{
long long par[maxn],res,wh[maxn];
dsu(){
res=1;
}
void ins(long long u){
par[u]=u;
wh[u]=w[u];
res*=c2(w[u]+1);
res%=mod;
}
long long root(long long u){
if(par[u]==u){
return u;
}
return par[u]=root(par[u]);
}
void con(long long u,long long v){
long long rootu=root(u),rootv=root(v);
if(rootu!=rootv){
par[rootv]=rootu;
res*=mypow(c2(wh[rootu]+1),mod-2);
res%=mod;
res*=mypow(c2(wh[rootv]+1),mod-2);
res%=mod;
wh[rootu]+=wh[rootv];
res*=c2(wh[rootu]+1);
res%=mod;
}
}
}ds;
void solve(){
vector<pair<long long,long long>>sor;
for(long long i=0;i<n;i++){
sor.push_back(make_pair(h[i],i));
}
sort(sor.begin(),sor.end());
long long last=sor.back().first;
while((long long)sor.size()>0){
mainres+=ds.res*((mod+mod+c2(last+1)-c2(sor.back().first+1))%mod)%mod;
mainres%=mod;
auto x=sor.back();
sor.pop_back();
vis[x.second]=1;
ds.ins(x.second);
if(vis[x.second+1]==1){
ds.con(x.second,x.second+1);
}
if(vis[x.second-1]==1){
ds.con(x.second,x.second-1);
}
last=x.first;
}
mainres+=ds.res*(c2(last+1))%mod;
mainres%=mod;
}
void khor(){
cout<<mainres<<"\n";
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("inp.txt","r",stdin);
vorod();
solve();
khor();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |