#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define F first
#define S second
#define sz(s) (int)s.size()
#define in insert
#define all(v) v.begin(),v.end()
#define lb lower_bound
#define int ll
const int MAX=1e6+10;
const int mod=1e9+7;
int n;
int a[MAX],b[MAX];
vector<int> vec;
struct segtree{
int t[4*MAX],t1[4*MAX],add[4*MAX];
void init(int n){
for(int i=1;i<=4*n;i++){
t[i]=t1[i]=0;
add[i]=1;
}
}
void upd(int v,int x){
t[v]=t[v]*x%mod;
t1[v]=t1[v]*x%mod;
add[v]=add[v]*x%mod;
}
void push(int v){
if(add[v]!=1){
upd(2*v,add[v]);
upd(2*v+1,add[v]);
add[v]=1;
}
}
void update(int v,int tl,int tr,int l,int r,int x){
if(l>r||tl>r||l>tr)return;
if(l<=tl&&tr<=r){
upd(v,x);
return;
}
push(v);
int tm=(tl+tr)/2;
update(2*v,tl,tm,l,r,x);
update(2*v+1,tm+1,tr,l,r,x);
t[v]=(t[2*v]+t[2*v+1])%mod;
t1[v]=(t1[2*v]+t1[2*v+1])%mod;
}
void set(int v,int tl,int tr,int pos,int x){
if(tl==tr){
t[v]=x;
t1[v]=vec[tl]*x%mod;
return;
}
push(v);
int tm=(tl+tr)/2;
if(pos<=tm)set(2*v,tl,tm,pos,x);
else set(2*v+1,tm+1,tr,pos,x);
t[v]=(t[2*v]+t[2*v+1])%mod;
t1[v]=(t1[2*v]+t1[2*v+1])%mod;
}
int get(int v,int tl,int tr,int l,int r){
if(l>r||tl>r||l>tr)return 0;
if(l<=tl&&tr<=r)return t[v];
push(v);
int tm=(tl+tr)/2;
return (get(2*v,tl,tm,l,r)+get(2*v+1,tm+1,tr,l,r))%mod;
}
int getT(int v,int tl,int tr,int l,int r){
if(l>r||tl>r||l>tr)return 0;
if(l<=tl&&tr<=r)return t1[v];
push(v);
int tm=(tl+tr)/2;
return (getT(2*v,tl,tm,l,r)+getT(2*v+1,tm+1,tr,l,r))%mod;
}
}T;
int pw[MAX];
void solve(){
cin>>n;
pw[0]=1;
for(int i=1;i<=n;i++)pw[i]=(pw[i-1]*2)%mod;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++)cin>>b[i];
vec.pb(0);
for(int i=1;i<=n;i++)vec.pb(a[i]),vec.pb(b[i]);
sort(all(vec));
vec.erase(unique(all(vec)),vec.end());
for(int i=1;i<=n;i++){
a[i]=lb(all(vec),a[i])-vec.begin();
b[i]=lb(all(vec),b[i])-vec.begin();
if(a[i]>b[i])swap(a[i],b[i]);
}
int ans=0;
for(int i=1;i<=n;i++){
ans-=(vec[a[i]]+vec[b[i]])*pw[n-1]%mod;
if(ans<0)ans+=mod;
}
T.init(2*n+1);
T.set(1,0,2*n,0,1);
for(int i=1;i<=n;i++){
int x=T.get(1,0,2*n,0,a[i]);
int y=(T.get(1,0,2*n,0,b[i])+T.get(1,0,2*n,b[i],b[i]))%mod;
T.set(1,0,2*n,a[i],x);
T.set(1,0,2*n,b[i],y);
T.update(1,0,2*n,0,a[i]-1,0);
T.update(1,0,2*n,b[i]+1,2*n,2);
ans+=T.getT(1,0,2*n,0,2*n)*pw[n-i]%mod;
if(ans>=mod)ans-=mod;
}
ans-=T.getT(1,0,2*n,0,2*n)*n%mod;
if(ans<0)ans+=mod;
T.init(2*n+1);
T.set(1,0,2*n,0,1);
for(int i=n;i>=1;i--){
int x=T.get(1,0,2*n,0,a[i]);
int y=(T.get(1,0,2*n,0,b[i])+T.get(1,0,2*n,b[i],b[i]))%mod;
T.set(1,0,2*n,a[i],x);
T.set(1,0,2*n,b[i],y);
T.update(1,0,2*n,0,a[i]-1,0);
T.update(1,0,2*n,b[i]+1,2*n,2);
ans+=T.getT(1,0,2*n,0,2*n)*pw[i-1]%mod;
if(ans>=mod)ans-=mod;
}
cout<<ans<<"\n";
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
// cin>>t;
while(t--)solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
600 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
472 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
600 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
408 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
600 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
408 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
24 ms |
2780 KB |
Output is correct |
28 |
Correct |
25 ms |
2784 KB |
Output is correct |
29 |
Correct |
18 ms |
2784 KB |
Output is correct |
30 |
Correct |
15 ms |
2792 KB |
Output is correct |
31 |
Correct |
17 ms |
2784 KB |
Output is correct |
32 |
Correct |
17 ms |
2784 KB |
Output is correct |
33 |
Correct |
16 ms |
2796 KB |
Output is correct |
34 |
Correct |
17 ms |
2800 KB |
Output is correct |
35 |
Correct |
17 ms |
2784 KB |
Output is correct |
36 |
Correct |
17 ms |
2784 KB |
Output is correct |
37 |
Correct |
15 ms |
2784 KB |
Output is correct |
38 |
Correct |
17 ms |
2784 KB |
Output is correct |
39 |
Correct |
17 ms |
2784 KB |
Output is correct |
40 |
Correct |
16 ms |
2784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
600 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
472 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
344 KB |
Output is correct |
44 |
Correct |
0 ms |
408 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
24 ms |
2780 KB |
Output is correct |
50 |
Correct |
25 ms |
2784 KB |
Output is correct |
51 |
Correct |
18 ms |
2784 KB |
Output is correct |
52 |
Correct |
15 ms |
2792 KB |
Output is correct |
53 |
Correct |
17 ms |
2784 KB |
Output is correct |
54 |
Correct |
17 ms |
2784 KB |
Output is correct |
55 |
Correct |
16 ms |
2796 KB |
Output is correct |
56 |
Correct |
17 ms |
2800 KB |
Output is correct |
57 |
Correct |
17 ms |
2784 KB |
Output is correct |
58 |
Correct |
17 ms |
2784 KB |
Output is correct |
59 |
Correct |
15 ms |
2784 KB |
Output is correct |
60 |
Correct |
17 ms |
2784 KB |
Output is correct |
61 |
Correct |
17 ms |
2784 KB |
Output is correct |
62 |
Correct |
16 ms |
2784 KB |
Output is correct |
63 |
Correct |
33 ms |
3040 KB |
Output is correct |
64 |
Correct |
35 ms |
3040 KB |
Output is correct |
65 |
Correct |
17 ms |
3040 KB |
Output is correct |
66 |
Correct |
17 ms |
2784 KB |
Output is correct |
67 |
Correct |
20 ms |
3036 KB |
Output is correct |
68 |
Correct |
17 ms |
2784 KB |
Output is correct |
69 |
Correct |
19 ms |
2876 KB |
Output is correct |
70 |
Correct |
19 ms |
2880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
15 ms |
2784 KB |
Output is correct |
11 |
Correct |
15 ms |
2784 KB |
Output is correct |
12 |
Correct |
15 ms |
2780 KB |
Output is correct |
13 |
Correct |
15 ms |
2784 KB |
Output is correct |
14 |
Correct |
1162 ms |
114060 KB |
Output is correct |
15 |
Correct |
1160 ms |
114280 KB |
Output is correct |
16 |
Correct |
1148 ms |
113844 KB |
Output is correct |
17 |
Correct |
1150 ms |
114080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
600 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
472 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
344 KB |
Output is correct |
44 |
Correct |
0 ms |
408 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
24 ms |
2780 KB |
Output is correct |
50 |
Correct |
25 ms |
2784 KB |
Output is correct |
51 |
Correct |
18 ms |
2784 KB |
Output is correct |
52 |
Correct |
15 ms |
2792 KB |
Output is correct |
53 |
Correct |
17 ms |
2784 KB |
Output is correct |
54 |
Correct |
17 ms |
2784 KB |
Output is correct |
55 |
Correct |
16 ms |
2796 KB |
Output is correct |
56 |
Correct |
17 ms |
2800 KB |
Output is correct |
57 |
Correct |
17 ms |
2784 KB |
Output is correct |
58 |
Correct |
17 ms |
2784 KB |
Output is correct |
59 |
Correct |
15 ms |
2784 KB |
Output is correct |
60 |
Correct |
17 ms |
2784 KB |
Output is correct |
61 |
Correct |
17 ms |
2784 KB |
Output is correct |
62 |
Correct |
16 ms |
2784 KB |
Output is correct |
63 |
Correct |
33 ms |
3040 KB |
Output is correct |
64 |
Correct |
35 ms |
3040 KB |
Output is correct |
65 |
Correct |
17 ms |
3040 KB |
Output is correct |
66 |
Correct |
17 ms |
2784 KB |
Output is correct |
67 |
Correct |
20 ms |
3036 KB |
Output is correct |
68 |
Correct |
17 ms |
2784 KB |
Output is correct |
69 |
Correct |
19 ms |
2876 KB |
Output is correct |
70 |
Correct |
19 ms |
2880 KB |
Output is correct |
71 |
Correct |
0 ms |
344 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
0 ms |
344 KB |
Output is correct |
74 |
Correct |
0 ms |
348 KB |
Output is correct |
75 |
Correct |
0 ms |
348 KB |
Output is correct |
76 |
Correct |
1 ms |
344 KB |
Output is correct |
77 |
Correct |
0 ms |
348 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
0 ms |
348 KB |
Output is correct |
80 |
Correct |
15 ms |
2784 KB |
Output is correct |
81 |
Correct |
15 ms |
2784 KB |
Output is correct |
82 |
Correct |
15 ms |
2780 KB |
Output is correct |
83 |
Correct |
15 ms |
2784 KB |
Output is correct |
84 |
Correct |
1162 ms |
114060 KB |
Output is correct |
85 |
Correct |
1160 ms |
114280 KB |
Output is correct |
86 |
Correct |
1148 ms |
113844 KB |
Output is correct |
87 |
Correct |
1150 ms |
114080 KB |
Output is correct |
88 |
Correct |
2885 ms |
123572 KB |
Output is correct |
89 |
Correct |
2886 ms |
123548 KB |
Output is correct |
90 |
Correct |
1244 ms |
116136 KB |
Output is correct |
91 |
Correct |
1224 ms |
123828 KB |
Output is correct |
92 |
Correct |
1226 ms |
116072 KB |
Output is correct |
93 |
Correct |
1194 ms |
116012 KB |
Output is correct |
94 |
Correct |
1245 ms |
123952 KB |
Output is correct |
95 |
Correct |
1195 ms |
116048 KB |
Output is correct |
96 |
Correct |
1281 ms |
119736 KB |
Output is correct |
97 |
Correct |
1266 ms |
119732 KB |
Output is correct |