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 int long long
using namespace std;
int A[100010], B[100010], n, i, j, k, a, b, C[100010], mod=1e9+7;
int ans;
int f(int a)
{
return ((a*(a+1))/2)%mod;
}
int g(int a)
{
return ((f(a)*(2*a+1))%mod*333333336)%mod;
}
int get(int a, int b, int c)
{
a%=mod;b%=mod;c%=mod;
int ans=0;
ans+=g(a)*c;
ans+=g(c)*a;
ans+=f(a)*c;
ans+=f(c)*a;
ans+=((f(a)*f(b))%mod*(2*b+2))%mod;
ans+=(((b+b*b)%mod*a)%mod*b)%mod;
ans%=mod;
if(ans%2)ans+=mod;
ans/=2;
return ans;
}
struct seg
{
pair<int, int>seg[400010];
void init(int id, int s, int e)
{
if(s==e)
{
seg[id]={B[s], s};
return;
}
int m=s+e>>1;
init(id*2, s, m);init(id*2+1, m+1, e);
seg[id]=min(seg[id*2], seg[id*2+1]);
}
pair<int, int>get(int id, int s, int e, int l, int r)
{
if(e<l||r<s)return {1e9, 1e9};
if(l<=s&&e<=r)return seg[id];
int m=s+e>>1;
return min(get(id*2, s, m, l, r), get(id*2+1, m+1, e, l, r));
}
}seg;
void dnc(int l, int r)
{
if(l>r)return;
pair<int, int>p=seg.get(1, 1, n, l, r);
int x=p.second;
int res=0;
int a=C[x-1]-C[l-1], b=A[x], c=C[r]-C[x];
a%=mod;b%=mod;c%=mod;
res+=f(b);
res+=a*c;
res+=b*c;
res+=a*b;
res%=mod;
res*=f(p.first);
res%=mod;
ans+=res;
ans%=mod;
dnc(l, x-1);dnc(x+1, r);
}
main()
{
ios_base::sync_with_stdio(!cin.tie(NULL));
for(cin>>n;i++<n;)cin>>B[i];
for(i=0;i++<n;)
{
cin>>A[i];
C[i]=C[i-1]+A[i];
}
seg.init(1, 1, n);
dnc(1, n);
cout<<ans;
}
Compilation message (stderr)
fancyfence.cpp: In member function 'void seg::init(long long int, long long int, long long int)':
fancyfence.cpp:39:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
39 | int m=s+e>>1;
| ~^~
fancyfence.cpp: In member function 'std::pair<long long int, long long int> seg::get(long long int, long long int, long long int, long long int, long long int)':
fancyfence.cpp:47:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
47 | int m=s+e>>1;
| ~^~
fancyfence.cpp: At global scope:
fancyfence.cpp:70:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
70 | main()
| ^~~~
# | 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... |