#include<bits/stdc++.h>
#define endl '\n'
#define fi first
#define se second
using namespace std;
const int MAXN=1e5+5;
const int MOD=1e9+7;
int n;
int h[MAXN];
int w[MAXN];
long long pref[MAXN];
int ord[MAXN];
bool used[MAXN];
vector<pair<int,int> > ev;
pair<int,int> rng[MAXN];
int ans=0;
int rect(long long w,long long h)
{
w%=MOD;
h%=MOD;
return ((w*(w+1)/2)%MOD*(h*(h+1)/2)%MOD)%MOD;
}
void sum(int &a,int val)
{
a+=val;
if(a>=MOD) a-=MOD;
}
void subtr(int &a,int val)
{
a-=val;
if(a<0) a+=MOD;
}
void addL(int ind)
{
ev.push_back({rng[ind-1].fi, ind});
long long d=pref[ind-1]-pref[rng[ind-1].fi-1];
subtr(ans, rect(d,h[ind]));
}
void changeRngL(int ind)
{
int l=rng[ind-1].fi;
rng[l]={rng[l].fi, rng[ind].se};
rng[ind]={rng[l].fi, rng[ind].se};
}
void addR(int ind)
{
ev.push_back({ind, rng[ind+1].se});
long long d=pref[rng[ind+1].se]-pref[ind];
subtr(ans, rect(d,h[ind]));
}
void changeRngR(int ind)
{
int r=rng[ind+1].se;
rng[r]={rng[ind].fi, rng[r].se};
rng[ind]={rng[ind].fi, rng[r].se};
}
void calc(int h)
{
for(auto cur: ev)
{
long long d=pref[cur.se]-pref[cur.fi-1];
sum(ans, rect(d,h));
}
ev.clear();
}
bool cmp(int x,int y)
{
return h[x]>h[y];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
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++) pref[i]=pref[i-1]+w[i];
for(int i=1;i<=n;i++) rng[i]={i,i};
iota(ord,ord+n,1);
sort(ord,ord+n,cmp);
for(int i=0;i<n;i++)
{
if(i>0 && h[ord[i]]<h[ord[i-1]]) calc(h[ord[i-1]]);
int ind=ord[i];
pair<int,int> last({-1,-1});
if(!ev.empty()) last=ev.back();
if(used[ind-1])
{
if(ind-1==last.se) last.se++, ev.back().se++;
else addL(ind);
changeRngL(ind);
}
if(used[ind+1])
{
if(last.se==ind)
{
last.se=rng[ind+1].se; ev.back().se=rng[ind+1].se;
long long d=pref[rng[ind+1].se]-pref[ind];
subtr(ans, rect(d,w[ind]));
}
else addR(ind);
changeRngR(ind);
}
if(!used[ind-1] && !used[ind+1]) sum(ans, rect(w[ind], h[ind]));
used[ind]=1;
}
calc(h[ord[n-1]]);
cout<<ans<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
472 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |