QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#896023#8712. Flooding Wallsitablechair12 1418ms186384kbC++2610.7kb2025-02-12 20:49:562025-02-12 20:50:02

Judging History

This is the latest submission verdict.

  • [2025-02-12 20:50:02]
  • Judged
  • Verdict: 12
  • Time: 1418ms
  • Memory: 186384kb
  • [2025-02-12 20:49:56]
  • Submitted

answer

#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native,sse,sse2,sse3")
#define ll long long
#define ldb long double
#define endl '\n'
#define For(i,l,r) for(int i=l;i<=r;i++)
#define ForD(i,r,l) for(int i=r;i>=l;i--)
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define sz(x) (signed)x.size()
#define unq(x) x.resize(unique(all(x))-x.begin())
#define F "C"
#define fio freopen(F".INP","r",stdin);freopen(F".OUT","w",stdout);

#ifdef NCGM
#include"debug.h"
#else
#define debug(...) "fr";
#endif

using namespace std;

const int N=1e6+3,MOD=1e9+7,INV=500000004;
ll prw[N];
ll binpow(ll x,ll y) {
    if(y == MOD - 2 && x <= 1000000) return prw[x];
    x%=MOD;
    ll res=1;
    while(y>0) {
        if (y%2) res=res*x%MOD;
        x=x*x%MOD;
        y/=2;
    }
    return res;
}


struct SegTree1 {
    struct Node {
        ll x=0,x1=0;
        Node(ll x=0,ll x1=0): x(x),x1(x1) {};
        Node operator + (const Node oth) {
            return Node((x+oth.x)%MOD,(x1+oth.x1)%MOD);
        }
    } tr[N*4];
    ll lazy[N*4];
    void reset(int id,int l,int r) {
        tr[id]=Node(0,0);
        lazy[id]=1;
        if (l==r) return;
        int mid=l+r>>1;
        reset(id*2,l,mid);
        reset(id*2+1,mid+1,r);
    }
    void push(int id) {
        if (lazy[id]==1) return;
        lazy[id*2]=lazy[id*2]*lazy[id]%MOD;
        lazy[id*2+1]=lazy[id*2+1]*lazy[id]%MOD;
        tr[id*2+1].x=tr[id*2+1].x*lazy[id]%MOD;
        tr[id*2+1].x1=tr[id*2+1].x1*lazy[id]%MOD;
        tr[id*2].x=tr[id*2].x*lazy[id]%MOD;
        tr[id*2].x1=tr[id*2].x1*lazy[id]%MOD;
        lazy[id]=1;
    }
    void update(int id,int l,int r,int u,int v,int x) {
        if (x==1) return;
        if (l>v || r<u) return;
        if (l>=u && r<=v) {
            tr[id].x=tr[id].x*x%MOD;
            tr[id].x1=tr[id].x1*x%MOD;
            lazy[id]=lazy[id]*x%MOD;
            return;
        }
        push(id);
        int mid=l+r>>1;
        update(id*2,l,mid,u,v,x);
        update(id*2+1,mid+1,r,u,v,x);
        tr[id].x=(tr[id*2].x+tr[id*2+1].x);
        tr[id].x1=(tr[id*2].x1+tr[id*2+1].x1)%MOD;
    }
    void update1(int id,int l,int r,int u,ll x,ll y) {
        if (l>u || r<u) return;
        if (l==r) return void(tr[id]=Node(x,y));
        int mid=l+r>>1;
        update1(id*2,l,mid,u,x,y);
        update1(id*2+1,mid+1,r,u,x,y);
        tr[id].x=(tr[id*2].x+tr[id*2+1].x);
        tr[id].x1=(tr[id*2].x1+tr[id*2+1].x1);
    }
    Node query(int id,int l,int r,int u,int v) {
        if (l>v || r<u) return Node(0,0);
        if (l>=u && r<=v) return tr[id];
        push(id);
        int mid=l+r>>1;
        return query(id*2,l,mid,u,v)+query(id*2+1,mid+1,r,u,v);
    }
} st1;

int n,a[N],b[N],c[N],d[N],e[N][2],lft[N][2],rgt[N][2];
int f[N],mi[N],ma[N];
ll pw[N*2],pf[N],pf1[N];
vector<pair<int,int>> in[N*2];
vector<ll> big;
ll ans=0;
struct SegTree {
    ll t[2 * N];

