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>
#define fori(i,a,b) for(int i=a;i<=b;i++)
#define int long long
#define pb push_back
using namespace std;
typedef pair<int,int> ii;
typedef tuple<int,int,int> tp;
const int mod=1e9+7;
const int M=5e5+10;
int n,a[M],b[M];
namespace sub1
{
const int M=30;
int x[M],y[M],kq=0;
void ktra()
{
fori(i,1,n) y[i]=1e9;
int l=0,r=0;
fori(i,1,n) {
l=max(l,x[i]);
y[i]=min(y[i],l);
}
for(int i=n;i!=0;i--) {
r=max(r,x[i]);
y[i]=min(y[i],r);
}
fori(i,1,n) kq=(kq+max(0ll,y[i]-x[i]))%mod;
}
void duyet(int i)
{
if(i==n+1) ktra();
else {
fori(j,0,1) {
if(j) x[i]=a[i];
else x[i]=b[i];
duyet(i+1);
}
}
}
void solve()
{
duyet(1);
cout << kq ;
}
}
namespace sub3
{
const int M=1e4+2;
const int N=2e4+2;
int f[2][N],g[M][N],kq=0;
vector<int> rrh;
void add(int &x,int y)
{
x+=y;
if(x>=mod) x-=mod;
}
void solve()
{
fori(i,1,n) {
rrh.pb(a[i]);
rrh.pb(b[i]);
}
sort(rrh.begin(),rrh.end());
rrh.resize(unique(rrh.begin(),rrh.end())-rrh.begin());
int lim=rrh.size()-1;
fori(i,1,n) {
a[i]=lower_bound(rrh.begin(),rrh.end(),a[i])-rrh.begin();
b[i]=lower_bound(rrh.begin(),rrh.end(),b[i])-rrh.begin();
}
g[n+1][0]=1;
for(int i=n;i!=0;i--) {
fori(j,0,lim) {
int val=max(a[i],j);
int val2=max(b[i],j);
add(g[i][val],g[i+1][j]);
add(g[i][val2],g[i+1][j]);
}
}
f[0][0]=1;
fori(i,1,n) {
fori(j,0,lim) {
int val=max(a[i],j);
int val2=max(b[i],j);
add(f[i&1][val],f[i-1&1][j]);
add(f[i&1][val2],f[i-1&1][j]);
}
int sum=0;
for(int j=lim;j>=min(a[i],b[i]);j--) {
add(sum,g[i+1][j]);
if(a[i]<=j) kq=(kq+(f[i-1&1][j]*sum)%mod*(rrh[j]-rrh[a[i]])%mod)%mod;
if(b[i]<=j) kq=(kq+(f[i-1&1][j]*sum)%mod*(rrh[j]-rrh[b[i]])%mod)%mod;
}
sum=0;
for(int j=lim;j>=min(a[i],b[i]);j--) {
if(a[i]<=j) kq=(kq+(sum*g[i+1][j])%mod*(rrh[j]-rrh[a[i]])%mod)%mod;
if(b[i]<=j) kq=(kq+(sum*g[i+1][j])%mod*(rrh[j]-rrh[b[i]])%mod)%mod;
add(sum,f[i-1&1][j]);
}
fori(j,0,lim) f[i-1&1][j]=0;
}
cout << kq;
}
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen("1.inp","r"))
{
freopen("1.inp","r",stdin);
freopen("1.out","w",stdout);
}
#define task ""
if(fopen(task".inp","r"))
{
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin >> n;
fori(i,1,n) cin >> a[i];
fori(i,1,n) cin >> b[i];
if(n<=20) sub1::solve();
else sub3::solve();
//sub3::solve();
}
Compilation message (stderr)
Main.cpp: In function 'void sub3::solve()':
Main.cpp:87:28: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
87 | add(f[i&1][val],f[i-1&1][j]);
| ~^~
Main.cpp:88:29: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
88 | add(f[i&1][val2],f[i-1&1][j]);
| ~^~
Main.cpp:95:34: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
95 | if(a[i]<=j) kq=(kq+(f[i-1&1][j]*sum)%mod*(rrh[j]-rrh[a[i]])%mod)%mod;
| ~^~
Main.cpp:96:34: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
96 | if(b[i]<=j) kq=(kq+(f[i-1&1][j]*sum)%mod*(rrh[j]-rrh[b[i]])%mod)%mod;
| ~^~
Main.cpp:102:20: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
102 | add(sum,f[i-1&1][j]);
| ~^~
Main.cpp:106:24: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
106 | fori(j,0,lim) f[i-1&1][j]=0;
| ~^~
Main.cpp: In function 'int32_t main()':
Main.cpp:119:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
119 | freopen("1.inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~
Main.cpp:120:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
120 | freopen("1.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
Main.cpp:125:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
125 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:126:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
126 | 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... |