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 vi vector<int>
#define vll vector<long long int>
#define vpii vector<pair<int,int>>
#define vpll vector<pair<long long int, long long int>>
#define pb push_back
#define f0r(i,n) for(int i = 0;i<n;i++)
using namespace std;
typedef long long int ll;
const int mod = 1e9 + 7;
const int mxn = 1e5 + 5;
const int lg = floor(log2(mxn));
ll w[mxn];
ll h[mxn];
pair<ll,ll> minh[lg][mxn];
ll c2(ll x){
return x * (x+1) / 2 % mod;
}
ll ans(ll w, ll h){
return c2(w) * c2(h) % mod;
}
pair<ll,ll> minq(int l, int r){
int sz = r - l + 1;
int curlg = floor(log2(sz));
ll se;
if(minh[curlg][l].first < minh[curlg][r - (int)pow(2, curlg) + 1].first){
se = minh[curlg][l].second;
}
else se = minh[curlg][r - (int)pow(2, curlg) + 1].second;
return {min(minh[curlg][l].first, minh[curlg][r - (int)pow(2, curlg) + 1].first), se};
}
ll answer = 0;
ll ps[mxn];
ll sumw(int l, int r){
if(r < l)return 0;
return (ps[r+1] - ps[l]) % mod;
}
int cnt = 0;
void solve(int l, int r){
cnt++;
if(cnt > 100)return;
if(l > r)return;
pair<ll,ll>cur = minq(l, r);
//cout<<cur.first<<' '<<cur.second<<'\n';
ll curh = cur.first;
ll dex = cur.second;
//cout<<l<<' '<<r<<' '<<dex<<'\n';
answer += c2(curh) * sumw(l, dex-1) % mod * sumw(dex+1, r) % mod + (sumw(l, dex-1) + sumw(dex+1, r)) % mod * c2(curh) % mod * w[dex] % mod + c2(w[dex]) * c2(curh) % mod;
answer %= mod;
//cout<<c2(curh) * sumw(l, dex) * sumw(dex, r)<<'\n';
//cout<<answer<<'\n';
if(l != r){
if(dex == l){
//cout<<'e'<<'\n';
solve(l+1, r);
}
else if(dex == r)solve(l, r-1);
else{
solve(l, dex - 1);
solve(dex + 1, r);
}
}
}
int main(){
int n;
cin>>n;
f0r(i,n)cin>>h[i];
f0r(i,n)cin>>w[i];
f0r(i,n)minh[0][i] = {h[i], i};
ps[0] = 0;
f0r(i, n){
ps[i+1] = ps[i] + w[i];
}
for(int l = 1;l<=floor(log2(n));l++){
f0r(i, n - pow(2, l) + 1){
ll fi;
ll se;
if(minh[l-1][i].first < minh[l-1][i + (int)pow(2, l-1)].first){
se = minh[l-1][i].second;
}
else se = minh[l-1][i + (int)pow(2, l-1)].second;
fi = min(minh[l-1][i].first, minh[l-1][i + (int)pow(2, l-1)].first);
minh[l][i] = {fi, se};
}
}
/*
ll dp[n+1];
dp[0] = ans(w[0], h[0]);
for(int i = 1;i<n;i++){
ll rm = h[i];
ll thing = 0;
for(int j = i-1;j>=0;j--){
rm = min(rm, h[j]);
thing += c2(rm) * w[j] % mod;
thing %= mod;
}
dp[i] = ((dp[i-1] + ans(w[i], h[i])) % mod + thing * w[i] % mod) % mod;
}
//for(auto u : dp)cout<<u<<' ';
//cout<<'\n';
cout<<dp[n-1];
*/
solve(0, n-1);
cout<<answer;
}
# | 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... |