    void reset() {
        for (int i = n - 1; i > 0; --i) t[i] = 0;
    }

    void update(int p, ll value) {
        p--;
        for (t[p += n] = value; p > 1; p >>= 1) t[p>>1] = t[p] * t[p^1]%MOD;
    }
    int query(int l, int r) {
        l--;
        ll res = 1;
        for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
            if (l&1) res = res*t[l++]%MOD;
            if (r&1) res = res*t[--r]%MOD;
        }
        return res;
    }

} st;
struct BIT {
    ll pf[N];
    ll query(int p) {
        int idx=p;
        ll ans=0;
        while(idx>0) {
            ans=ans+pf[idx];
            idx-=(idx&(-idx));
        }
        return ans%MOD;
    }
    void update(int u,ll v) {
        int idx=u;
        while(idx<=n) {
            pf[idx]=pf[idx]+v;
            idx+=(idx&(-idx));
        }
    }
} bit1,bit;

int find_set(int u) {
    return (f[u]<0?u:f[u]=find_set(f[u]));
}

void unite(int u,int v) {
    int a=find_set(u),b=find_set(v);
    if (a==b) return;
    if (f[a]>f[b]) swap(a,b);
    f[a]+=f[b];
    f[b]=a;
    mi[a]=min(mi[a],mi[b]);
    ma[a]=max(ma[a],ma[b]);
}

