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 ll = long long;
const ll MOD = 1e9+7;
typedef std::pair<ll,ll> pii;
ll gaussian(ll x){
return ((x*(x+1LL))/2LL)%MOD;
}
ll get_local(pii u){
return (gaussian(u.first)*gaussian(u.second))%MOD;
}
ll get_total(pii u){
return (gaussian(u.first)*u.second)%MOD;
}
int main()
{
ll ans=0;
ll temp=0;
int N;std::cin>>N;
long long a[N],b[N];
for(auto&x:a)std::cin>>x;
for(auto&x:b)std::cin>>x;
std::deque<pii> stack;
for(int i=0;i!=N;++i){
ll alpha=a[i],beta=b[i];
pii local = {alpha,beta};
ans=(ans+get_local(local))%MOD;
ll k=0;
while(stack.size()){
auto __ = stack.back();
if(__.first>=alpha){
k=(k+stack.back().second)%MOD;
temp-=get_total(stack.back());
if(temp<0)temp+=MOD;
local.second=(local.second+stack.back().second)%MOD;
stack.pop_back();
}else break;
}
ans=(ans+(((k*b[i])%MOD)*gaussian(alpha)))%MOD;
ans=((((temp*b[i])+ans)%MOD))%MOD;
stack.push_back(local);
temp=(temp+get_total(local))%MOD;
}
std::cout<<ans<<"\n";
}
# | 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... |