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>
using namespace std;
using ll = long long;
#define pb push_back
const int MOD = (int)1e9 + 7, INV_2 = (MOD + 1) / 2;
int N, glob, a[500005], b[500005];
vector<int> disc_vec;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> a[i];
}
for (int i = 1; i <= N; i++) {
cin >> b[i];
if (a[i] > b[i]) {
swap(a[i], b[i]);
}
for (int delta = -1; delta <= 1; delta++) {
disc_vec.pb(a[i] + delta);
disc_vec.pb(b[i] + delta);
}
}
sort(disc_vec.begin(), disc_vec.end());
disc_vec.erase(unique(disc_vec.begin(), disc_vec.end()), disc_vec.end());
for (int i = 1; i <= N; i++) {
for (int h : {a[i], b[i]}) {
int expected = 0;
vector<int> probs = {1}, vals = {h};
for (auto it = upper_bound(disc_vec.begin(), disc_vec.end(), h); it != disc_vec.end(); ++it) {
int v = *it;
int lft_prob = 1, rig_prob = 1;
for (int j = 1; j < i; j++) {
int cur_prob = INV_2;
if (a[j] >= v) {
cur_prob = 0;
} else if (b[j] < v) {
cur_prob = 1;
}
lft_prob = ((ll)lft_prob * cur_prob) % MOD;
}
lft_prob = (1 - lft_prob + MOD) % MOD;
for (int j = i + 1; j <= N; j++) {
int cur_prob = INV_2;
if (a[j] >= v) {
cur_prob = 0;
} else if (b[j] < v) {
cur_prob = 1;
}
rig_prob = ((ll)rig_prob * cur_prob) % MOD;
}
rig_prob = (1 - rig_prob + MOD) % MOD;
int p = (ll)lft_prob * rig_prob % MOD;
probs.pb(p);
vals.pb(v);
}
for (int idx = 0; idx + 1 < (int)probs.size(); idx++) {
int tmp = (probs[idx] - probs[idx + 1] + MOD) % MOD;
expected = (expected + (ll)vals[idx] * tmp % MOD) % MOD;
}
expected = (expected - h + MOD) % MOD;
glob = (glob + expected) % MOD;
}
}
for (int rep = 0; rep < N - 1; rep++) {
glob = (glob * 2) % MOD;
}
cout << glob << '\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... |