#include <bits/stdc++.h>
#define f first
#define s second
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define vec vector
#define pb push_back
#define sz(x) (int)(x).size()
#define pw(x) (1LL<<(x))
#define fast_resp ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef long double ld;
typedef pair<int,ll> pil;
template<class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
template<class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
const ll inf=1e18;
const int M=1e9+7;
void add(int &a,int b){
a+=b;
if(a>=M) a-=M;
else if(a<0) a+=M;
}
int mult(int a,int b){
return 1ll*a*b%M;
}
int binpow(int a,int b){
int ans=1;
while(b){
if(b&1) ans=mult(ans,a);
a=mult(a,a);b>>=1;
}
return ans;
}
signed main(){
fast_resp;
int n;
cin>>n;
vec<int>a(n),b(n);
vec<array<int,4>> arr;
/// h,sum_prefs,sum_prefs^1,cnt
vec<int>pref(n);
for(int i=0;i<n;i++) cin>>a[i];
for(int i=0;i<n;i++) cin>>b[i];
// for(int i=0;i<n;i++){
// cin>>a[i]>>b[i];
// pref[i]=(i?pref[i-1]:0);
// add(pref[i],b[i]);
// }
arr.pb({(int)-1e9-1,0,0,0});
int ans=0;
// int sumwh=0;
// int sumh=0;
int their=0;
int p2=binpow(2,M-2);
for(int i=0;i<n;i++){
array<int,4> me={0,0,0,0};
me[0]=a[i];
// me[1]=b[i];me[2]=1;
while(sz(arr) && arr.back()[0]>=a[i]){
int h=arr.back()[0];
int smw2=arr.back()[1];
int cnt=arr.back()[2];
add(their,-mult(smw2,mult(h,h+1)));
arr.pop_back();
add(me[1],smw2);
add(me[2],cnt);
// add(me[2],cnt);
}
int h=me[0];
int smw2=me[1];
int cnt=me[2];
add(their,mult(smw2,mult(h,h+1)));
add(ans,mult(b[i],their));
add(ans,mult(p2,mult(b[i],mult(a[i],mult(b[i]+1,a[i]+1)))));
add(me[1],b[i]);add(me[2],1);
add(their,mult(b[i],mult(h,h+1)));
arr.pb(me);
// add(ans,mult(b[i],mult(h,h+1)));
// add(ans,mult(sumh,mult(pref[i],pref[i])));
// add(ans,mult(2,mult(sumwh,pref[i])));
}
// cout<<ans<<endl;
ans=mult(ans,binpow(2,M-2));
cout<<ans;
return 0;
}
/*
*/
Compilation message
fancyfence.cpp: In function 'int main()':
fancyfence.cpp:78:13: warning: unused variable 'cnt' [-Wunused-variable]
78 | int cnt=me[2];
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
11 ms |
1492 KB |
Output is correct |
4 |
Correct |
25 ms |
2648 KB |
Output is correct |
5 |
Correct |
23 ms |
2644 KB |
Output is correct |
6 |
Correct |
21 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
4 ms |
548 KB |
Output is correct |
3 |
Correct |
13 ms |
1808 KB |
Output is correct |
4 |
Correct |
28 ms |
3400 KB |
Output is correct |
5 |
Correct |
33 ms |
3532 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
596 KB |
Output is correct |
4 |
Correct |
13 ms |
1804 KB |
Output is correct |
5 |
Correct |
28 ms |
3420 KB |
Output is correct |
6 |
Correct |
29 ms |
3540 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
4 ms |
980 KB |
Output is correct |
9 |
Correct |
14 ms |
2564 KB |
Output is correct |
10 |
Correct |
26 ms |
5440 KB |
Output is correct |
11 |
Correct |
29 ms |
5688 KB |
Output is correct |
12 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
316 KB |
Output is correct |
2 |
Correct |
1 ms |
352 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
328 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
12 ms |
1420 KB |
Output is correct |
12 |
Correct |
21 ms |
2644 KB |
Output is correct |
13 |
Correct |
22 ms |
2640 KB |
Output is correct |
14 |
Correct |
20 ms |
2640 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
596 KB |
Output is correct |
17 |
Correct |
21 ms |
1876 KB |
Output is correct |
18 |
Correct |
27 ms |
3400 KB |
Output is correct |
19 |
Correct |
29 ms |
3552 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
4 ms |
980 KB |
Output is correct |
22 |
Correct |
16 ms |
2560 KB |
Output is correct |
23 |
Correct |
36 ms |
5436 KB |
Output is correct |
24 |
Correct |
34 ms |
5572 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
4 ms |
596 KB |
Output is correct |
31 |
Correct |
3 ms |
632 KB |
Output is correct |
32 |
Correct |
17 ms |
1680 KB |
Output is correct |
33 |
Correct |
15 ms |
1868 KB |
Output is correct |
34 |
Correct |
28 ms |
3280 KB |
Output is correct |
35 |
Correct |
30 ms |
3144 KB |
Output is correct |
36 |
Correct |
28 ms |
3408 KB |
Output is correct |
37 |
Correct |
26 ms |
3400 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
26 ms |
3540 KB |
Output is correct |
40 |
Correct |
27 ms |
3540 KB |
Output is correct |
41 |
Correct |
25 ms |
3788 KB |
Output is correct |
42 |
Correct |
31 ms |
4552 KB |
Output is correct |