#include<iostream>
#include<stdio.h>
#include<math.h>
#include<map>
#include<string>
#include<algorithm>
#include<vector>
#include<string.h>
#include<utility>
#include<set>
#include<cmath>
#include<queue>
#include<deque>
#include<functional>
#include<stack>
#include<limits.h>
#include<iomanip>
#include<unordered_map>
using namespace std;
#define MOD 1000000007
#define int long long
#define endl '\n'
int cal(int a, int b){
int f=(a*(a+1))%MOD*((MOD+1)/2)%MOD;
int s=(b*(b+1))%MOD*((MOD+1)/2)%MOD;
return (f*s)%MOD;
}
void solve(){
int n; cin>>n;
vector<int> h(n+1),w(n+1);
vector<pair<int,int> > p(n+1);
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++) p[i]=make_pair(h[i],w[i]);
sort(p.begin(),p.end());
vector<int> up(n+1),dw(n+2);
for(int i=1; i<=n; i++) up[i]=(up[i-1]+p[i].second)%MOD;
for(int i=n; i>=1; i--) dw[i]=(dw[i+1]+p[i].second)%MOD;
stack<int> f;
f.push(n+1);
int ans=0;
for(int i=n; i>=1; i--){
int pos=i+1;
while(f.size()>1&&p[f.top()].first>=p[i].first){
f.pop();
ans=(ans-cal(dw[pos]-dw[f.top()],p[i].first)+MOD)%MOD;
pos=f.top();
}
ans=(ans+cal(dw[i]-dw[f.top()],p[i].first))%MOD;
f.push(i);
}
cout<<ans;
}
signed main(){
// freopen("hps.in", "r", stdin);
// freopen("hps.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
// cin>>t;
while(t--){
solve();
cout<<endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 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 |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
860 KB |
Output is correct |
3 |
Correct |
14 ms |
2652 KB |
Output is correct |
4 |
Correct |
32 ms |
5128 KB |
Output is correct |
5 |
Correct |
27 ms |
4956 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
1112 KB |
Output is correct |
4 |
Correct |
14 ms |
2808 KB |
Output is correct |
5 |
Correct |
27 ms |
5148 KB |
Output is correct |
6 |
Correct |
27 ms |
4944 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
12 ms |
2648 KB |
Output is correct |
10 |
Correct |
23 ms |
4956 KB |
Output is correct |
11 |
Correct |
26 ms |
6660 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |