// ~~ icebear ~~
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
template<class T>
bool minimize(T &a, const T &b) {
if (a > b) return a = b, true;
return false;
}
template<class T>
bool maximize(T &a, const T &b) {
if (a < b) return a = b, true;
return false;
}
#define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
#define FORR(i,a,b) for(int i=(a); i>=(b); --i)
#define REP(i, n) for(int i=0; i<(n); ++i)
#define RED(i, n) for(int i=(n)-1; i>=0; --i)
#define MASK(i) (1LL << (i))
#define BIT(S, i) (((S) >> (i)) & 1)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define task "icebear"
const int MOD = 1e9 + 7;
const int inf = 1e9 + 27092008;
const ll INF = 1e18 + 27092008;
const int N = 5e5 + 5;
int n, a[N], b[N];
vector<int> compress, pL, pR;
vector<vector<int>> dpL, dpR;
void init(void) {
cin >> n;
FOR(i, 1, n) cin >> a[i], compress.pb(a[i]);
FOR(i, 1, n) cin >> b[i], compress.pb(b[i]);
sort(all(compress));
compress.resize(unique(all(compress)) - compress.begin());
FOR(i, 1, n) a[i] = lower_bound(all(compress), a[i]) - compress.begin() + 1;
FOR(i, 1, n) b[i] = lower_bound(all(compress), b[i]) - compress.begin() + 1;
}
void process(void) {
int MAX = (int)compress.size();
dpL.resize(n + 5, vector<int>(MAX + 5, 0));
dpR.resize(n + 5, vector<int>(MAX + 5, 0));
pL.resize(max(n, MAX) + 5, 0);
pR.resize(max(n, MAX) + 5, 0);
dpL[0][0] = dpR[n + 1][0] = 1;
FOR(i, 0, n - 1) FOR(j, 0, MAX) {
(dpL[i + 1][max(a[i + 1], j)] += dpL[i][j]) %= MOD;
(dpL[i + 1][max(b[i + 1], j)] += dpL[i][j]) %= MOD;
}
FORR(i, n + 1, 2) FOR(j, 0, MAX) {
(dpR[i - 1][max(a[i - 1], j)] += dpR[i][j]) %= MOD;
(dpR[i - 1][max(b[i - 1], j)] += dpR[i][j]) %= MOD;
}
int ans = 0;
FOR(i, 2, n - 1) {
FOR(j, 1, MAX) pL[j] = (pL[j - 1] + dpL[i - 1][j]) % MOD;
FOR(j, 1, MAX) pR[j] = (pR[j - 1] + dpR[i + 1][j]) % MOD;
FOR(j, 1, MAX) if (dpL[i - 1][j] > 0) {
int v1 = max(0, compress[j - 1] - compress[a[i] - 1]);
int v2 = max(0, compress[j - 1] - compress[b[i] - 1]);
(ans += 1LL * (v1 + v2) * dpL[i - 1][j] % MOD * (pR[MAX] - pR[j - 1] + MOD) % MOD) %= MOD;
}
FOR(k, 1, MAX) if (dpR[i + 1][k] > 0) {
int v1 = max(0, compress[k - 1] - compress[a[i] - 1]);
int v2 = max(0, compress[k - 1] - compress[b[i] - 1]);
(ans += 1LL * (v1 + v2) * dpR[i + 1][k] % MOD * (pL[MAX] - pL[k] + MOD) % MOD) %= MOD;
}
}
cout << ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int tc = 1;
// cin >> tc;
while(tc--) {
init();
process();
}
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:93:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:94:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
94 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |