#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%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD%MOD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%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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
448 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Runtime error |
185 ms |
1048576 KB |
Execution killed with signal 9 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
600 KB |
Output is correct |
10 |
Correct |
0 ms |
460 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
6 ms |
1884 KB |
Output is correct |
16 |
Correct |
6 ms |
1884 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
4 ms |
1884 KB |
Output is correct |
21 |
Correct |
7 ms |
2044 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
5 ms |
1884 KB |
Output is correct |
24 |
Correct |
7 ms |
1932 KB |
Output is correct |
25 |
Correct |
7 ms |
1884 KB |
Output is correct |
26 |
Correct |
6 ms |
1884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
600 KB |
Output is correct |
10 |
Correct |
0 ms |
460 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
6 ms |
1884 KB |
Output is correct |
16 |
Correct |
6 ms |
1884 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
4 ms |
1884 KB |
Output is correct |
21 |
Correct |
7 ms |
2044 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
5 ms |
1884 KB |
Output is correct |
24 |
Correct |
7 ms |
1932 KB |
Output is correct |
25 |
Correct |
7 ms |
1884 KB |
Output is correct |
26 |
Correct |
6 ms |
1884 KB |
Output is correct |
27 |
Correct |
1258 ms |
157872 KB |
Output is correct |
28 |
Correct |
1215 ms |
157956 KB |
Output is correct |
29 |
Correct |
4 ms |
1628 KB |
Output is correct |
30 |
Correct |
4 ms |
1628 KB |
Output is correct |
31 |
Correct |
14 ms |
2908 KB |
Output is correct |
32 |
Correct |
838 ms |
158032 KB |
Output is correct |
33 |
Correct |
1656 ms |
157268 KB |
Output is correct |
34 |
Correct |
15 ms |
2908 KB |
Output is correct |
35 |
Correct |
834 ms |
157780 KB |
Output is correct |
36 |
Correct |
1613 ms |
157728 KB |
Output is correct |
37 |
Correct |
4 ms |
1628 KB |
Output is correct |
38 |
Correct |
4 ms |
1628 KB |
Output is correct |
39 |
Correct |
15 ms |
2908 KB |
Output is correct |
40 |
Correct |
14 ms |
2908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
448 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Runtime error |
185 ms |
1048576 KB |
Execution killed with signal 9 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
376 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
5 ms |
1672 KB |
Output is correct |
11 |
Correct |
4 ms |
1628 KB |
Output is correct |
12 |
Correct |
5 ms |
1628 KB |
Output is correct |
13 |
Correct |
5 ms |
1768 KB |
Output is correct |
14 |
Correct |
240 ms |
65036 KB |
Output is correct |
15 |
Correct |
241 ms |
64840 KB |
Output is correct |
16 |
Correct |
251 ms |
64968 KB |
Output is correct |
17 |
Correct |
258 ms |
64856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
448 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Runtime error |
185 ms |
1048576 KB |
Execution killed with signal 9 |
12 |
Halted |
0 ms |
0 KB |
- |