#include<bits/stdc++.h>
#include <chrono>
#include <random>
using namespace std;
#define F first
#define S second
#define pb push_back
#define vll vector<ll>
#define pll pair<ll, ll>
typedef long long ll;
const ll mxN=5e5+5;
const ll mod=1e9+7;
ll pw[mxN];
struct segtree{
vll tree, lazy;
ll treelen;
void init(ll siz){
treelen=siz+1;
while(__builtin_popcount(treelen)!=1){
treelen++;
}
tree.resize(2*treelen);
lazy=vll(2*treelen, 1);
}
void build(ll idx, ll low, ll high, vll &a){
if(low==high){
if(low<a.size()) tree[idx]=a[low];
else tree[idx]=0;
return;
}
ll mid=(low+high)/2;
build(2*idx, low, mid, a);
build(2*idx+1, mid+1, high, a);
tree[idx]=tree[2*idx]+tree[2*idx+1];
tree[idx]%=mod;
}
void push(ll idx, ll low, ll high){
if(low!=high){
lazy[2*idx]*=lazy[idx];
lazy[2*idx]%=mod;
lazy[2*idx+1]*=lazy[idx];
lazy[2*idx+1]%=mod;
}
}
void check(ll idx, ll low, ll high){
tree[idx]*=lazy[idx];
tree[idx]%=mod;
push(idx, low, high);
lazy[idx]=1;
}
void update1(ll idx, ll low, ll high, ll qlow, ll qhigh, ll val){
check(idx, low, high);
if(low>=qlow && high<=qhigh){
lazy[idx]*=val;
lazy[idx]%=mod;
check(idx, low, high);
return;
}
if(low>qhigh || high<qlow){
return;
}
ll mid=(low+high)/2;
update1(2*idx, low, mid, qlow, qhigh, val);
update1(2*idx+1, mid+1, high, qlow, qhigh, val);
tree[idx]=tree[2*idx]+tree[2*idx+1];
tree[idx]%=mod;
}
ll getsum1(ll idx, ll low, ll high, ll qlow, ll qhigh){
check(idx, low, high);
if(low>=qlow && high<=qhigh){
return tree[idx];
}
if(low>qhigh || high<qlow){
return 0LL;
}
ll mid=(low+high)/2;
return (getsum1(2*idx, low, mid, qlow, qhigh)+
getsum1(2*idx+1, mid+1, high, qlow, qhigh))%mod;
}
void update(ll qlow, ll qhigh, ll val){
update1(1, 0, treelen-1, qlow, qhigh, val);
}
ll getsum(ll qlow, ll qhigh){
return getsum1(1, 0, treelen-1, qlow, qhigh);
}
};
segtree seg;
vll con;
ll id(ll tar){
return lower_bound(con.begin(), con.end(), tar)-con.begin();
}
void solve(){
ll n;
cin>>n;
vector<pll> a(n);
for(auto &[x, y]:a){
cin>>x;
}
for(auto &[x, y]:a){
cin>>y;
}
for(auto &[x, y]:a){
if(x>y){
swap(x, y);
}
}
for(auto &[x, y]:a){
con.pb(x);
con.pb(y);
}
sort(con.begin(), con.end());
con.erase(unique(con.begin(), con.end()), con.end());
ll siz=con.size();
vll bas(siz);
bas[0]=con[0];
for(ll i=1;i<siz;i++){
bas[i]=con[i]-con[i-1];
}
seg.init(siz);
seg.build(1, 0, seg.treelen-1, bas);
ll ans=0;
vll cnt(siz);
for(auto &[x, y]:a){
cnt[id(x)]++;
cnt[id(y)]++;
}
ll pre=0;
for(ll i=0;i<siz;i++){
ans+=((pre*pw[n-1])%mod)*bas[i];
ans%=mod;
pre+=cnt[i];
}
for(ll i=0;i<n;i++){
ll id1=id(a[i].F);
ll id2=id(a[i].S);
seg.update(0, id1, 0);
if(id2+1<siz) seg.update(id2+1, siz-1, 2);
ans-=seg.getsum(0, siz-1)*pw[n-i-1];
ans%=mod;
if(ans<0) ans+=mod;
}
seg.init(siz);
seg.build(1, 0, seg.treelen-1, bas);
for(ll i=n-1;i>=0;i--){
ll id1=id(a[i].F);
ll id2=id(a[i].S);
seg.update(0, id1, 0);
if(id2+1<siz) seg.update(id2+1, siz-1, 2);
ans-=seg.getsum(0, siz-1)*pw[i];
ans%=mod;
if(ans<0) ans+=mod;
}
ans+=seg.getsum(0, siz-1)*n;
ans%=mod;
cout<<ans<<'\n';
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
pw[0]=1;
for(ll i=1;i<mxN;i++){
pw[i]=pw[i-1]*2;
pw[i]%=mod;
}
solve();
return 0;
}
Compilation message
Main.cpp: In member function 'void segtree::build(ll, ll, ll, std::vector<long long int>&)':
Main.cpp:33:10: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | if(low<a.size()) tree[idx]=a[low];
| ~~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4188 KB |
Output is correct |
2 |
Correct |
3 ms |
4144 KB |
Output is correct |
3 |
Correct |
3 ms |
4188 KB |
Output is correct |
4 |
Correct |
3 ms |
4188 KB |
Output is correct |
5 |
Correct |
3 ms |
4184 KB |
Output is correct |
6 |
Correct |
3 ms |
4188 KB |
Output is correct |
7 |
Correct |
3 ms |
4216 KB |
Output is correct |
8 |
Correct |
3 ms |
4188 KB |
Output is correct |
9 |
Correct |
3 ms |
4188 KB |
Output is correct |
10 |
Correct |
3 ms |
4212 KB |
Output is correct |
11 |
Correct |
3 ms |
4188 KB |
Output is correct |
12 |
Correct |
3 ms |
4184 KB |
Output is correct |
13 |
Correct |
3 ms |
4188 KB |
Output is correct |
14 |
Correct |
3 ms |
4188 KB |
Output is correct |
15 |
Correct |
3 ms |
4188 KB |
Output is correct |
16 |
Correct |
4 ms |
4188 KB |
Output is correct |
17 |
Correct |
3 ms |
4188 KB |
Output is correct |
18 |
Correct |
4 ms |
4184 KB |
Output is correct |
19 |
Correct |
3 ms |
4188 KB |
Output is correct |
20 |
Correct |
3 ms |
4184 KB |
Output is correct |
21 |
Correct |
3 ms |
4188 KB |
Output is correct |
22 |
Correct |
3 ms |
4188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4188 KB |
Output is correct |
2 |
Correct |
3 ms |
4188 KB |
Output is correct |
3 |
Correct |
4 ms |
4188 KB |
Output is correct |
4 |
Correct |
4 ms |
4188 KB |
Output is correct |
5 |
Correct |
3 ms |
4212 KB |
Output is correct |
6 |
Correct |
3 ms |
4188 KB |
Output is correct |
7 |
Correct |
3 ms |
4212 KB |
Output is correct |
8 |
Correct |
3 ms |
4188 KB |
Output is correct |
9 |
Correct |
3 ms |
4184 KB |
Output is correct |
10 |
Correct |
3 ms |
4188 KB |
Output is correct |
11 |
Correct |
4 ms |
4188 KB |
Output is correct |
12 |
Correct |
4 ms |
4184 KB |
Output is correct |
13 |
Correct |
3 ms |
4216 KB |
Output is correct |
14 |
Correct |
3 ms |
4188 KB |
Output is correct |
15 |
Correct |
3 ms |
4188 KB |
Output is correct |
16 |
Correct |
3 ms |
4184 KB |
Output is correct |
17 |
Correct |
3 ms |
4188 KB |
Output is correct |
18 |
Correct |
3 ms |
4188 KB |
Output is correct |
19 |
Correct |
3 ms |
4188 KB |
Output is correct |
20 |
Correct |
3 ms |
4188 KB |
Output is correct |
21 |
Correct |
3 ms |
4188 KB |
Output is correct |
22 |
Correct |
3 ms |
4188 KB |
Output is correct |
23 |
Correct |
3 ms |
4184 KB |
Output is correct |
24 |
Correct |
3 ms |
4188 KB |
Output is correct |
25 |
Correct |
3 ms |
4188 KB |
Output is correct |
26 |
Correct |
4 ms |
4184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4188 KB |
Output is correct |
2 |
Correct |
3 ms |
4188 KB |
Output is correct |
3 |
Correct |
4 ms |
4188 KB |
Output is correct |
4 |
Correct |
4 ms |
4188 KB |
Output is correct |
5 |
Correct |
3 ms |
4212 KB |
Output is correct |
6 |
Correct |
3 ms |
4188 KB |
Output is correct |
7 |
Correct |
3 ms |
4212 KB |
Output is correct |
8 |
Correct |
3 ms |
4188 KB |
Output is correct |
9 |
Correct |
3 ms |
4184 KB |
Output is correct |
10 |
Correct |
3 ms |
4188 KB |
Output is correct |
11 |
Correct |
4 ms |
4188 KB |
Output is correct |
12 |
Correct |
4 ms |
4184 KB |
Output is correct |
13 |
Correct |
3 ms |
4216 KB |
Output is correct |
14 |
Correct |
3 ms |
4188 KB |
Output is correct |
15 |
Correct |
3 ms |
4188 KB |
Output is correct |
16 |
Correct |
3 ms |
4184 KB |
Output is correct |
17 |
Correct |
3 ms |
4188 KB |
Output is correct |
18 |
Correct |
3 ms |
4188 KB |
Output is correct |
19 |
Correct |
3 ms |
4188 KB |
Output is correct |
20 |
Correct |
3 ms |
4188 KB |
Output is correct |
21 |
Correct |
3 ms |
4188 KB |
Output is correct |
22 |
Correct |
3 ms |
4188 KB |
Output is correct |
23 |
Correct |
3 ms |
4184 KB |
Output is correct |
24 |
Correct |
3 ms |
4188 KB |
Output is correct |
25 |
Correct |
3 ms |
4188 KB |
Output is correct |
26 |
Correct |
4 ms |
4184 KB |
Output is correct |
27 |
Correct |
21 ms |
4828 KB |
Output is correct |
28 |
Correct |
24 ms |
4832 KB |
Output is correct |
29 |
Correct |
5 ms |
4700 KB |
Output is correct |
30 |
Correct |
6 ms |
4832 KB |
Output is correct |
31 |
Correct |
11 ms |
4832 KB |
Output is correct |
32 |
Correct |
9 ms |
4836 KB |
Output is correct |
33 |
Correct |
11 ms |
4840 KB |
Output is correct |
34 |
Correct |
9 ms |
4728 KB |
Output is correct |
35 |
Correct |
9 ms |
4832 KB |
Output is correct |
36 |
Correct |
10 ms |
4832 KB |
Output is correct |
37 |
Correct |
6 ms |
4916 KB |
Output is correct |
38 |
Correct |
6 ms |
4796 KB |
Output is correct |
39 |
Correct |
10 ms |
4700 KB |
Output is correct |
40 |
Correct |
11 ms |
4828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4188 KB |
Output is correct |
2 |
Correct |
3 ms |
4144 KB |
Output is correct |
3 |
Correct |
3 ms |
4188 KB |
Output is correct |
4 |
Correct |
3 ms |
4188 KB |
Output is correct |
5 |
Correct |
3 ms |
4184 KB |
Output is correct |
6 |
Correct |
3 ms |
4188 KB |
Output is correct |
7 |
Correct |
3 ms |
4216 KB |
Output is correct |
8 |
Correct |
3 ms |
4188 KB |
Output is correct |
9 |
Correct |
3 ms |
4188 KB |
Output is correct |
10 |
Correct |
3 ms |
4212 KB |
Output is correct |
11 |
Correct |
3 ms |
4188 KB |
Output is correct |
12 |
Correct |
3 ms |
4184 KB |
Output is correct |
13 |
Correct |
3 ms |
4188 KB |
Output is correct |
14 |
Correct |
3 ms |
4188 KB |
Output is correct |
15 |
Correct |
3 ms |
4188 KB |
Output is correct |
16 |
Correct |
4 ms |
4188 KB |
Output is correct |
17 |
Correct |
3 ms |
4188 KB |
Output is correct |
18 |
Correct |
4 ms |
4184 KB |
Output is correct |
19 |
Correct |
3 ms |
4188 KB |
Output is correct |
20 |
Correct |
3 ms |
4184 KB |
Output is correct |
21 |
Correct |
3 ms |
4188 KB |
Output is correct |
22 |
Correct |
3 ms |
4188 KB |
Output is correct |
23 |
Correct |
3 ms |
4188 KB |
Output is correct |
24 |
Correct |
3 ms |
4188 KB |
Output is correct |
25 |
Correct |
4 ms |
4188 KB |
Output is correct |
26 |
Correct |
4 ms |
4188 KB |
Output is correct |
27 |
Correct |
3 ms |
4212 KB |
Output is correct |
28 |
Correct |
3 ms |
4188 KB |
Output is correct |
29 |
Correct |
3 ms |
4212 KB |
Output is correct |
30 |
Correct |
3 ms |
4188 KB |
Output is correct |
31 |
Correct |
3 ms |
4184 KB |
Output is correct |
32 |
Correct |
3 ms |
4188 KB |
Output is correct |
33 |
Correct |
4 ms |
4188 KB |
Output is correct |
34 |
Correct |
4 ms |
4184 KB |
Output is correct |
35 |
Correct |
3 ms |
4216 KB |
Output is correct |
36 |
Correct |
3 ms |
4188 KB |
Output is correct |
37 |
Correct |
3 ms |
4188 KB |
Output is correct |
38 |
Correct |
3 ms |
4184 KB |
Output is correct |
39 |
Correct |
3 ms |
4188 KB |
Output is correct |
40 |
Correct |
3 ms |
4188 KB |
Output is correct |
41 |
Correct |
3 ms |
4188 KB |
Output is correct |
42 |
Correct |
3 ms |
4188 KB |
Output is correct |
43 |
Correct |
3 ms |
4188 KB |
Output is correct |
44 |
Correct |
3 ms |
4188 KB |
Output is correct |
45 |
Correct |
3 ms |
4184 KB |
Output is correct |
46 |
Correct |
3 ms |
4188 KB |
Output is correct |
47 |
Correct |
3 ms |
4188 KB |
Output is correct |
48 |
Correct |
4 ms |
4184 KB |
Output is correct |
49 |
Correct |
21 ms |
4828 KB |
Output is correct |
50 |
Correct |
24 ms |
4832 KB |
Output is correct |
51 |
Correct |
5 ms |
4700 KB |
Output is correct |
52 |
Correct |
6 ms |
4832 KB |
Output is correct |
53 |
Correct |
11 ms |
4832 KB |
Output is correct |
54 |
Correct |
9 ms |
4836 KB |
Output is correct |
55 |
Correct |
11 ms |
4840 KB |
Output is correct |
56 |
Correct |
9 ms |
4728 KB |
Output is correct |
57 |
Correct |
9 ms |
4832 KB |
Output is correct |
58 |
Correct |
10 ms |
4832 KB |
Output is correct |
59 |
Correct |
6 ms |
4916 KB |
Output is correct |
60 |
Correct |
6 ms |
4796 KB |
Output is correct |
61 |
Correct |
10 ms |
4700 KB |
Output is correct |
62 |
Correct |
11 ms |
4828 KB |
Output is correct |
63 |
Correct |
36 ms |
6700 KB |
Output is correct |
64 |
Correct |
41 ms |
6612 KB |
Output is correct |
65 |
Correct |
11 ms |
4828 KB |
Output is correct |
66 |
Correct |
10 ms |
4828 KB |
Output is correct |
67 |
Correct |
9 ms |
4840 KB |
Output is correct |
68 |
Correct |
10 ms |
4832 KB |
Output is correct |
69 |
Correct |
33 ms |
5100 KB |
Output is correct |
70 |
Correct |
23 ms |
5104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4188 KB |
Output is correct |
2 |
Correct |
4 ms |
4188 KB |
Output is correct |
3 |
Correct |
3 ms |
4188 KB |
Output is correct |
4 |
Correct |
3 ms |
4188 KB |
Output is correct |
5 |
Correct |
3 ms |
4128 KB |
Output is correct |
6 |
Correct |
3 ms |
4184 KB |
Output is correct |
7 |
Correct |
3 ms |
4188 KB |
Output is correct |
8 |
Correct |
4 ms |
4188 KB |
Output is correct |
9 |
Correct |
3 ms |
4188 KB |
Output is correct |
10 |
Correct |
6 ms |
4868 KB |
Output is correct |
11 |
Correct |
6 ms |
4832 KB |
Output is correct |
12 |
Correct |
7 ms |
4700 KB |
Output is correct |
13 |
Correct |
5 ms |
4832 KB |
Output is correct |
14 |
Correct |
147 ms |
20532 KB |
Output is correct |
15 |
Correct |
125 ms |
20772 KB |
Output is correct |
16 |
Correct |
124 ms |
21284 KB |
Output is correct |
17 |
Correct |
135 ms |
21700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4188 KB |
Output is correct |
2 |
Correct |
3 ms |
4144 KB |
Output is correct |
3 |
Correct |
3 ms |
4188 KB |
Output is correct |
4 |
Correct |
3 ms |
4188 KB |
Output is correct |
5 |
Correct |
3 ms |
4184 KB |
Output is correct |
6 |
Correct |
3 ms |
4188 KB |
Output is correct |
7 |
Correct |
3 ms |
4216 KB |
Output is correct |
8 |
Correct |
3 ms |
4188 KB |
Output is correct |
9 |
Correct |
3 ms |
4188 KB |
Output is correct |
10 |
Correct |
3 ms |
4212 KB |
Output is correct |
11 |
Correct |
3 ms |
4188 KB |
Output is correct |
12 |
Correct |
3 ms |
4184 KB |
Output is correct |
13 |
Correct |
3 ms |
4188 KB |
Output is correct |
14 |
Correct |
3 ms |
4188 KB |
Output is correct |
15 |
Correct |
3 ms |
4188 KB |
Output is correct |
16 |
Correct |
4 ms |
4188 KB |
Output is correct |
17 |
Correct |
3 ms |
4188 KB |
Output is correct |
18 |
Correct |
4 ms |
4184 KB |
Output is correct |
19 |
Correct |
3 ms |
4188 KB |
Output is correct |
20 |
Correct |
3 ms |
4184 KB |
Output is correct |
21 |
Correct |
3 ms |
4188 KB |
Output is correct |
22 |
Correct |
3 ms |
4188 KB |
Output is correct |
23 |
Correct |
3 ms |
4188 KB |
Output is correct |
24 |
Correct |
3 ms |
4188 KB |
Output is correct |
25 |
Correct |
4 ms |
4188 KB |
Output is correct |
26 |
Correct |
4 ms |
4188 KB |
Output is correct |
27 |
Correct |
3 ms |
4212 KB |
Output is correct |
28 |
Correct |
3 ms |
4188 KB |
Output is correct |
29 |
Correct |
3 ms |
4212 KB |
Output is correct |
30 |
Correct |
3 ms |
4188 KB |
Output is correct |
31 |
Correct |
3 ms |
4184 KB |
Output is correct |
32 |
Correct |
3 ms |
4188 KB |
Output is correct |
33 |
Correct |
4 ms |
4188 KB |
Output is correct |
34 |
Correct |
4 ms |
4184 KB |
Output is correct |
35 |
Correct |
3 ms |
4216 KB |
Output is correct |
36 |
Correct |
3 ms |
4188 KB |
Output is correct |
37 |
Correct |
3 ms |
4188 KB |
Output is correct |
38 |
Correct |
3 ms |
4184 KB |
Output is correct |
39 |
Correct |
3 ms |
4188 KB |
Output is correct |
40 |
Correct |
3 ms |
4188 KB |
Output is correct |
41 |
Correct |
3 ms |
4188 KB |
Output is correct |
42 |
Correct |
3 ms |
4188 KB |
Output is correct |
43 |
Correct |
3 ms |
4188 KB |
Output is correct |
44 |
Correct |
3 ms |
4188 KB |
Output is correct |
45 |
Correct |
3 ms |
4184 KB |
Output is correct |
46 |
Correct |
3 ms |
4188 KB |
Output is correct |
47 |
Correct |
3 ms |
4188 KB |
Output is correct |
48 |
Correct |
4 ms |
4184 KB |
Output is correct |
49 |
Correct |
21 ms |
4828 KB |
Output is correct |
50 |
Correct |
24 ms |
4832 KB |
Output is correct |
51 |
Correct |
5 ms |
4700 KB |
Output is correct |
52 |
Correct |
6 ms |
4832 KB |
Output is correct |
53 |
Correct |
11 ms |
4832 KB |
Output is correct |
54 |
Correct |
9 ms |
4836 KB |
Output is correct |
55 |
Correct |
11 ms |
4840 KB |
Output is correct |
56 |
Correct |
9 ms |
4728 KB |
Output is correct |
57 |
Correct |
9 ms |
4832 KB |
Output is correct |
58 |
Correct |
10 ms |
4832 KB |
Output is correct |
59 |
Correct |
6 ms |
4916 KB |
Output is correct |
60 |
Correct |
6 ms |
4796 KB |
Output is correct |
61 |
Correct |
10 ms |
4700 KB |
Output is correct |
62 |
Correct |
11 ms |
4828 KB |
Output is correct |
63 |
Correct |
36 ms |
6700 KB |
Output is correct |
64 |
Correct |
41 ms |
6612 KB |
Output is correct |
65 |
Correct |
11 ms |
4828 KB |
Output is correct |
66 |
Correct |
10 ms |
4828 KB |
Output is correct |
67 |
Correct |
9 ms |
4840 KB |
Output is correct |
68 |
Correct |
10 ms |
4832 KB |
Output is correct |
69 |
Correct |
33 ms |
5100 KB |
Output is correct |
70 |
Correct |
23 ms |
5104 KB |
Output is correct |
71 |
Correct |
3 ms |
4188 KB |
Output is correct |
72 |
Correct |
4 ms |
4188 KB |
Output is correct |
73 |
Correct |
3 ms |
4188 KB |
Output is correct |
74 |
Correct |
3 ms |
4188 KB |
Output is correct |
75 |
Correct |
3 ms |
4128 KB |
Output is correct |
76 |
Correct |
3 ms |
4184 KB |
Output is correct |
77 |
Correct |
3 ms |
4188 KB |
Output is correct |
78 |
Correct |
4 ms |
4188 KB |
Output is correct |
79 |
Correct |
3 ms |
4188 KB |
Output is correct |
80 |
Correct |
6 ms |
4868 KB |
Output is correct |
81 |
Correct |
6 ms |
4832 KB |
Output is correct |
82 |
Correct |
7 ms |
4700 KB |
Output is correct |
83 |
Correct |
5 ms |
4832 KB |
Output is correct |
84 |
Correct |
147 ms |
20532 KB |
Output is correct |
85 |
Correct |
125 ms |
20772 KB |
Output is correct |
86 |
Correct |
124 ms |
21284 KB |
Output is correct |
87 |
Correct |
135 ms |
21700 KB |
Output is correct |
88 |
Correct |
3321 ms |
84920 KB |
Output is correct |
89 |
Correct |
3228 ms |
85040 KB |
Output is correct |
90 |
Correct |
326 ms |
20432 KB |
Output is correct |
91 |
Correct |
362 ms |
20676 KB |
Output is correct |
92 |
Correct |
365 ms |
21700 KB |
Output is correct |
93 |
Correct |
315 ms |
21188 KB |
Output is correct |
94 |
Correct |
350 ms |
21188 KB |
Output is correct |
95 |
Correct |
391 ms |
21188 KB |
Output is correct |
96 |
Correct |
1739 ms |
36240 KB |
Output is correct |
97 |
Correct |
1704 ms |
36120 KB |
Output is correct |