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;
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]);
m++;
vector<int> cmnl(m, 0), cmxl(m, 0), cmnr(m, 0), cmxr(m, 0);
for(int i = 0; i < n; i++) cmnr[A[i]]++, cmxr[B[i]]++;
ll ans = 0;
for(int i = 0; i+1 < n; i++){
cmnr[A[i]]--, cmxr[B[i]]--;
int xl = 0, yl = 0, xr = 0, yr = 0;
for(int j = m-1; j > A[i]; j--){
xl+=cmnl[j], yl+=cmxl[j], xr+=cmnr[j], yr+=cmxr[j];
ll cur = 2;
if(j <= B[i]) cur = 1;
if(xl > 0) cur*=pw2[i], cur%=md;
else cur*=(pw2[i-yl]*(pw2[yl]-1))%md, cur%=md;
if(xr > 0) cur*=pw2[n-i-1], cur%=md;
else cur*=(pw2[n-i-1-yr]*(pw2[yr]-1))%md, cur%=md;
ans+=cur, ans%=md;
}
cmnl[A[i]]++, cmxl[B[i]]++;
}
cout << ans << "\n";
}
# | 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... |