1 solutions

  • 0
    @ 2026-7-29 10:48:39
    /*
     *                                                     __----~~~~~~~~~~~------___
     *                                    .  .   ~~//====......          __--~ ~~
     *                    -.            \_|//     |||\\  ~~~~~~::::... /~
     *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
     *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
     *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
     *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
     *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
     *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
     *           '         ~-|      /|    |-~\~~       __--~~
     *                       |-~~-_/ |    |   ~\_   _-~            /\
     *                            /  \     \__   \/~                \__
     *                        _--~ _/ | .-~~____--~-/                  ~~==.
     *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
     *                                  -_     ~\      ~~---l__i__i__i--~~_/
     *                                  _-~-__   ~)  \--______________--~~
     *                                //.-~~~-~_--~- |-------~~~~~~~~
     *                                       //.-~~~--\
     *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     *                               神兽保佑            永无BUG
     */
    
    /*
     * @Description: I want to be the weakest vegetable in the world!
     * @Author: I.Z.
    */
    #include<bits/stdc++.h>
    using namespace std;
    #define MOD         1000000007
    #define speMOD      2933256077ll
    #define int         long long
    #define pii         pair<int,int>
    #define all(v)      v.begin(),v.end()
    #define pb          push_back
    #define REP(i,b,e)  for(int i=(b);i<(int)(e);++i)
    #define over(x)     {cout<<(x)<<endl;return;}
    #define lowbit(x)   ((x)&(-(x)))
    #define cntbit(x)   __builtin_popcount(x)
    #define deal(v)     sort(all(v));v.erase(unique(v.begin(),v.end()),v.end())
    #define lbound(v,x) lower_bound(all(v),x)-v.begin()
    mt19937 sd(random_device{}());
    int qpow(int a,int b,int m=MOD,int res=1){
    	a%=m;
    	while(b>0)res=(b&1)?(res*a%m):(res),a=a*a%m,b>>=1;
    	return res;
    }
    int exgcd(int x,int y,int &a,int &b){
    	if(y==0){
    		a=1;b=0;
    		return x;
    	}
    	int d=exgcd(y,x%y,a,b);
    	int z=b;
    	b=a-b*(x/y);
    	a=z;
    	return d;
    }
    int _x_,_y_;
    inline int inverse(int x,int y=MOD){
    	exgcd(x,y,_x_,_y_);
    	return (_x_+y)%y;
    }
    int fac[2000005],inv[2000005];
    void init(int n){
    	fac[0]=inv[0]=1;
    	REP(i,1,n+1)fac[i]=fac[i-1]*i%MOD;
    	inv[n]=qpow(fac[n],MOD-2);
    	for(int i=n-1;i>=1;--i)inv[i]=inv[i+1]*(i+1)%MOD;
    }
    int binom(int x,int y){
    	return x<y||y<0? 0:fac[x]*inv[y]%MOD*inv[x-y]%MOD;
    }
    vector<int>v;
    int n;
    int a[500005],b[500005];
    struct ds{
    	int seg[4000005];
    	int tag[4000005];
    	void build(int l,int r,int p){
    		tag[p]=1;seg[p]=0;
    		if(l==r)return seg[p]=v[l],void();
    		int m=(l+r)>>1;
    		build(l,m,p*2+1);build(m+1,r,p*2+2);
    		seg[p]=(seg[p*2+1]+seg[p*2+2])%MOD;
    	}
    	void update(int l,int r,int s,int t,int p,int val){
    		if(l<=s&&t<=r){
    			(tag[p]*=val)%=MOD;(seg[p]*=val)%=MOD;
    			return;
    		}
    		int m=(s+t)>>1;
    		if(m>=l)update(l,r,s,m,p*2+1,val);
    		if(m<r)update(l,r,m+1,t,p*2+2,val);
    		seg[p]=(seg[p*2+1]+seg[p*2+2])*tag[p]%MOD;
    	}
    }seg;
    int p2[500005];
    int cnt[500005];
    #define C 1'000'000'001
    int solve(){
    	vector<pii>t;
    	REP(i,0,n)t.pb({a[i],i*2}),t.pb({b[i],i*2+1});
    	sort(all(t));
    	v.clear();
    	t.pb({C,-1});
    	REP(i,1,t.size())v.pb(t[i].first-t[i-1].first);
    	seg.build(0,2*n-1,0);
    	int ans=0;
    	REP(i,0,n){
    		int x=lower_bound(all(t),pii{a[i],i*2})-t.begin();
    		int y=lower_bound(all(t),pii{b[i],i*2+1})-t.begin();
    //		cout<<x<<' '<<y<<endl;
    		if(x>y)swap(x,y);
    		if(x)seg.update(0,x-1,0,2*n-1,0,0);
    		seg.update(y,2*n-1,0,2*n-1,0,2);
    		(ans+=seg.seg[0]*p2[n-i-1])%=MOD;
    	}
    //	cout<<ans<<endl;
    	ans=(MOD+C*p2[n]%MOD*n%MOD-ans)%MOD;
    	return ans;
    }
    void Main() {
    	cin>>n;
    	REP(i,0,n)cin>>a[i];
    	REP(i,0,n)cin>>b[i];
    	p2[0]=1;
    	REP(i,1,n+1)p2[i]=p2[i-1]*2%MOD;
    	int ans=0;
    	REP(i,0,n)ans+=a[i]+b[i];
    	ans=ans%MOD*p2[n-1]%MOD;
    //	cout<<ans<<endl;
    	vector<pii>t;
    	REP(i,0,n)t.pb({a[i],i}),t.pb({b[i],i});
    	sort(all(t));
    	int c0=n,c2=0;
    	for(auto [j,i]:t){
    		if(!cnt[i])--c0;
    		if(!c0)(ans+=p2[c2]*j%MOD*n)%=MOD;
    		++cnt[i];
    		if(cnt[i]==2)++c2;
    	}
    //	cout<<ans<<endl;
    	ans=MOD-ans;
    	ans+=solve();
    //	cout<<'!'<<ans<<endl;
    	reverse(a,a+n);reverse(b,b+n);
    	ans+=solve();
    	over(ans%MOD)
    }
    void TC() {
        int tc=1;
    	while(tc--){
    		Main();
    		cout.flush();
    	}
    }
    signed main() {
    	freopen("nietzsche.in","r",stdin);
    	freopen("nietzsche.out","w",stdout);
    	return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0;
    }
    /*
    1. CLEAR the arrays (ESPECIALLY multitests)
    2. DELETE useless output
     */
    
    • 1

    Information

    ID
    3065
    Time
    8000ms
    Memory
    512MiB
    Difficulty
    10
    Tags
    (None)
    # Submissions
    2
    Accepted
    1
    Uploaded By