#include<bits/stdc++.h>
using namespace std;
bool M1;
#define PI 3.14159265358979323846
#define sz(a) (int)a.size()
#define all(x) x.begin(),x.end()
#define ii pair<int,int>
#define iii pair<int,ii>
#define iv pair<ii,ii>
#define se second
#define fi first
#define ffi fi.fi
#define sfi se.fi
#define sse se.se
#define fse fi.se
#define lt(i, c, d) for(int i = c; i <= d; ++i)
#define fl(i, c, d) for(int i = d; i >= c; --i)
#define pb push_back
#define emb emplace_back
#define emf emplace_front
#define em emplace
#define look_memory cerr<<abs(&M2-&M1)/1024.0/1024<<'\n'
#define look_time cerr << "TIME : " << clock() * 0.001 << "s" <<'\n'
const int N=1e6+5,lg=30,mod=1e9+7;
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
int Rand(int u,int v){
return u+rd()%(v-u+1);
}
int dx[]={1,0,-1,0,1,1,-1,-1};
int dy[]={0,-1,0,1,1,-1,1,-1};
int node,a[N],b[N],ans,po[N];
struct segsum1 {
struct pt{
int ans,siz,suf;
pt(){
ans=0,siz=1,suf=0;
};
};
pt combine(pt x,pt y){
pt t;
t.siz=x.siz+y.siz;
t.ans=(1LL*x.ans*y.suf%mod+1LL*y.ans*po[x.siz]%mod)%mod;
t.suf=1LL*x.suf*y.suf%mod;
return t;
}
int n;
vector<pt> st;
segsum1() {};
segsum1(int _n): n(_n), st(n * 4 + 5){};
void build(int id, int l, int r) {
if (l == r) {
st[id] = pt();
return;
}
int mid = (l + r) >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
st[id] = combine(st[id << 1] , st[id << 1 | 1]);
}
void update(int id, int l, int r, int i) {
if (l ==r) {
st[id].ans++;
st[id].suf++;
return;
}
int mid = (l + r) >> 1;
if(i<=mid)
update(id << 1, l, mid, i);
else
update(id << 1 | 1, mid + 1, r, i);
st[id] = combine(st[id << 1] , st[id << 1 | 1]);
}
};
struct segsum2 {
struct pt{
int ans,siz,pre;
pt(){
ans=0,siz=1,pre=0;
};
};
pt combine(pt x,pt y){
pt t;
t.siz=x.siz+y.siz;
t.ans=(1LL*y.ans*x.pre%mod+1LL*x.ans*po[y.siz]%mod)%mod;
t.pre=1LL*x.pre*y.pre%mod;
return t;
}
int n;
vector<pt> st;
segsum2() {};
segsum2(int _n): n(_n), st(n * 4 + 5){};
void build(int id, int l, int r) {
if (l == r) {
st[id] = pt();
return;
}
int mid = (l + r) >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
st[id] = combine(st[id << 1] , st[id << 1 | 1]);
}
void update(int id, int l, int r, int i) {
if (l ==r) {
st[id].ans++;
st[id].pre++;
return;
}
int mid = (l + r) >> 1;
if(i<=mid)
update(id << 1, l, mid, i);
else
update(id << 1 | 1, mid + 1, r, i);
st[id] = combine(st[id << 1] , st[id << 1 | 1]);
}
};
struct segsum3 {
struct pt{
int ans,suf,pre;
pt(){
ans=0,suf=0,pre=0;
};
};
pt combine(pt x,pt y){
pt t;
t.ans=(1LL*y.ans*x.pre%mod+1LL*x.ans*y.suf%mod)%mod;
t.pre=1LL*x.pre*y.pre%mod;
t.suf=1LL*x.suf*y.suf%mod;
return t;
}
int n;
vector<pt> st;
segsum3() {};
segsum3(int _n): n(_n), st(n * 4 + 5){};
void build(int id, int l, int r) {
if (l == r) {
st[id] = pt();
return;
}
int mid = (l + r) >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
st[id] = combine(st[id << 1] , st[id << 1 | 1]);
}
void update(int id, int l, int r, int i) {
if (l ==r) {
st[id].ans++;
st[id].pre++;
st[id].suf++;
return;
}
int mid = (l + r) >> 1;
if(i<=mid)
update(id << 1, l, mid, i);
else
update(id << 1 | 1, mid + 1, r, i);
st[id] = combine(st[id << 1] , st[id << 1 | 1]);
}
};
vector<int>nen,req[N];
int ne(int u){
return lower_bound(all(nen),u)-nen.begin();
}
bool M2;
void solve(){
nen.emb(0);
cin >> node;
po[0]=1;
for(int i=1;i<=node;++i){
cin >> a[i];
nen.emb(a[i]);
po[i]=2LL*po[i-1]%mod;
}
for(int i=1;i<=node;++i){
cin >> b[i];
nen.emb(b[i]);
}
sort(all(nen));
nen.erase(unique(all(nen)),nen.end());
for(int i=1;i<=node;++i){
req[ne(a[i])].emb(i);
req[ne(b[i])].emb(i);
}
segsum1 seg1(node);
segsum2 seg2(node);
segsum3 seg3(node);
seg1.build(1,1,node);
seg2.build(1,1,node);
seg3.build(1,1,node);
int n=sz(nen);
for(int i=1;i<n;++i){
int kc=nen[i]-nen[i-1];
ans=((ans-1LL*seg1.st[1].ans*kc%mod)%mod+mod)%mod;
ans=((ans-1LL*seg2.st[1].ans*kc%mod)%mod+mod)%mod;
ans=((ans+1LL*seg3.st[1].ans*kc%mod)%mod+mod)%mod;
for(auto x:req[i]){
seg1.update(1,1,node,x);
seg2.update(1,1,node,x);
seg3.update(1,1,node,x);
}
}
int val=po[node-1];
for(int i=1;i<=node;++i){
ans=(ans+1LL*val*(nen.back()-a[i])%mod)%mod;
ans=(ans+1LL*val*(nen.back()-b[i])%mod)%mod;
}
cout << ans;
}
main()
{
srand(time(0));
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#define task "aws"
if(fopen(task".inp","r")){
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
int t=1;
// cin >> t;
while(t--){
solve();cout<<'\n';
}
look_memory;
look_time;
}
Compilation message (stderr)
Main.cpp:216:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
216 | main()
| ^~~~
Main.cpp: In function 'int main()':
Main.cpp:224:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
224 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:225:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
225 | 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... |