QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#632129 | #9440. Train Seats | pengpeng_fudan | WA | 3ms | 13328kb | C++23 | 4.5kb | 2024-10-12 12:26:03 | 2024-10-12 12:26:06 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
mt19937 rnd(time(NULL));
const int N=200010;
struct Info{
int sum;int num;
ll ans;
int s1,n1,a1;int bm;
Info(ll s=0,ll n=0,ll a=0,int b=0){
sum=s,num=n;
ans=a;s1=s,n1=n;a1=a;
bm=b;
};
void print(){
cerr<<sum<<' '<<num<<' '<<ans<<' '<<s1<<' '<<n1<<' '<<bm<<'\n';
}
bool operator<(const Info a)const {
if(1ll*s1*a.n1!=1ll*a.s1*n1) return 1ll*s1*a.n1<1ll*a.s1*n1;
else return bm<a.bm;
}
bool operator<=(const Info a)const {
if(1ll*s1*a.n1!=1ll*a.s1*n1) return 1ll*s1*a.n1<1ll*a.s1*n1;
else return bm<=a.bm;
}
};
Info add(Info l,Info mid,Info r){
Info res=mid;
res.sum=l.sum+mid.s1+r.sum;res.num=l.num+mid.n1+r.num;
res.ans=l.ans+mid.a1+r.ans+1ll*mid.s1*l.num+1ll*(l.num+mid.n1)*r.sum;
return res;
}
struct treap{
#define ls(x) tr[x].l
#define rs(x) tr[x].r
struct node{
Info res;
int l,r,rnd;
}tr[N];
int tot=0,rt=0;
int intt(Info a){
++tot;int q=tot;
ls(q)=rs(q)=0,tr[q].rnd=rnd();
tr[q].res=a;
return q;
}
void pushup(int p){
tr[p].res=add(tr[ls(p)].res,tr[p].res,tr[rs(p)].res);
}
int merge(int p,int q){
if(!p||!q) return p|q;
if(tr[p].rnd<=tr[q].rnd){
tr[p].r=merge(tr[p].r,q);
pushup(p);return p;
}
else{
tr[q].l=merge(p,tr[q].l);
pushup(q);return q;
}
}
void split(int p,Info res,int& x,int& y,int t){
if(!p) {x=y=0;return ;}
if((t==0?(tr[p].res<res):(tr[p].res<=res))){
x=p;split(rs(p),res,rs(x),y,t);
}
else{
y=p;split(ls(p),res,x,ls(y),t);
}
pushup(p);
}
array<int,3> split(Info x){
int lo,ro,mid;
split(rt,x,lo,ro,0);
split(ro,x,mid,ro,1);
return {lo,mid,ro};
}
void insert(Info a){
auto [lo,mid,ro]=split(a);
mid=intt(a);
rt=merge(merge(lo,mid),ro);
}
void del(Info a){
auto [lo,mid,ro]=split(a);
assert(mid);
rt=merge(lo,ro);
}
ll get_ans(){
return tr[rt].res.ans;
}
};
treap tp;
void solve(){
int n,m;
cin>>n>>m;
vector<int> a(n);
for(int i=0;i<n;i++) cin>>a[i];
sort(a.begin(),a.end());
vector<int> b(n+2);
for(int i=0;i<n;i++){
if(i==0) b[i+1]=a[i];
else b[i+1]=a[i]-a[i-1];
}
b[n+1]=m+1-a[n-1];
vector<ll> preb(n+2),segb(n+3);
vector<int> pre(n+2);
for(int i=1;i<=n+1;i++){
preb[i]=preb[i-1]+1ll*b[i]*i;
}
for(int i=n+1;i>=1;i--){
segb[i]=segb[i+1]+1ll*b[i]*(n+2-i);
}
for(int i=1;i<=n+1;i++){
pre[i]=pre[i-1]+b[i];
}
auto getp=[&](int lo,int ro)->ll {
return 2*(preb[ro]-preb[lo-1]-1ll*lo*(pre[ro]-pre[lo-1]))-1ll*(ro-lo)*(pre[ro]-pre[lo-1]);
};
auto gets=[&](int lo,int ro)->ll {
return 2*(segb[lo]-segb[ro+1]-1ll*(n+2-ro)*(pre[ro]-pre[lo-1]))-1ll*(ro-lo)*(pre[ro]-pre[lo-1]);
};
vector<Info> ad(n+2);
vector<vector<Info>> dl(n+2);
int bm=0;
vector<Info> stk;
for(int i=n+1;i>=1;i--){
int lo=i,s1=b[i],n1=1;
while(!stk.empty()){
Info r=stk.back();
if(1ll*r.s1*n1>=1ll*s1*r.n1) {s1+=r.s1,n1+=r.n1;dl[i].push_back(r);stk.pop_back();}
else break;
}
int ro=lo+n1-1;
Info w=Info(s1,n1,(gets(lo,ro)+1ll*s1*(n1+1))/2,++bm);
stk.push_back(w);ad[i]=w;
}
for(auto i:stk) tp.insert(i);
stk.clear();
ll ans=0;
for(int i=1;i<=n+1;i++){
tp.del(ad[i]);for(auto j:dl[i]) tp.insert(j);
if(i!=1){
int ro=i-1,s1=b[i-1],n1=1;
while(!stk.empty()){
Info r=stk.back();
if(1ll*r.s1*n1>=1ll*s1*r.n1) {s1+=r.s1,n1+=r.n1;tp.del(r);stk.pop_back();}
else break;
}
int lo=ro-n1+1;
Info w=Info(s1,n1,(getp(lo,ro)+1ll*s1*(n1+1))/2,++bm);tp.insert(w);
stk.push_back(w);
}
ans=max(ans,tp.get_ans()+1ll*n*b[i]);
// cerr<<tp.get_ans()+n*b[i]<<'\n';
}
cout<<ans<<'\n';
}
int main(){
ios::sync_with_stdio(0),cin.tie(0);
int _ =1;
// cin>>_;
while(_--) solve();
return 0;
}
/*
3 10
3 7 10
*/
/*
5 20
3 10 11 14 17
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 13084kb
input:
3 10 3 7 10
output:
28
result:
ok "28"
Test #2:
score: 0
Accepted
time: 0ms
memory: 13328kb
input:
5 20 3 10 11 14 17
output:
73
result:
ok "73"
Test #3:
score: 0
Accepted
time: 2ms
memory: 13024kb
input:
10 1000000000 136909656 243332691 643531997 505919307 43553384 657638276 57213246 179732866 357373203 182482400
output:
7649951260
result:
ok "7649951260"
Test #4:
score: 0
Accepted
time: 3ms
memory: 13272kb
input:
1 172024959 118390965
output:
172024960
result:
ok "172024960"
Test #5:
score: 0
Accepted
time: 2ms
memory: 13312kb
input:
2 920065252 24963998 580538879
output:
1815166508
result:
ok "1815166508"
Test #6:
score: 0
Accepted
time: 0ms
memory: 13324kb
input:
3 172078788 90217996 89200357 170380183
output:
432676968
result:
ok "432676968"
Test #7:
score: 0
Accepted
time: 2ms
memory: 13060kb
input:
4 440425711 125318960 91140311 293637977 102491554
output:
1442752023
result:
ok "1442752023"
Test #8:
score: 0
Accepted
time: 2ms
memory: 13092kb
input:
5 322827483 22471802 47973794 3707067 27640905 273307033
output:
1512343852
result:
ok "1512343852"
Test #9:
score: -100
Wrong Answer
time: 0ms
memory: 13072kb
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:
19990186709
result:
wrong answer 1st words differ - expected: '25015466409', found: '19990186709'