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 MOD = 1e9+7;
int n, maxHeight;
vector<int> a, b;
vector<vector<int>> prefSum, sufSum;
int modExp(int x, int y) {
int res = 1;
while (y > 0) {
if (y % 2 == 1) {
res = res * x % MOD;
}
x = x * x % MOD;
y /= 2;
}
return res;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
maxHeight = 1;
a.resize(n);
b.resize(n);
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]);
maxHeight = max(maxHeight, b[i]);
}
prefSum.assign(n, vector<int>(maxHeight + 1, 1));
sufSum.assign(n, vector<int>(maxHeight + 1, 1));
for (int i = 0; i < n; i++) {
for (int j = 0; j <= maxHeight; j++) {
if (i > 0) prefSum[i][j] = prefSum[i - 1][j];
if (b[i] <= j) {
prefSum[i][j] = prefSum[i][j] * 2 % MOD;
} else if (a[i] > j) {
prefSum[i][j] = 0;
}
}
}
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j <= maxHeight; j++) {
if (i + 1 < n) sufSum[i][j] = sufSum[i + 1][j];
if (b[i] <= j) {
sufSum[i][j] = sufSum[i][j] * 2 % MOD;
} else if (a[i] > j) {
sufSum[i][j] = 0;
}
}
}
int ans = 0;
for (int i = 1; i < n - 1; i++) {
for (int j = 1; j <= maxHeight; j++) {
int x = 0;
x = (x + modExp(2, n - 1 - i) * prefSum[i - 1][j - 1] % MOD) % MOD;
x = (x + modExp(2, i) * sufSum[i + 1][j - 1] % MOD) % MOD;
x = (x - prefSum[i - 1][j - 1] * sufSum[i + 1][j - 1] % MOD + MOD) % MOD;
if (a[i] < j) {
ans = (ans%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%M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OD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD + modExp(2, n - 1) - x + MOD) % MOD;
}
}
}
cout << ans << endl;
}
# | 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... |