void solve(bool lmao=0) {
    For(i,1,n) bit1.pf[i]=bit.pf[i]=0;
    For(i,1,n) in[lower_bound(all(big),a[i])-big.begin()+1].pb({i,0});
    For(i,1,n) in[lower_bound(all(big),b[i])-big.begin()+1].pb({i,1});
    fill(c,c+1+n,0);
    st1.reset(1,1,n);
    if (!lmao) {
        st.reset();
        For(i,1,sz(big)+1) {
            for(auto el: in[i-1]) {
                c[el.ff]++;
                st.update(el.ff,c[el.ff]);
            }
            for(auto el: in[i-1]) {
                ll x=st.query(1,el.ff-1),y=st.query(el.ff+1,n);
                if (el.ff>1 && el.ff<n) {
                    ans=(ans+1LL*(e[el.ff][el.ss])*x%MOD*pw[n-el.ff]%MOD)%MOD;
                    ans=(ans+1LL*(e[el.ff][el.ss])*y%MOD*pw[el.ff-1]%MOD)%MOD;
                    ans=(ans-1LL*(e[el.ff][el.ss])*x%MOD*y%MOD)%MOD;
                }
                else ans=(ans+1LL*(e[el.ff][el.ss])*pw[n-1]%MOD)%MOD;

                if (el.ff>1) lft[el.ff][el.ss]=x;
                else lft[el.ff][el.ss]=1;
                if (el.ff<n) rgt[el.ff][el.ss]=y;
                else rgt[el.ff][el.ss]=1;
            }
        }
    } else {
        For(i,1,n)
            For(k,0,1) swap(lft[i][k],rgt[n-i+1][k]);
    }
    For(i,1,n) f[i]=-1,mi[i]=ma[i]=i,c[i]=0,d[i]=2;
    st.reset();
    For(i,1,sz(big)) {
        for(auto el: in[i]) {
            d[el.ff]--;
            if (d[el.ff]==1) st1.update(1,1,n,el.ff,el.ff,INV);
            else st1.update(1,1,n,el.ff,el.ff,0);
        }
        for(auto el: in[i-1]) {
            c[el.ff]++;
            st.update(el.ff,c[el.ff]);
            if (c[el.ff]==1) {
                ll tmp=pw[el.ff-1]*binpow(c[el.ff],MOD-2)%MOD*d[el.ff]%MOD;
                if (c[el.ff-1]>0) {
                    ll mmb=st.query(mi[find_set(el.ff-1)],el.ff-1)%MOD;
                    st1.update1(1,1,n,el.ff,tmp*(el.ff+1)%MOD*binpow(mmb,MOD-2)%MOD,tmp*binpow(mmb,MOD-2)%MOD);
                    unite(el.ff,el.ff-1);
                } else st1.update1(1,1,n,el.ff,tmp*(el.ff+1)%MOD,tmp);

                if (c[el.ff+1]>0) {
                    ll mmb=st.query(mi[find_set(el.ff)],el.ff)%MOD;
                    st1.update(1,1,n,el.ff+1,ma[find_set(el.ff+1)],binpow(mmb,MOD-2));
                    unite(el.ff,el.ff+1);
                }
            } else st1.update(1,1,n,el.ff,ma[find_set(el.ff)],INV);

        }
        for(auto el: in[i]) {
            if (c[el.ff-1]>0) {
                if (mi[find_set(el.ff-1)]<el.ff-1) {
                    auto mmb=st1.query(1,1,n,mi[find_set(el.ff-1)],el.ff-1);
                    ll x=mmb.x%MOD,x1=mmb.x1%MOD;
                    ll tmp=(1LL*x1*el.ff%MOD-x)%MOD;
                    ans=(ans+big[i-1]*rgt[el.ff][el.ss]%MOD*tmp%MOD*(st.query(mi[find_set(el.ff-1)],el.ff-1)%MOD)%MOD);
                }
                if (mi[find_set(el.ff-1)]>1) {
                    int j=mi[find_set(el.ff-1)]-1;
                    ll tmp=st.query(mi[find_set(el.ff-1)],el.ff-1)%MOD;
                    ll cnst=big[i-1]*tmp%MOD*(el.ff-j-1)%MOD;
                    For(k,0,1) {
                        if (e[j][k]>big[i-1]) ans=(ans+big[i-1]*tmp%MOD*pw[j-1]%MOD*rgt[el.ff][el.ss]%MOD)%MOD;
                        else if (e[j][k]==big[i-1] && !lmao) {
                            ans=(ans+cnst*(pw[j-1]-lft[j][k])%MOD*rgt[el.ff][el.ss]%MOD)%MOD;
                            ans=(ans+cnst*lft[j][k]%MOD*pw[n-el.ff]%MOD)%MOD;
                        }
                    }
                }
            }

        }
        if (!lmao) {
            for(auto el: in[i]) {
                ll tmp=(pw[el.ff-1]-lft[el.ff][el.ss])%MOD*big[i-1]%MOD;
                if (c[el.ff]>0) tmp=tmp*binpow(st.query(mi[find_set(el.ff)],el.ff)%MOD,MOD-2)%MOD;
                bit.update(el.ff,tmp);
                bit1.update(el.ff,tmp*(el.ff+1)%MOD);
                pf[el.ff]=(pf[el.ff]+tmp)%MOD;
                pf1[el.ff]=(pf1[el.ff]+tmp*(el.ff+1))%MOD;
            }
            for(auto el: in[i])
                if (c[el.ff-1] && el.ff-1>mi[find_set(el.ff-1)]) {
                    ll x=bit.query(el.ff-2)-bit.query(mi[find_set(el.ff-1)]-1);
                    ll x1=bit1.query(el.ff-2)-bit1.query(mi[find_set(el.ff-1)]-1);
                    ans=(ans+(el.ff*x%MOD-x1)*rgt[el.ff][el.ss]%MOD*(st.query(mi[find_set(el.ff-1)],el.ff-1)%MOD)%MOD)%MOD;
                }
            for(auto el: in[i]) {
                bit.update(el.ff,-pf[el.ff]);
                bit1.update(el.ff,-pf1[el.ff]);
                pf[el.ff]=pf1[el.ff]=0;
            }
            for(auto el: in[i]) {
                ll tmp=lft[el.ff][el.ss]%MOD*big[i-1]%MOD;
                if (c[el.ff]>0) tmp=tmp*binpow(st.query(mi[find_set(el.ff)],el.ff)%MOD,MOD-2)%MOD;
                bit.update(el.ff,tmp);
                bit1.update(el.ff,tmp*(el.ff+1)%MOD);
                pf[el.ff]=(pf[el.ff]+tmp)%MOD;
                pf1[el.ff]=(pf1[el.ff]+tmp*(el.ff+1))%MOD;
            }
            for(auto el: in[i])
                if (c[el.ff-1] && el.ff-1>mi[find_set(el.ff-1)]) {
                    ll x=bit.query(el.ff-2)-bit.query(mi[find_set(el.ff-1)]-1);
                    ll x1=bit1.query(el.ff-2)-bit1.query(mi[find_set(el.ff-1)]-1);
                    ans=(ans+(el.ff*x%MOD-x1)*pw[n-el.ff]%MOD*(st.query(mi[find_set(el.ff-1)],el.ff-1)%MOD)%MOD)%MOD;
                }
            for(auto el: in[i]) {
                bit.update(el.ff,-pf[el.ff]);
                bit1.update(el.ff,-pf1[el.ff]);
                pf[el.ff]=pf1[el.ff]=0;
            }
        }

    }

    For(i,1,sz(big)) in[i].clear();
}
ll powv(ll x, ll y)
{
    ll ans = 1;
    while(y > 0)
    {
        if(y % 2 == 0)
        {
            y /= 2;
            x *= x;
            x %= MOD;
        }else
        {
            y--;
            ans *= x;
            ans %= MOD;
        }
    }
    return ans;
}
int main() {
    // fio
    cin.tie(0)->sync_with_stdio(0);
    cin >> n;
    for(ll i = 1;i <= 1000000;i++)
    {
        prw[i] = powv(i,MOD - 2);
    }
    pw[0]=1;
    For(i,1,n) pw[i]=pw[i-1]*2%MOD;
    For(i,1,n) {
        a[i]=2;
        cin >> a[i];
        e[i][0]=a[i];
        big.pb(a[i]);
    }
    For(i,1,n) {
        b[i]=1;
        cin >> b[i];
        e[i][1]=b[i];
        big.pb(b[i]);
    }
    sort(all(big));
    unq(big);
    solve();
    reverse(b+1,b+1+n);
    reverse(a+1,a+1+n);
    For(i,1,n) {
        e[i][0]=a[i];
        e[i][1]=b[i];
    }
    solve(1);
    For(i,1,n) ans=(ans-1LL*a[i]*pw[n-1]%MOD)%MOD;
    For(i,1,n) ans=(ans-1LL*b[i]*pw[n-1]%MOD)%MOD;
    ans=(ans+MOD)%MOD;
    cout << ans;

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 8
Accepted
time: 106ms
memory: 105016kb

input:

4
1 1 1 1
2 2 2 2

output:

6

result:

ok single line: '6'

Test #2:

score: 8
Accepted
time: 103ms
memory: 102172kb

input:

10
1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1

output:

21116

result:

ok single line: '21116'

Test #3:

score: 8
Accepted
time: 107ms
memory: 104420kb

input:

1
1
2

output:

0

result:

ok single line: '0'

Test #4:

score: 8
Accepted
time: 106ms
memory: 103412kb

input:

2
1 1
2 2

output:

0

result:

ok single line: '0'

Test #5:

score: 8
Accepted
time: 105ms
memory: 103500kb

input:

3
1 1 1
2 2 2

output:

1

result:

ok single line: '1'

Test #6:

score: 8
Accepted
time: 107ms
memory: 104436kb

input:

3
1 1 1
3 2 3

output:

3

result:

ok single line: '3'

Test #7:

score: 8
Accepted
time: 109ms
memory: 104796kb

input:

3
2 1 1
3 2 3

output:

4

result:

ok single line: '4'

Test #8:

score: 8
Accepted
time: 112ms
memory: 103492kb

input:

3
1 1 2
3 2 3

output:

4

result:

ok single line: '4'

Test #9:

score: 8
Accepted
time: 110ms
memory: 101784kb

input:

4
1 1 2 2
2 2 1 1

output:

6

result:

ok single line: '6'

Test #10:

score: 8
Accepted
time: 112ms
memory: 104868kb

input:

3
1 4 4
3 1 1

output:

2

result:

ok single line: '2'

Test #11:

score: 0
Wrong Answer
time: 105ms
memory: 101576kb

input:

20
801072306 297281669 94099424 745640358 822582129 579751930 776707816 425952653 529794053 256263083 615445445 401366545 990263003 967996508 788867983 916880116 837997768 346996508 623409388 122077161
141734988 448434751 822901346 825591177 388082644 468025968 260356829 1164654 537396602 730502935 ...

output:

530366323

result:

wrong answer 1st lines differ - expected: '840988190', found: '530366323'

Subtask #2:

score: 0
Wrong Answer

Test #23:

score: 0
Wrong Answer
time: 105ms
memory: 104808kb

input:

100
948 425 211 688 416 81 356 602 712 954 117 522 797 75 281 491 662 669 78 156 939 526 929 937 916 619 166 777 48 898 449 278 298 714 668 755 679 38 389 602 195 135 672 833 655 541 473 27 596 274 351 353 598 993 837 246 950 99 179 751 481 843 550 195 964 279 806 82 330 599 124 756 649 838 513 625 ...

output:

570226672

result:

wrong answer 1st lines differ - expected: '164439470', found: '570226672'

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Skipped

Dependency #1:

0%

Subtask #5:

score: 12
Accepted

Test #57:

score: 12
Accepted
time: 1406ms
memory: 183172kb

input:

500000
1 1 1 1 2 2 2 1 1 1 1 1 2 2 1 1 2 2 2 2 1 2 1 1 2 1 1 1 1 2 1 2 2 1 1 2 2 2 1 1 2 2 2 2 1 2 2 2 2 1 2 2 2 1 2 2 2 1 2 2 2 2 2 2 2 2 1 1 2 1 2 1 2 2 1 2 2 2 2 2 2 2 1 1 1 2 2 2 1 1 2 1 2 2 1 2 2 1 2 2 2 1 2 2 2 2 2 2 2 2 1 1 1 2 1 2 1 1 2 1 2 1 2 2 1 1 2 1 1 1 1 1 2 2 2 2 2 2 1 2 2 1 1 2 1 2 1...

output:

869044223

result:

ok single line: '869044223'

Test #58:

score: 12
Accepted
time: 1418ms
memory: 183400kb

input:

499993
1 1 2 1 1 2 1 1 2 1 1 1 2 2 2 2 1 1 1 2 1 1 1 2 2 2 1 1 2 2 1 2 1 2 1 2 1 2 2 2 2 1 1 2 1 2 1 2 2 1 1 2 2 2 2 2 1 1 2 1 1 2 1 1 2 2 2 1 2 2 1 2 1 1 2 1 1 2 1 1 1 1 2 1 1 1 2 2 1 1 2 1 1 1 2 2 2 2 1 1 2 1 2 1 2 2 1 2 1 1 1 1 2 1 1 2 1 2 2 1 2 2 2 2 2 1 2 2 1 1 1 2 1 1 1 1 2 1 1 1 2 1 1 2 1 1 1...

output:

480826834

result:

ok single line: '480826834'

Test #59:

score: 12
Accepted
time: 1400ms
memory: 186384kb

input:

500000
2 2 1 1 2 2 2 1 1 2 2 2 2 2 1 1 1 1 1 2 1 2 1 2 1 2 2 1 1 1 2 1 1 1 1 1 1 2 2 2 2 2 2 2 2 1 2 1 2 1 2 1 1 2 1 1 2 2 2 1 2 2 1 2 2 1 2 2 1 1 1 2 1 1 1 2 2 2 1 1 1 1 1 1 1 2 1 1 1 1 1 2 2 1 2 1 2 2 2 1 2 1 1 2 1 2 1 1 2 2 2 1 2 1 2 2 2 2 2 2 1 1 2 2 2 2 1 1 1 1 1 1 2 2 1 1 1 2 1 2 2 2 1 1 1 2 2...

output:

869044223

result:

ok single line: '869044223'

Test #60:

score: 12
Accepted
time: 1406ms
memory: 185888kb

input:

499999
2 1 2 2 1 2 1 1 2 1 1 1 2 2 2 1 1 2 2 2 1 1 1 2 2 2 2 2 1 2 2 1 1 2 2 2 2 1 1 2 1 2 2 1 2 2 2 2 2 2 1 2 2 2 2 2 2 1 2 2 2 2 2 1 2 1 2 1 2 1 2 1 1 1 1 1 1 2 1 1 1 1 2 1 2 2 1 1 2 2 1 2 1 1 2 1 2 1 1 2 1 1 1 2 2 1 1 2 2 2 2 2 1 2 1 2 2 1 1 1 2 2 1 1 2 1 1 2 1 1 1 2 1 2 1 1 2 1 2 1 2 1 2 2 2 1 1...

output:

192864306

result:

ok single line: '192864306'

Subtask #6:

score: 0
Skipped

Dependency #1:

0%