QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#95694 | #71. Cake 3 | eyiigjkn | 0 | 0ms | 0kb | C++14 | 1.8kb | 2023-04-11 14:00:36 | 2023-04-11 14:00:40 |
Judging History
answer
# include <bits/stdc++.h>
using namespace std;
using ll=long long;
constexpr ll INF=1e18;
int m,X[200010],N=0;
ll ans=-INF;
struct Node
{
int v,c;
bool operator<(const Node &t)const{return c<t.c;}
}a[200010];
namespace S
{
int rt[200010],lc[4000010],rc[4000010],cnt[4000010],tot=0;
ll sum[4000010];
inline void pushup(int rt)
{
cnt[rt]=cnt[lc[rt]]+cnt[rc[rt]];
sum[rt]=sum[lc[rt]]+sum[rc[rt]];
}
void update(int &rt,int l,int r,int x)
{
lc[++tot]=lc[rt];rc[tot]=rc[rt];cnt[tot]=cnt[rt];sum[tot]=sum[rt];rt=tot;
if(l==r) return cnt[rt]++,sum[rt]+=X[x],void();
int mid=(l+r)/2;
if(x<=mid) update(lc[rt],l,mid,x);
else update(rc[rt],mid+1,r,x);
pushup(rt);
}
ll query(int r1,int r2,int l,int r,int x)
{
if(l==r) return assert(x<=cnt[r1]-cnt[r2]),(ll)min(x,cnt[r1]-cnt[r2])*X[l];
int mid=(l+r)/2;
if(x<=cnt[rc[r1]]-cnt[rc[r2]]) return query(rc[r1],rc[r2],mid+1,r,x);
else return sum[rc[r1]]-sum[rc[r2]]+query(lc[r1],lc[r2],l,mid,x-(cnt[rc[r1]]-cnt[rc[r2]]));
}
inline ll query(int l,int r,int x){assert(x<=cnt[rt[r]]-cnt[rt[l-1]]);return query(rt[r],rt[l-1],1,N,x);}
}
inline void chkmax(ll &x,const ll &y){x=max(x,y);}
void slv(int l,int r,int L,int R)
{
if(l>r) return;
int mid=(l+r)/2,M;
ll maxn=-INF;
for(int i=L;i<=R && i<=mid-m+1;i++)
{
ll s=S::query(i+1,mid-1,m-2)+a[i].v+a[mid].v-2*(a[mid].c-a[i].c);
if(s>maxn) M=i,maxn=s;
}
chkmax(ans,maxn);
slv(l,mid-1,L,M);slv(mid+1,r,M,R);
}
int main()
{
int n;
cin>>n>>m;
for(int i=1;i<=n;i++) scanf("%d%d",&a[i].v,&a[i].c),X[++N]=a[i].v;
sort(a+1,a+n+1);
sort(X+1,X+N+1);
N=unique(X+1,X+N+1)-X-1;
auto get=[](int x){return lower_bound(X+1,X+N+1,x)-X;};
for(int i=1;i<=n;i++) S::update(S::rt[i]=S::rt[i-1],1,N,get(a[i].v));
slv(1,n,1,n);
cout<<ans<<endl;
return 0;
}
詳細信息
Subtask #1:
score: 0
Runtime Error
Test #1:
score: 0
Runtime Error
input:
100 32 671208774 266481733 115497791 342597239 326245300 76223942 528973483 754205900 437996819 995535247 100582194 42402057 771100621 351934207 89058009 81951602 768935397 186435060 842907845 376386254 187943947 59424920 997369107 493642356 455078419 68850493 542835555 938351581 970171972 611243076...
output:
result:
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #1:
0%