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>
using namespace std;
#define int long long
const int MOD = 1e9 + 7;
const int HALF = 5e8 + 4;
struct seg {
int s, e, m, v, lazy;
seg *l, *r;
seg(int _s, int _e): s(_s), e(_e), m(_s+_e>>1), v(e-s+1), lazy(1), l(NULL) {}
void create() {
if(!l) l=new seg(s,m), r=new seg(m+1,e);
}
int val() {return v*lazy%MOD;}
void up(int x, int y, int u) {
if(s==x&&e==y) {lazy=lazy*u%MOD; return;}
create();
if(y<=m) l->up(x,y,u);
else if(x>m) r->up(x,y,u);
else l->up(x,m,u), r->up(m+1,y,u);
v=(l->val())+(r->val()); if(v>=MOD) v-=MOD;
}
void res() {
v=e-s+1;
lazy=1;
if(l) l->res(), r->res();
}
};
main() {
int n; cin >> n;
int a[n], b[n];
for(int i=0;i<n;i++) cin>>a[i];
for(int i=0;i<n;i++) cin>>b[i];
seg sl(1,1<<30), sr(1,1<<30);
int ansl[n], ansr[n];
int ans = 0;
for(int i=0;i<n;i++) {
if(a[i]>b[i]) swap(a[i],b[i]);
sl.up(1,a[i],0);
sl.up(a[i]+1,b[i],HALF);
ans-=sl.val();
}
sl.res();
for(int i=n;i--;) {
sl.up(1,a[i],0);
sl.up(a[i]+1,b[i],HALF);
ans-=sl.val();
}
for(int i=0;i<n;i++) ans-=(HALF*(a[i]+b[i]))%MOD;
ans += (n<<30) + n * sl.val();
ans %= MOD; if(ans<0) ans+=MOD;
for(int i=0;i<n;i++) {ans<<=1; if(ans>=MOD) ans-=MOD;}
cout << ans;
}
Compilation message (stderr)
Main.cpp: In constructor 'seg::seg(long long int, long long int)':
Main.cpp:9:41: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
9 | seg(int _s, int _e): s(_s), e(_e), m(_s+_e>>1), v(e-s+1), lazy(1), l(NULL) {}
| ~~^~~
Main.cpp: At global scope:
Main.cpp:28:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
28 | main() {
| ^~~~
Main.cpp: In function 'int main()':
Main.cpp:34:6: warning: unused variable 'ansl' [-Wunused-variable]
34 | int ansl[n], ansr[n];
| ^~~~
Main.cpp:34:15: warning: unused variable 'ansr' [-Wunused-variable]
34 | int ansl[n], ansr[n];
| ^~~~
# | 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... |