#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
const int N = 5e5+5, mod = 1e9+7;
int n, mx, a[N], b[N], c[N], pre2[N], suf2[N];
vector<int> pre[N], suf[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++) {
pre[i].resize(mx+1);
suf[i].resize(mx+1);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j <= mx; j++) {
pre[i][j] = (i ? pre[i-1][j] : 1);
if (b[i] <= j) {
pre[i][j] *= 2;
pre[i][j] %= mod;
}
else if (a[i] > j) pre[i][j] = 0;
}
}
for (int i = n-1; i >= 0; i--) {
for (int j = 0; j <= mx; j++) {
suf[i][j] = (i+1 < n ? suf[i+1][j] : 1);
if (b[i] <= j) {
suf[i][j] *= 2;
suf[i][j] %= mod;
}
else if (a[i] > j) suf[i][j] = 0;
}
}
int ans = 0;
for (int i = 1; i < n-1; i++) {
for (int j = 1; j <= mx; j++) {
int x = 0;
x += pw(2, n-1-i) * pre[i-1][j-1] % mod;
x %= mod;
x += pw(2, i) * suf[i+1][j-1] % mod;
x %= mod;
x = (x - pre[i-1][j-1] * suf[i+1][j-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 |
1 |
Correct |
9 ms |
23944 KB |
Output is correct |
2 |
Correct |
9 ms |
23988 KB |
Output is correct |
3 |
Correct |
9 ms |
23796 KB |
Output is correct |
4 |
Correct |
8 ms |
23932 KB |
Output is correct |
5 |
Correct |
8 ms |
23900 KB |
Output is correct |
6 |
Correct |
9 ms |
23756 KB |
Output is correct |
7 |
Correct |
9 ms |
23900 KB |
Output is correct |
8 |
Correct |
9 ms |
23892 KB |
Output is correct |
9 |
Correct |
9 ms |
23896 KB |
Output is correct |
10 |
Correct |
8 ms |
23900 KB |
Output is correct |
11 |
Correct |
112 ms |
23900 KB |
Output is correct |
12 |
Correct |
11 ms |
23900 KB |
Output is correct |
13 |
Correct |
172 ms |
23956 KB |
Output is correct |
14 |
Correct |
12 ms |
23896 KB |
Output is correct |
15 |
Correct |
120 ms |
23900 KB |
Output is correct |
16 |
Correct |
116 ms |
23900 KB |
Output is correct |
17 |
Correct |
129 ms |
23956 KB |
Output is correct |
18 |
Correct |
11 ms |
23900 KB |
Output is correct |
19 |
Correct |
9 ms |
23780 KB |
Output is correct |
20 |
Correct |
23 ms |
23900 KB |
Output is correct |
21 |
Correct |
121 ms |
23960 KB |
Output is correct |
22 |
Correct |
67 ms |
23964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
23900 KB |
Output is correct |
2 |
Correct |
9 ms |
23900 KB |
Output is correct |
3 |
Correct |
9 ms |
23900 KB |
Output is correct |
4 |
Correct |
9 ms |
23964 KB |
Output is correct |
5 |
Correct |
9 ms |
23896 KB |
Output is correct |
6 |
Correct |
9 ms |
23900 KB |
Output is correct |
7 |
Correct |
9 ms |
23788 KB |
Output is correct |
8 |
Correct |
9 ms |
23900 KB |
Output is correct |
9 |
Correct |
9 ms |
23748 KB |
Output is correct |
10 |
Correct |
9 ms |
23900 KB |
Output is correct |
11 |
Correct |
165 ms |
23896 KB |
Output is correct |
12 |
Correct |
14 ms |
23896 KB |
Output is correct |
13 |
Correct |
140 ms |
23900 KB |
Output is correct |
14 |
Correct |
9 ms |
23900 KB |
Output is correct |
15 |
Correct |
16 ms |
25372 KB |
Output is correct |
16 |
Correct |
16 ms |
25436 KB |
Output is correct |
17 |
Correct |
9 ms |
23900 KB |
Output is correct |
18 |
Correct |
9 ms |
23900 KB |
Output is correct |
19 |
Correct |
9 ms |
23776 KB |
Output is correct |
20 |
Correct |
14 ms |
25436 KB |
Output is correct |
21 |
Correct |
18 ms |
25408 KB |
Output is correct |
22 |
Correct |
9 ms |
23908 KB |
Output is correct |
23 |
Correct |
14 ms |
25284 KB |
Output is correct |
24 |
Correct |
19 ms |
25180 KB |
Output is correct |
25 |
Correct |
22 ms |
25436 KB |
Output is correct |
26 |
Correct |
17 ms |
25492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
23900 KB |
Output is correct |
2 |
Correct |
9 ms |
23900 KB |
Output is correct |
3 |
Correct |
9 ms |
23900 KB |
Output is correct |
4 |
Correct |
9 ms |
23964 KB |
Output is correct |
5 |
Correct |
9 ms |
23896 KB |
Output is correct |
6 |
Correct |
9 ms |
23900 KB |
Output is correct |
7 |
Correct |
9 ms |
23788 KB |
Output is correct |
8 |
Correct |
9 ms |
23900 KB |
Output is correct |
9 |
Correct |
9 ms |
23748 KB |
Output is correct |
10 |
Correct |
9 ms |
23900 KB |
Output is correct |
11 |
Correct |
165 ms |
23896 KB |
Output is correct |
12 |
Correct |
14 ms |
23896 KB |
Output is correct |
13 |
Correct |
140 ms |
23900 KB |
Output is correct |
14 |
Correct |
9 ms |
23900 KB |
Output is correct |
15 |
Correct |
16 ms |
25372 KB |
Output is correct |
16 |
Correct |
16 ms |
25436 KB |
Output is correct |
17 |
Correct |
9 ms |
23900 KB |
Output is correct |
18 |
Correct |
9 ms |
23900 KB |
Output is correct |
19 |
Correct |
9 ms |
23776 KB |
Output is correct |
20 |
Correct |
14 ms |
25436 KB |
Output is correct |
21 |
Correct |
18 ms |
25408 KB |
Output is correct |
22 |
Correct |
9 ms |
23908 KB |
Output is correct |
23 |
Correct |
14 ms |
25284 KB |
Output is correct |
24 |
Correct |
19 ms |
25180 KB |
Output is correct |
25 |
Correct |
22 ms |
25436 KB |
Output is correct |
26 |
Correct |
17 ms |
25492 KB |
Output is correct |
27 |
Correct |
2047 ms |
180828 KB |
Output is correct |
28 |
Correct |
1926 ms |
180972 KB |
Output is correct |
29 |
Correct |
14 ms |
24668 KB |
Output is correct |
30 |
Correct |
14 ms |
24792 KB |
Output is correct |
31 |
Correct |
30 ms |
25948 KB |
Output is correct |
32 |
Correct |
1245 ms |
181064 KB |
Output is correct |
33 |
Correct |
2533 ms |
180388 KB |
Output is correct |
34 |
Correct |
29 ms |
25944 KB |
Output is correct |
35 |
Correct |
1271 ms |
180976 KB |
Output is correct |
36 |
Correct |
2514 ms |
180816 KB |
Output is correct |
37 |
Correct |
13 ms |
24668 KB |
Output is correct |
38 |
Correct |
13 ms |
24792 KB |
Output is correct |
39 |
Correct |
30 ms |
25948 KB |
Output is correct |
40 |
Correct |
29 ms |
25948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
23944 KB |
Output is correct |
2 |
Correct |
9 ms |
23988 KB |
Output is correct |
3 |
Correct |
9 ms |
23796 KB |
Output is correct |
4 |
Correct |
8 ms |
23932 KB |
Output is correct |
5 |
Correct |
8 ms |
23900 KB |
Output is correct |
6 |
Correct |
9 ms |
23756 KB |
Output is correct |
7 |
Correct |
9 ms |
23900 KB |
Output is correct |
8 |
Correct |
9 ms |
23892 KB |
Output is correct |
9 |
Correct |
9 ms |
23896 KB |
Output is correct |
10 |
Correct |
8 ms |
23900 KB |
Output is correct |
11 |
Correct |
112 ms |
23900 KB |
Output is correct |
12 |
Correct |
11 ms |
23900 KB |
Output is correct |
13 |
Correct |
172 ms |
23956 KB |
Output is correct |
14 |
Correct |
12 ms |
23896 KB |
Output is correct |
15 |
Correct |
120 ms |
23900 KB |
Output is correct |
16 |
Correct |
116 ms |
23900 KB |
Output is correct |
17 |
Correct |
129 ms |
23956 KB |
Output is correct |
18 |
Correct |
11 ms |
23900 KB |
Output is correct |
19 |
Correct |
9 ms |
23780 KB |
Output is correct |
20 |
Correct |
23 ms |
23900 KB |
Output is correct |
21 |
Correct |
121 ms |
23960 KB |
Output is correct |
22 |
Correct |
67 ms |
23964 KB |
Output is correct |
23 |
Correct |
9 ms |
23900 KB |
Output is correct |
24 |
Correct |
9 ms |
23900 KB |
Output is correct |
25 |
Correct |
9 ms |
23900 KB |
Output is correct |
26 |
Correct |
9 ms |
23964 KB |
Output is correct |
27 |
Correct |
9 ms |
23896 KB |
Output is correct |
28 |
Correct |
9 ms |
23900 KB |
Output is correct |
29 |
Correct |
9 ms |
23788 KB |
Output is correct |
30 |
Correct |
9 ms |
23900 KB |
Output is correct |
31 |
Correct |
9 ms |
23748 KB |
Output is correct |
32 |
Correct |
9 ms |
23900 KB |
Output is correct |
33 |
Correct |
165 ms |
23896 KB |
Output is correct |
34 |
Correct |
14 ms |
23896 KB |
Output is correct |
35 |
Correct |
140 ms |
23900 KB |
Output is correct |
36 |
Correct |
9 ms |
23900 KB |
Output is correct |
37 |
Correct |
16 ms |
25372 KB |
Output is correct |
38 |
Correct |
16 ms |
25436 KB |
Output is correct |
39 |
Correct |
9 ms |
23900 KB |
Output is correct |
40 |
Correct |
9 ms |
23900 KB |
Output is correct |
41 |
Correct |
9 ms |
23776 KB |
Output is correct |
42 |
Correct |
14 ms |
25436 KB |
Output is correct |
43 |
Correct |
18 ms |
25408 KB |
Output is correct |
44 |
Correct |
9 ms |
23908 KB |
Output is correct |
45 |
Correct |
14 ms |
25284 KB |
Output is correct |
46 |
Correct |
19 ms |
25180 KB |
Output is correct |
47 |
Correct |
22 ms |
25436 KB |
Output is correct |
48 |
Correct |
17 ms |
25492 KB |
Output is correct |
49 |
Correct |
2047 ms |
180828 KB |
Output is correct |
50 |
Correct |
1926 ms |
180972 KB |
Output is correct |
51 |
Correct |
14 ms |
24668 KB |
Output is correct |
52 |
Correct |
14 ms |
24792 KB |
Output is correct |
53 |
Correct |
30 ms |
25948 KB |
Output is correct |
54 |
Correct |
1245 ms |
181064 KB |
Output is correct |
55 |
Correct |
2533 ms |
180388 KB |
Output is correct |
56 |
Correct |
29 ms |
25944 KB |
Output is correct |
57 |
Correct |
1271 ms |
180976 KB |
Output is correct |
58 |
Correct |
2514 ms |
180816 KB |
Output is correct |
59 |
Correct |
13 ms |
24668 KB |
Output is correct |
60 |
Correct |
13 ms |
24792 KB |
Output is correct |
61 |
Correct |
30 ms |
25948 KB |
Output is correct |
62 |
Correct |
29 ms |
25948 KB |
Output is correct |
63 |
Runtime error |
463 ms |
1048576 KB |
Execution killed with signal 9 |
64 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23896 KB |
Output is correct |
2 |
Correct |
11 ms |
23900 KB |
Output is correct |
3 |
Correct |
9 ms |
23900 KB |
Output is correct |
4 |
Correct |
9 ms |
23776 KB |
Output is correct |
5 |
Correct |
10 ms |
23900 KB |
Output is correct |
6 |
Correct |
149 ms |
23948 KB |
Output is correct |
7 |
Correct |
17 ms |
23900 KB |
Output is correct |
8 |
Correct |
10 ms |
23896 KB |
Output is correct |
9 |
Correct |
10 ms |
23900 KB |
Output is correct |
10 |
Correct |
13 ms |
24788 KB |
Output is correct |
11 |
Correct |
14 ms |
24664 KB |
Output is correct |
12 |
Correct |
14 ms |
24668 KB |
Output is correct |
13 |
Correct |
14 ms |
24668 KB |
Output is correct |
14 |
Correct |
335 ms |
64844 KB |
Output is correct |
15 |
Correct |
361 ms |
65072 KB |
Output is correct |
16 |
Correct |
323 ms |
64848 KB |
Output is correct |
17 |
Correct |
342 ms |
65104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
23944 KB |
Output is correct |
2 |
Correct |
9 ms |
23988 KB |
Output is correct |
3 |
Correct |
9 ms |
23796 KB |
Output is correct |
4 |
Correct |
8 ms |
23932 KB |
Output is correct |
5 |
Correct |
8 ms |
23900 KB |
Output is correct |
6 |
Correct |
9 ms |
23756 KB |
Output is correct |
7 |
Correct |
9 ms |
23900 KB |
Output is correct |
8 |
Correct |
9 ms |
23892 KB |
Output is correct |
9 |
Correct |
9 ms |
23896 KB |
Output is correct |
10 |
Correct |
8 ms |
23900 KB |
Output is correct |
11 |
Correct |
112 ms |
23900 KB |
Output is correct |
12 |
Correct |
11 ms |
23900 KB |
Output is correct |
13 |
Correct |
172 ms |
23956 KB |
Output is correct |
14 |
Correct |
12 ms |
23896 KB |
Output is correct |
15 |
Correct |
120 ms |
23900 KB |
Output is correct |
16 |
Correct |
116 ms |
23900 KB |
Output is correct |
17 |
Correct |
129 ms |
23956 KB |
Output is correct |
18 |
Correct |
11 ms |
23900 KB |
Output is correct |
19 |
Correct |
9 ms |
23780 KB |
Output is correct |
20 |
Correct |
23 ms |
23900 KB |
Output is correct |
21 |
Correct |
121 ms |
23960 KB |
Output is correct |
22 |
Correct |
67 ms |
23964 KB |
Output is correct |
23 |
Correct |
9 ms |
23900 KB |
Output is correct |
24 |
Correct |
9 ms |
23900 KB |
Output is correct |
25 |
Correct |
9 ms |
23900 KB |
Output is correct |
26 |
Correct |
9 ms |
23964 KB |
Output is correct |
27 |
Correct |
9 ms |
23896 KB |
Output is correct |
28 |
Correct |
9 ms |
23900 KB |
Output is correct |
29 |
Correct |
9 ms |
23788 KB |
Output is correct |
30 |
Correct |
9 ms |
23900 KB |
Output is correct |
31 |
Correct |
9 ms |
23748 KB |
Output is correct |
32 |
Correct |
9 ms |
23900 KB |
Output is correct |
33 |
Correct |
165 ms |
23896 KB |
Output is correct |
34 |
Correct |
14 ms |
23896 KB |
Output is correct |
35 |
Correct |
140 ms |
23900 KB |
Output is correct |
36 |
Correct |
9 ms |
23900 KB |
Output is correct |
37 |
Correct |
16 ms |
25372 KB |
Output is correct |
38 |
Correct |
16 ms |
25436 KB |
Output is correct |
39 |
Correct |
9 ms |
23900 KB |
Output is correct |
40 |
Correct |
9 ms |
23900 KB |
Output is correct |
41 |
Correct |
9 ms |
23776 KB |
Output is correct |
42 |
Correct |
14 ms |
25436 KB |
Output is correct |
43 |
Correct |
18 ms |
25408 KB |
Output is correct |
44 |
Correct |
9 ms |
23908 KB |
Output is correct |
45 |
Correct |
14 ms |
25284 KB |
Output is correct |
46 |
Correct |
19 ms |
25180 KB |
Output is correct |
47 |
Correct |
22 ms |
25436 KB |
Output is correct |
48 |
Correct |
17 ms |
25492 KB |
Output is correct |
49 |
Correct |
2047 ms |
180828 KB |
Output is correct |
50 |
Correct |
1926 ms |
180972 KB |
Output is correct |
51 |
Correct |
14 ms |
24668 KB |
Output is correct |
52 |
Correct |
14 ms |
24792 KB |
Output is correct |
53 |
Correct |
30 ms |
25948 KB |
Output is correct |
54 |
Correct |
1245 ms |
181064 KB |
Output is correct |
55 |
Correct |
2533 ms |
180388 KB |
Output is correct |
56 |
Correct |
29 ms |
25944 KB |
Output is correct |
57 |
Correct |
1271 ms |
180976 KB |
Output is correct |
58 |
Correct |
2514 ms |
180816 KB |
Output is correct |
59 |
Correct |
13 ms |
24668 KB |
Output is correct |
60 |
Correct |
13 ms |
24792 KB |
Output is correct |
61 |
Correct |
30 ms |
25948 KB |
Output is correct |
62 |
Correct |
29 ms |
25948 KB |
Output is correct |
63 |
Runtime error |
463 ms |
1048576 KB |
Execution killed with signal 9 |
64 |
Halted |
0 ms |
0 KB |
- |