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 rall(s) s.rbegin(), s.rend()
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()
#define s second
#define f first
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int N = 1e6, mod = 1e9 + 7;
const int inf = 1e9;
int n;
int a[N][2];
void f() {
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j < i; j++) {
for (int k = i + 1; k <= n; k++) {
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 2; y++) {
int v = 1;
for (int s = 1; s <= n; s++) {
if (s == i || s == j || s == k) continue;
int cnt = 0;
if (s < i) {
if (s < j) {
if (a[s][1] < a[j][x]) cnt = 2;
else if (a[s][0] < a[j][x]) cnt = 1;
else cnt = 0;
}else {
if (a[s][1] <= a[j][x]) cnt = 2;
else if (a[s][0] <= a[j][x]) cnt = 1;
else cnt = 0;
}
}else {
if (s > k) {
if (a[s][1] < a[k][y]) cnt = 2;
else if (a[s][0] < a[k][y]) cnt = 1;
else cnt = 0;
}else {
if (a[s][1] <= a[k][y]) cnt = 2;
else if (a[s][0] <= a[k][y]) cnt = 1;
else cnt = 0;
}
}
v = (v * cnt) % mod;
}
if (!v) continue;
for (int z = 0; z < 2; z++) {
if (a[i][z] > a[j][x] || a[i][z] > a[k][y]) continue;
ans = (ans + ((min(a[j][x], a[k][y]) - a[i][z]) * 1ll * v) % mod) % mod;
}
}
}
}
}
}
cout << ans;
exit(0);
}
void solve() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i][0];
for (int i = 1; i <= n; i++) cin >> a[i][1];
bool ok = 1;
for (int i = 1; i <= n; i++) {
if (a[i][1] < a[i][0]) swap(a[i][1], a[i][0]);
if (a[i][1] > 1000) ok = 0;
}
if (n <= 20) f();
int dp[n + 1][1001], cnt[n + 1][1001];
memset(dp, 0, sizeof(dp));
memset(cnt, 0, sizeof(cnt));
cnt[1][0] = 1;
for (int i = 1; i < n; i++) {
for (int x = 0; x <= 1000; x++) {
int v = a[i][0];
if (v < x) {
int val = (cnt[i][x] * 1ll * (x - v)) % mod;
dp[i + 1][x] = (dp[i + 1][x] + (dp[i][x] + val) % mod) % mod;
cnt[i + 1][x] = (cnt[i + 1][x] + cnt[i][x]) % mod;
}else {
cnt[i + 1][v] = (cnt[i + 1][v] + cnt[i][x]) % mod;
dp[i + 1][v] = (dp[i + 1][v] + dp[i][x]) % mod;
}
v = a[i][1];
if (v < x) {
int val = (cnt[i][x] * 1ll * (x - v)) % mod;
dp[i + 1][x] = (dp[i + 1][x] + (dp[i][x] + val) % mod) % mod;
cnt[i + 1][x] = (cnt[i + 1][x] + cnt[i][x]) % mod;
}else {
cnt[i + 1][v] = (cnt[i + 1][v] + cnt[i][x]) % mod;
dp[i + 1][v] = (dp[i + 1][v] + dp[i][x]) % mod;
}
}
}
int pd[n + 1][1001], ct[n + 1][1001];
memset(pd, 0, sizeof(pd));
memset(ct, 0, sizeof(ct));
ct[n][0] = 1;
for (int i = n; i > 1; i--) {
for (int x = 0; x <= 1000; x++) {
int v = a[i][0];
if (v < x) {
int val = (ct[i][x] * 1ll * (x - v)) % mod;
pd[i - 1][x] = (pd[i - 1][x] + (pd[i][x] + val) % mod) % mod;
ct[i - 1][x] = (ct[i - 1][x] + ct[i][x]) % mod;
}else {
ct[i - 1][v] = (ct[i - 1][v] + ct[i][x]) % mod;
pd[i - 1][v] = (pd[i - 1][v] + pd[i][x]) % mod;
}
v = a[i][1];
if (v < x) {
int val = (ct[i][x] * 1ll * (x - v)) % mod;
pd[i - 1][x] = (pd[i - 1][x] + (pd[i][x] + val) % mod) % mod;
ct[i - 1][x] = (ct[i - 1][x] + ct[i][x]) % mod;
}else {
ct[i - 1][v] = (ct[i - 1][v] + ct[i][x]) % mod;
pd[i - 1][v] = (pd[i - 1][v] + pd[i][x]) % mod;
}
}
}
int ans = 0;
for (int i = 1; i <= n; i++) {
int v = a[i][0];
for (int x = 0; x <= v; x++) {
if (x < v) ans = (ans + dp[i][x]) % mod;
ans = (ans + pd[i][x]) % mod;
}
v = a[i][1];
for (int x = 0; x <= v; x++) {
if (x < v) ans = (ans + dp[i][x]) % mod;
ans = (ans + pd[i][x]) % mod;
}
}
cout << ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
}
Compilation message (stderr)
Main.cpp: In function 'void solve()':
Main.cpp:73:7: warning: variable 'ok' set but not used [-Wunused-but-set-variable]
73 | bool ok = 1;
| ^~
# | 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... |