/*
Problem Link:https://oj.uz/problem/view/CEOI20_fancyfence
Author: varungoyalbits
*/
#include <bits/stdc++.h>
using namespace std;
long long sumTillN(const long long N,const long long mod)
{
return ((N*(N+1))/2)%mod;
}
long long subTask6()
{
int n;
cin>>n;
vector<long long>h(n);
vector<long long>w(n);
long long mod=1e9+7;
for(int i=0;i<n;i++)
{
cin>>h[i];
}
for(int i=0;i<n;i++)
{
cin>>w[i];
}
long long ans=0;
for(int i=0;i<n;i++)
{
long long val=sumTillN(h[i],mod);
val=(val*sumTillN(w[i],mod))%mod;
ans=(ans+val)%mod;
long long mn=h[i];
for(int j=i-1;j>=0;j--)
{
mn=min(mn,h[j]);
val=sumTillN(mn,mod);
val=(val*((w[i]*w[j])%mod))%mod;
ans=(ans+val)%mod;
}
}
return ans;
}
long long sol()
{
int n;
cin>>n;
vector<long long>h(n);
vector<long long>w(n);
vector<long long>w2(n);
vector<int>left(n,-1);
vector<int>right(n,n);
long long mod=1e9+7;
for(int i=0;i<n;i++)
{
cin>>h[i];
}
for(int i=0;i<n;i++)
{
cin>>w[i];
}
w2[0]=w[0];
for(int i=1;i<n;i++)
{
w2[i]=(w2[i-1]+w[i])%mod;
}
for(int i=0;i<n;i++)
{
int ind=i-1;
while(ind>=0&&h[ind]>=h[i])
{
ind=left[ind];
}
left[i]=ind;
}
for(int i=n-1;i>=0;i--)
{
int ind=i+1;
while(ind<n&&h[ind]>=h[i])
{
ind=right[ind];
}
right[i]=ind;
}
long long ans=0;
for(int i=0;i<n;i++)
{
long long val=sumTillN(h[i],mod);
val=(val*sumTillN(w[i],mod))%mod;
ans=(ans+val)%mod;
}
//cout<<ans<<endl;
for(int i=0;i<n;i++)
{
long long l=left[i];
long long r=right[i];
//[l+1,i-1]
//[i+1,r-1]
//cout<<"l: "<<l+1<<" r: "<<r-1<<endl;
long long sumLeft=0;
long long sumRight=0;
if(l>=0&&i-1>=0)
{
sumLeft=w2[i-1]-w2[l];
//cout<<"sumLeft: "<<sumLeft<<endl;
}
else if(i-1>=0)
{
sumLeft=w2[i-1];
}
if(r-1<=n-1&&r-1>=0)
{
sumRight=w2[r-1]-w2[i];
//cout<<"sumRight: "<<sumRight<<endl;
}
sumLeft+=mod;
sumLeft%=mod;
sumRight+=mod;
sumRight%=mod;
long long totSum=(sumLeft+sumRight)%mod;
long long val=sumTillN(h[i],mod);
long long val2=(sumLeft*sumRight)%mod;
long long val3=(w[i]*totSum)%mod;
val=(val*((val2+val3)%mod))%mod;
ans=(ans+val)%mod;
}
return ans;
}
int main()
{
//cout<<subTask6()<<"\n";
cout<<sol()<<"\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
2 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
292 KB |
Output is correct |
6 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |