///*** Sown_Vipro ***///
/// ->TEAM SELECTION TEST<- ///
#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("popcnt")
#define F first
#define S second
#define pb push_back
#define pi pair<int, int>
#define pii pair<int, pair<int, int> >
#define FOR(i, a, b) for(int i = a; i <= b; ++i)
#define REP(i, a, b) for(int i = a; i >= b; --i)
#define all(s) s.begin(), s.end()
#define szz(s) int(s.size())
#define int long long
const string NAME = "sown";
const int N = 1e6 + 5, MAX = 1e6, MOD = 1e9 + 7;
void maxi(int &x, int y){ if(x < y) x = y; }
void mini(int &x, int y){ if(x > y) x = y; };
void add(int &x, int y){ x += y; x += MOD * (x < 0); x -= MOD * (x >= MOD); };
void fixmod(int &x){
x %= MOD;
}
int n;
int a[N], b[N], c[N], lt[N];
namespace sub1{
int l[N], r[N];
void solve(){
int res = 0;
FOR(mask, 0, (1 << n) - 1){
FOR(i, 1, n){
if(mask & (1 << i - 1)) c[i] = b[i];
else c[i] = a[i];
l[i] = max(l[i - 1], c[i]);
}
REP(i, n, 1){
r[i] = max(r[i + 1], c[i]);
// cout << l[i] << " " << r[i] << "\n";
add(res, min(l[i], r[i]) - c[i]);
}
// break;
}
cout << res;
}
}
namespace sub2{
int res;
int suf[2][1005][1005];
void calc(int i, int x, int t){
int pre = 1;
FOR(j, 1, i - 1){
pre = 1ll * pre * ((a[j] <= x) + (b[j] <= x)) % MOD;
}
FOR(j, i + 1, n - 1){
if(a[j] < x){
int tmp = 1ll * pre * suf[t][i][j + 1] % MOD * (x - a[j]) % MOD;
res = (res + tmp) % MOD;
}
if(b[j] < x){
int tmp = 1ll * pre * suf[t][i][j + 1] % MOD * (x - b[j]) % MOD;
res = (res + tmp) % MOD;
}
pre = 1ll * pre * ((a[j] < x) + (b[j] < x)) % MOD;
if(a[j] >= x && b[j] >= x) break;
}
}
void solve(){
lt[0] = 1;
FOR(i, 1, n) lt[i] = lt[i - 1] * 2 % MOD;
FOR(i, 1, n){
// FOR(j, i + 1, n){
// int ans = 1;
// FOR(k, j, n){
// int tmp = ans;
// tmp = 1ll * tmp * ((a[k] >= a[i]) + (b[k] >= a[i])) % MOD;
// tmp = 1ll * tmp * lt[n - k] % MOD;
//
// suf[0][i][j] = (suf[0][i][j] + tmp) % MOD;
// ans = 1ll * ans * ((a[k] < a[i]) + (b[k] < a[i])) % MOD;
// }
//
// ans = 1;
// FOR(k, j, n){
// int tmp = ans;
// tmp = 1ll * tmp * ((a[k] >= b[i]) + (b[k] >= b[i])) % MOD;
// tmp = 1ll * tmp * lt[n - k] % MOD;
//
// suf[1][i][j] = (suf[1][i][j] + tmp) % MOD;
// ans = 1ll * ans * ((a[k] < b[i]) + (b[k] < b[i])) % MOD;
// }
// }
}
FOR(i, 1, n){
calc(i, a[i], 0);
calc(i, b[i], 1);
}
reverse(a + 1, a + 1 + n);
reverse(b + 1, b + 1 + n);
FOR(i, 1, n){
FOR(j, i + 1, n){
int ans = 1;
suf[0][i][j] = suf[1][i][j] = 0;
FOR(k, j, n){
int tmp = ans;
tmp = 1ll * tmp * ((a[k] > a[i]) + (b[k] > a[i])) % MOD;
tmp = 1ll * tmp * lt[n - k] % MOD;
suf[0][i][j] = (suf[0][i][j] + tmp) % MOD;
ans = 1ll * ans * ((a[k] <= a[i]) + (b[k] <= a[i])) % MOD;
}
ans = 1;
FOR(k, j, n){
int tmp = ans;
tmp = 1ll * tmp * ((a[k] > b[i]) + (b[k] > b[i])) % MOD;
tmp = 1ll * tmp * lt[n - k] % MOD;
suf[1][i][j] = (suf[1][i][j] + tmp) % MOD;
ans = 1ll * ans * ((a[k] <= b[i]) + (b[k] <= b[i])) % MOD;
}
}
}
FOR(i, 1, n){
calc(i, a[i], 0);
calc(i, b[i], 1);
}
cout << res;
}
}
void solve(){
cin >> n;
FOR(i, 1, n) cin >> a[i];
FOR(i, 1, n) cin >> b[i];
if(n <= 100) sub2::solve();
// if(n <= 20) sub1::solve();
// else sub2::solve();
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
if(fopen((NAME + ".inp").c_str(), "r")){
freopen((NAME + ".inp").c_str(), "r", stdin);
freopen((NAME + ".out").c_str(), "w", stdout);
}
int t = 1;
// cin >> t;
while(t--){
solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int main()':
Main.cpp:162:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
162 | freopen((NAME + ".inp").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:163:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
163 | freopen((NAME + ".out").c_str(), "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... |