#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
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 |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
17 ms |
4168 KB |
Output is correct |
4 |
Correct |
32 ms |
7912 KB |
Output is correct |
5 |
Correct |
33 ms |
7864 KB |
Output is correct |
6 |
Correct |
34 ms |
7928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
4 ms |
1232 KB |
Output is correct |
3 |
Correct |
21 ms |
4608 KB |
Output is correct |
4 |
Correct |
36 ms |
8648 KB |
Output is correct |
5 |
Correct |
38 ms |
8704 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
4 ms |
1268 KB |
Output is correct |
4 |
Correct |
22 ms |
4460 KB |
Output is correct |
5 |
Correct |
41 ms |
8644 KB |
Output is correct |
6 |
Correct |
38 ms |
8760 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
4 ms |
1220 KB |
Output is correct |
9 |
Correct |
19 ms |
4480 KB |
Output is correct |
10 |
Correct |
35 ms |
8588 KB |
Output is correct |
11 |
Correct |
37 ms |
8576 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
17 ms |
4040 KB |
Output is correct |
12 |
Correct |
32 ms |
7844 KB |
Output is correct |
13 |
Correct |
35 ms |
7876 KB |
Output is correct |
14 |
Correct |
34 ms |
7868 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
4 ms |
1256 KB |
Output is correct |
17 |
Correct |
18 ms |
4424 KB |
Output is correct |
18 |
Correct |
46 ms |
8772 KB |
Output is correct |
19 |
Correct |
43 ms |
8808 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
13 ms |
1228 KB |
Output is correct |
22 |
Correct |
19 ms |
4556 KB |
Output is correct |
23 |
Correct |
34 ms |
8524 KB |
Output is correct |
24 |
Correct |
35 ms |
8644 KB |
Output is correct |
25 |
Correct |
1 ms |
328 KB |
Output is correct |
26 |
Correct |
1 ms |
328 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
5 ms |
1248 KB |
Output is correct |
31 |
Correct |
6 ms |
1220 KB |
Output is correct |
32 |
Correct |
22 ms |
4424 KB |
Output is correct |
33 |
Correct |
20 ms |
4488 KB |
Output is correct |
34 |
Correct |
39 ms |
8388 KB |
Output is correct |
35 |
Correct |
39 ms |
8472 KB |
Output is correct |
36 |
Correct |
41 ms |
8672 KB |
Output is correct |
37 |
Correct |
41 ms |
8584 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
41 ms |
8676 KB |
Output is correct |
40 |
Correct |
42 ms |
8772 KB |
Output is correct |
41 |
Correct |
44 ms |
10036 KB |
Output is correct |
42 |
Correct |
43 ms |
15696 KB |
Output is correct |