QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#698808#9440. Train Seatsucup-team902WA 19ms123168kbC++173.6kb2024-11-01 22:07:532024-11-01 22:07:54

Judging History

This is the latest submission verdict.

  • [2024-11-01 22:07:54]
  • Judged
  • Verdict: WA
  • Time: 19ms
  • Memory: 123168kb
  • [2024-11-01 22:07:53]
  • Submitted

answer

#include<bits/stdc++.h>
using namespace std;
using ll=__int128;
const int N=2e5;
int n,m;
ll a[N+5];
ll ans;
struct val{
    ll s,ss; int c;
    val(ll s=0,ll ss=0,int c=0):s(s),ss(ss),c(c){}
    bool operator < (const val t) const{
        return s*t.c==t.s*c?c<t.c:s*t.c<t.s*c;
    }
}st1[N+5],st2[N+5];
int s1,s2;
val xx[N*2+5];
int xs;
vector<val> op[N+5];
val merge(val x,val y){
    // printf("x %lld %lld %d\n",x.s,x.ss,x.c);
    // printf("y %lld %lld %d\n",y.s,y.ss,y.c);
    x.ss+=y.ss+x.s*y.c;
    x.s+=y.s; x.c+=y.c;
    // printf("res %lld %lld %d\n",x.s,x.ss,x.c);
    // printf("%lld %lld %d\n",x.s,x.ss,x.c);
    return x;
}
void prep(){
    for(int i=1;i<=n;i++){
        val tmp=val(a[i],a[i],1);
        while(s1&&tmp<st1[s1]){
            tmp=merge(tmp,st1[s1]);
            s1--;
        }
        st1[++s1]=tmp;
        xx[++xs]=tmp;
    }
    for(int i=n;i;i--){
        val tmp=val(a[i],a[i],1);
        while(s2&&tmp<st2[s2]){
            op[i].push_back(st2[s2]);
            tmp=merge(tmp,st2[s2]);
            s2--;
        }
        st2[++s2]=tmp;
        xx[++xs]=tmp;
    }
    sort(xx+1,xx+1+xs);
}
struct Segtr{
    val tr[N*8+5];
    #define lc id<<1
    #define rc id<<1|1
    void push_up(int id){
        tr[id]=merge(tr[lc],tr[rc]);
    }
    void add(int l,int r,int p,int x,int id){
        if(l==r){
            if(x>0){
                tr[id].s+=xx[l].s;
                tr[id].c+=xx[l].c;
            }
            else{
                tr[id].s-=xx[l].s;
                tr[id].c-=xx[l].c;
            }
            return;
        }
        int mid=l+r>>1;
        if(p<=mid) add(l,mid,p,x,lc);
        else add(mid+1,r,p,x,rc);
        push_up(id);
    }
    val query(int l,int r,int st,int en,int id){
        if(st>en) return val();
        if(st<=l&&en>=r) return tr[id];
        int mid=l+r>>1; val ans;
        if(st<=mid) ans=merge(ans,query(l,mid,st,en,lc));
        if(en>mid) ans=merge(ans,query(mid+1,r,st,en,rc));
        return ans;
    }
}T;
int find(val x){
    int l=1,r=xs;
    while(l<=r){
        int mid=l+r>>1;
        if(x<xx[mid]) r=mid-1;
        else l=mid+1;
    }
    return r;
}
ll ins(val it){
    int pos=find(it);
    val vl=T.query(1,xs,1,pos-1,1),vr=T.query(1,xs,pos,xs,1);
    T.add(1,xs,pos,1,1);
    return it.s*(n-1-vl.c)-it.ss-vr.s*it.c;
}
ll del(val it){
    int pos=find(it);
    T.add(1,xs,pos,-1,1);
    val vl=T.query(1,xs,1,pos-1,1),vr=T.query(1,xs,pos,xs,1);
    return it.s*(n-1-vl.c)-it.ss-vr.s*it.c;
}
int main(){
    scanf("%d %d",&n,&m); m++;
    for(int i=1;i<=n;i++)
        scanf("%lld",&a[i]);
    sort(a+1,a+1+n);
    a[++n]=m;
    for(int i=n;i;i--) a[i]-=a[i-1];
    prep();
    // fprintf(stderr,"prep\n");
    ll res=0;
    for(int i=1;i<=s2;i++){
        // fprintf(stderr,"%lld %lld %d\n",st2[i].s,st2[i].ss,st2[i].c);
        res+=ins(st2[i]);
    }
    s1=0;
    // fprintf(stderr,"prep\n");
    for(int i=1;i<=n;i++){
        res-=del(st2[s2]); s2--;
        for(auto it:op[i]){
            res+=ins(it);
            st2[++s2]=it;
        }
        ans=max(ans,1ll*m*(n-1)-res);
        // printf("%lld\n",res);
        val tmp=val(a[i],a[i],1);
        while(s1&&tmp<st1[s1]){
            res-=del(st1[s1]);
            tmp=merge(tmp,st1[s1]);
            s1--;
        }
        res+=ins(tmp);
        st1[++s1]=tmp;
    }
    // printf("lstc %d\n",T.tr[1].c);
    // for(int i=1;i<=s1;i++){
    //     printf("%lld %lld %d\n",st1[i].s,st1[i].ss,st1[i].c);
    // }
    printf("%lld\n",(long long)ans);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 19ms
memory: 121320kb

input:

3 10
3 7 10

output:

28

result:

ok "28"

Test #2:

score: 0
Accepted
time: 7ms
memory: 122268kb

input:

5 20
3 10 11 14 17

output:

73

result:

ok "73"

Test #3:

score: 0
Accepted
time: 4ms
memory: 121504kb

input:

10 1000000000
136909656 243332691 643531997 505919307 43553384 657638276 57213246 179732866 357373203 182482400

output:

7649951260

result:

ok "7649951260"

Test #4:

score: 0
Accepted
time: 0ms
memory: 123168kb

input:

1 172024959
118390965

output:

172024960

result:

ok "172024960"

Test #5:

score: 0
Accepted
time: 7ms
memory: 122492kb

input:

2 920065252
24963998 580538879

output:

1815166508

result:

ok "1815166508"

Test #6:

score: 0
Accepted
time: 15ms
memory: 122508kb

input:

3 172078788
90217996 89200357 170380183

output:

432676968

result:

ok "432676968"

Test #7:

score: 0
Accepted
time: 11ms
memory: 122636kb

input:

4 440425711
125318960 91140311 293637977 102491554

output:

1442752023

result:

ok "1442752023"

Test #8:

score: 0
Accepted
time: 7ms
memory: 122416kb

input:

5 322827483
22471802 47973794 3707067 27640905 273307033

output:

1512343852

result:

ok "1512343852"

Test #9:

score: -100
Wrong Answer
time: 11ms
memory: 121816kb

input:

72 630313504
112329946 338670434 45608233 444381955 513206139 543955969 420916952 485920423 598657953 568525637 92887514 375155749 230002387 302266710 539300386 433464422 380969627 445990156 239073197 278937451 50602251 494375406 139348725 11780176 601670777 68418714 591190755 96719555 612628609 778...

output:

29126316958

result:

wrong answer 1st words differ - expected: '25015466409', found: '29126316958'