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;
#define int long long
#define all(x) x.begin(), x.end()
const int N = 1e4+4, M = 1e3+3, mod = 1e9+7;
int n, mx, a[N], b[N], c[N], pre[M][N], suf[M][N], pre2[N], suf2[N];
int pw(int x, int y) {
return (!y ? 1 : pw(x*x % mod, y/2) * (y%2 ? x : 1) % mod);
}
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n;
mx = 1;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
cin >> b[i];
if (a[i] > b[i]) swap(a[i], b[i]);
mx = max(mx, b[i]);
}
if (n <= 20) {
int ans = 0;
for (int mask = 0; mask < (1 << n); mask++) {
for (int i = 0; i < n; i++) {
c[i] = a[i];
if ((mask >> i) & 1) c[i] = b[i];
}
for (int i = 0; i < n; i++) {
pre2[i] = max((i ? pre2[i-1] : 1), c[i]);
}
for (int i = n-1; i >= 0; i--) {
suf2[i] = max((i+1 < n ? suf2[i+1] : 1), c[i]);
}
for (int i = 1; i < n-1; i++) {
if (c[i] < min(pre2[i-1], suf2[i+1])) {
ans += min(pre2[i-1], suf2[i+1]) - c[i];
ans %= mod;
}
}
}
cout << ans << "\n";
}
else {
for (int i = 0; i < n; i++) {
for (int j = 0; j <= mx; j++) {
pre[j][i] = (i ? pre[j][i-1] : 1);
if (b[i] <= j) {
pre[j][i] *= 2;
pre[j][i] %= mod;
}
else if (a[i] > j) pre[j][i] = 0;
}
}
for (int i = n-1; i >= 0; i--) {
for (int j = 0; j <= mx; j++) {
suf[j][i] = (i+1 < n ? suf[j][i+1] : 1);
if (b[i] <= j) {
suf[j][i] *= 2;
suf[j][i] %= mod;
}
else if (a[i] > j) suf[j][i] = 0;
}
}
int ans = 0;
for (int i = 1; i < n-1; i++) {
for (int j = 0; j <= mx; j++) {
int x = 0;
x += pw(2, n-1-i) * pre[j-1][i-1] % mod;
x %= mod;
x += pw(2, i) * suf[j-1][i+1] % mod;
x %= mod;
x = (x - pre[j-1][i-1] * suf[j-1][i+1] % mod + mod) % mod;
//cerr << i << " " << j << " " << x << endl;
if (a[i] < j) {
ans += (pw(2, n-1) - x + mod) % mod;
ans %= mod;
}
if (b[i] < j) {
ans += (pw(2, n-1) - x + mod) % mod;
ans %= mod;
}
}
}
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... |