#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define len(s) (ll) s.size()
#define pb push_back
#define F first
#define S second
using namespace std;
typedef long long ll;
const int N = 1e4 + 3;
const int MAX = 2e4 + 11;
const int P = 31;
const int mod = 1e9 + 7;
const ll inf = 1e18;
int n, sz, res, x, i, j, l, r, cur1, cur2;
int a[N], b[N], ia[N], ib[N], p[MAX], s[MAX], val[MAX];
int dp[N][MAX][2];
vector < int > c;
map < int, int > mp;
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for(i = 1; i <= n; i++) cin >> a[i], c.pb(a[i]);
for(i = 1; i <= n; i++) cin >> b[i], c.pb(b[i]);
sort(all(c));
for(int vl : c)
if(!mp[vl])
mp[vl] = ++sz, val[sz] = vl;
for(i = 1; i <= n; i++) ia[i] = a[i], a[i] = mp[a[i]], ib[i] = b[i], b[i] = mp[b[i]];
for(int fg = 0; fg < 2; fg++){
if(fg){
reverse(a + 1, a + n + 1);
reverse(b + 1, b + n + 1);
}
dp[0][0][fg] = 1;
for(x = 0; x <= sz; x++) p[x] = 1;
for(i = 1; i <= n; i++){
for(x = sz; x >= 0; x--){
int &y = dp[i][x][fg];
if(a[i] > x && b[i] > x) y = 0;
else if(a[i] > x || b[i] > x){
if(a[i] == x || b[i] == x) y = p[x];
else y = dp[i - 1][x][fg];
}
else{
if(a[i] == x && b[i] == x) y = p[x] * 2;
else if(a[i] == x || b[i] == x) y = p[x] + dp[i - 1][x][fg];
else y = dp[i - 1][x][fg] * 2;
}
y %= mod;
}
for(x = 0; x <= sz; x++){
if(x) p[x] = (p[x - 1] + dp[i][x][fg]) % mod;
else p[x] = dp[i][x][fg];
}
}
if(fg){
reverse(a + 1, a + n + 1);
reverse(b + 1, b + n + 1);
}
}
for(i = 2; i < n; i++){
l = i - 1, r = n - i, cur1 = 0, cur2 = 0;
for(x = sz; x >= 0; x--){
s[x] = (s[x + 1] + dp[r][x][1]) % mod;
}
for(x = 0; x <= sz; x++){
res += (dp[l][x][0] * 1ll * cur1) % mod, res %= mod;
res += (dp[l][x][0] * 1ll * cur2) % mod, res %= mod;
if(x > a[i])
res += ((dp[l][x][0] * 1ll * s[x]) % mod * 1ll * (val[x] - ia[i])) % mod, res %= mod,
cur1 += (dp[r][x][1] * 1ll * (val[x] - ia[i])) % mod, cur1 %= mod;
if(x > b[i])
res += ((dp[l][x][0] * 1ll * s[x]) % mod * 1ll * (val[x] - ib[i])) % mod, res %= mod,
cur2 += (dp[r][x][1] * 1ll * (val[x] - ib[i])) % mod, cur2 %= mod;
}
}
cout << res;
}
# | 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... |