/*
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: "<<ans<<endl;
for(int i=0;i<n;i++)
{
int l=left[i];
int r=right[i];
//[l+1,i-1]
//[i+1,r-1]
//cout<<"l: "<<l+1<<" r: "<<r-1<<" i: "<<i<<endl;
long long sumLeft=0;
long long sumRight=0;
if(l>=0&&i-1>=0)
{
sumLeft=w2[i-1]-w2[l];
}
else if(i-1>=0)
{
sumLeft=w2[i-1];
}
//cout<<"sumLeft: "<<sumLeft<<endl;
if(r-1<=n-1)
{
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;
val2=(val2+val3)%mod;
val=(val*val2)%mod;
//cout<<"val: "<<val<<endl;
ans=(ans+val)%mod;
//cout<<"ans: "<<ans<<endl;
}
return ans;
}
int main()
{
//cout<<subTask6()<<"\n";
cout<<sol()<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
204 KB |
Output is correct |
3 |
Correct |
34 ms |
2252 KB |
Output is correct |
4 |
Correct |
66 ms |
4576 KB |
Output is correct |
5 |
Correct |
67 ms |
4560 KB |
Output is correct |
6 |
Correct |
66 ms |
4576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
11 ms |
692 KB |
Output is correct |
3 |
Correct |
46 ms |
2352 KB |
Output is correct |
4 |
Correct |
89 ms |
3796 KB |
Output is correct |
5 |
Correct |
97 ms |
3820 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
10 ms |
716 KB |
Output is correct |
4 |
Correct |
46 ms |
2256 KB |
Output is correct |
5 |
Correct |
93 ms |
3788 KB |
Output is correct |
6 |
Correct |
110 ms |
3792 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
10 ms |
716 KB |
Output is correct |
9 |
Correct |
47 ms |
2236 KB |
Output is correct |
10 |
Correct |
86 ms |
5204 KB |
Output is correct |
11 |
Correct |
93 ms |
5444 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
292 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
308 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
2 ms |
300 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
292 KB |
Output is correct |
10 |
Correct |
2 ms |
204 KB |
Output is correct |
11 |
Correct |
37 ms |
2360 KB |
Output is correct |
12 |
Correct |
67 ms |
4708 KB |
Output is correct |
13 |
Correct |
67 ms |
4576 KB |
Output is correct |
14 |
Correct |
66 ms |
4576 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
10 ms |
720 KB |
Output is correct |
17 |
Correct |
45 ms |
2824 KB |
Output is correct |
18 |
Correct |
110 ms |
5324 KB |
Output is correct |
19 |
Correct |
103 ms |
5572 KB |
Output is correct |
20 |
Correct |
2 ms |
348 KB |
Output is correct |
21 |
Correct |
10 ms |
720 KB |
Output is correct |
22 |
Correct |
46 ms |
2728 KB |
Output is correct |
23 |
Correct |
89 ms |
5188 KB |
Output is correct |
24 |
Correct |
89 ms |
5316 KB |
Output is correct |
25 |
Correct |
1 ms |
296 KB |
Output is correct |
26 |
Correct |
1 ms |
300 KB |
Output is correct |
27 |
Correct |
2 ms |
332 KB |
Output is correct |
28 |
Correct |
2 ms |
332 KB |
Output is correct |
29 |
Correct |
2 ms |
304 KB |
Output is correct |
30 |
Correct |
10 ms |
708 KB |
Output is correct |
31 |
Correct |
10 ms |
800 KB |
Output is correct |
32 |
Correct |
44 ms |
2728 KB |
Output is correct |
33 |
Correct |
46 ms |
2744 KB |
Output is correct |
34 |
Correct |
98 ms |
5088 KB |
Output is correct |
35 |
Correct |
85 ms |
5092 KB |
Output is correct |
36 |
Correct |
91 ms |
5340 KB |
Output is correct |
37 |
Correct |
91 ms |
5340 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
91 ms |
5456 KB |
Output is correct |
40 |
Correct |
92 ms |
5332 KB |
Output is correct |
41 |
Correct |
119 ms |
5316 KB |
Output is correct |
42 |
Correct |
91 ms |
5340 KB |
Output is correct |