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>
#define mp make_pair
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define xx first
#define yy second
#define srt(a) sort(a.begin(),a.end());
#define srtg(a,int) sort(a.begin(),a.end(),greater<int>())
#define lb(a,x) lower_bound(a.begin(),a.end(),x)
#define up(a,x) upper_bound(a.begin(),a.end(),x)
#define fnd(a,x) find(a.begin(),a.end(),x)
#define vstart auto startt=chrono::system_clock::now()
#define vend auto endd=chrono::system_clock::now()
#define vvreme chrono::duration<double> vremee=endd-startt
#define ios ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef string str;
const int maxn=1e5+10;
const int mod=1e9+7;
int add(int a,int b){
return (a+b)%mod;
}
int mul(int a,int b){
return ll(a)*ll(b)%ll(mod);
}
int h[maxn];
int w[maxn];
int sta(int a){
a++;
if(a&1) return mul(a,(a-1)/2);
else return mul(a/2,a-1);
}
void solve(){
int n;
cin>>n;
for(int i=0;i<n;i++) cin>>h[i];
for(int i=0;i<n;i++) cin>>w[i];
stack<pii> stek;
int sum=0;
int res=0;
for(int i=0;i<n;i++){
int duz=w[i];
int vis=h[i];
while(!stek.empty() && stek.top().xx>=vis){
auto [a,b]=stek.top();
sum=add(sum,-mul(sta(a),b));
duz=add(duz,b);
stek.pop();
}
stek.push(mp(vis,duz));
sum=add(sum,mul(sta(vis),duz));
res=add(res,mul(sum,w[i]));
res=add(res,-mul(sta(vis),mul(w[i],w[i])));
res=add(res,mul(sta(vis),sta(w[i])));
}
cout<<(res+mod)%mod;
}
int main(){
ios;
int t=1;
//cin>>t;
while(t--) solve();
return 0;
}
# | 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... |