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 pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
const ll md = 1e9+7;
const int N = 5e5+5;
const ll inv2 = 500000004;
ll pw2[N];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
pw2[0] = 1;
for(int i = 1; i < N; i++) pw2[i] = (pw2[i-1]*2)%md;
int n; cin >> n;
vector<int> A(n), B(n);
int m = 0;
for(int &x: A) cin >> x, m = max(m, x);
for(int &x: B) cin >> x, m = max(m, x);
for(int i = 0; i < n; i++) if(A[i] > B[i]) swap(A[i], B[i]);
ll ans = 0;
auto solve = [&](int i, ll x){
ll cur = 1, sum = 0;
int c = 0;
for(int j = i-1; j >= 0; j--){
if(A[j] > x){
cur = 0;
break;
}
if(B[j] <= x) c++;
}
cur*=pw2[c], cur%=md;
cur*=pw2[n-i-1], cur%=md;
bool breaked = 0;
for(int j = i+1; j < n; j++){
if(B[j] < x) continue;
if(A[j] >= x){
ans+=((((j-i)*x)%md)*cur)%md, ans%=md;
breaked = 1;
break;
}
cur*=inv2, cur%=md;
ans+=((((j-i)*x)%md)*cur)%md, ans%=md;
}
if(!breaked) ans+=(x*cur)%md, ans%=md;
};
auto solve_rev = [&](int i, ll x){
ll cur = 1, sum = 0;
int c = 0;
for(int j = i+1; j < n; j++){
if(A[j] >= x){
cur = 0;
break;
}
if(B[j] < x) c++;
}
cur*=pw2[c], cur%=md;
cur*=pw2[i], cur%=md;
for(int j = i-1; j >= 0; j--){
if(B[j] <= x) continue;
if(A[j] > x){
ans+=((((i-j)*x)%md)*cur)%md, ans%=md;
break;
}
cur*=inv2, cur%=md;
ans+=((((i-j)*x)%md)*cur)%md, ans%=md;
}
};
for(int i = 0; i < n; i++){
solve(i, A[i]);
solve(i, B[i]);
solve_rev(i, A[i]);
solve_rev(i, B[i]);
}
for(int i = 0; i < n; i++) ans-=((A[i]+B[i])*pw2[n-1])%md, ans%=md;
if(ans < 0) ans+=md;
cout << ans << "\n";
}
Compilation message (stderr)
Main.cpp: In lambda function:
Main.cpp:31:21: warning: unused variable 'sum' [-Wunused-variable]
31 | ll cur = 1, sum = 0;
| ^~~
Main.cpp: In lambda function:
Main.cpp:58:21: warning: unused variable 'sum' [-Wunused-variable]
58 | ll cur = 1, sum = 0;
| ^~~
# | 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... |