Submission #499305

#TimeUsernameProblemLanguageResultExecution timeMemory
499305couplefireCake 3 (JOI19_cake3)C++17
100 / 100
1348 ms151640 KiB
#include <iostream>
#include <algorithm>
using namespace std;
#define sz 30000000
pair<long long,int> tree[sz];
int n,m,lc[sz],rc[sz],root[200005],cn;
long long ans=-1e18;
pair<int,int> arr[200005];
void update(int node,int node2,int st,int en,int idx){
	tree[node2]=tree[node];
	tree[node2].first+=idx;
	tree[node2].second++;
	if (st!=en){
		int mid=(st+en)/2;
		if (st<=idx && idx<=mid){
			lc[node2]=++cn;
			rc[node2]=rc[node];
			update(lc[node],lc[node2],st,mid,idx);
		}
		else{
			rc[node2]=++cn;
			lc[node2]=lc[node];
			update(rc[node],rc[node2],mid+1,en,idx);
		}
	}
}
long long get(int node,int node2,int st,int en,int k){
	if (st==en)
	return 1LL*k*st;
	int mid=(st+en)/2;
	if (tree[rc[node2]].second-tree[rc[node]].second>=k)
	return get(rc[node],rc[node2],mid+1,en,k);
	k-=tree[rc[node2]].second-tree[rc[node]].second;
	return tree[rc[node2]].first-tree[rc[node]].first+get(lc[node],lc[node2],st,mid,k);
}
long long query(int l,int r){
	return get(root[l],root[r-1],1,1e9,m-2)+arr[l].second+arr[r].second-2*arr[r].first+2*arr[l].first;
}
void solve(int l,int r,int optl,int optr){
	if (r<l)
	return;
	int opt=optl,mid=(l+r)/2;
	for (int i=optl+1;i<=min(mid-m+1,optr);i++)
	{
		if (query(i,mid)>query(opt,mid))
		opt=i;
	}
	ans=max(ans,query(opt,mid));
	solve(l,mid-1,optl,opt);
	solve(mid+1,r,opt,optr);
}
int main(){
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++)
	scanf("%d%d",&arr[i].second,&arr[i].first);
	sort(arr+1,arr+n+1);
	for (int i=1;i<=n;i++)
	{
		root[i]=++cn;
		update(root[i-1],root[i],1,1e9,arr[i].second);
	}
	solve(m,n,1,n-m+1);
	printf("%lld",ans);
}

Compilation message (stderr)

cake3.cpp: In function 'int main()':
cake3.cpp:53:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   53 |  scanf("%d%d",&n,&m);
      |  ~~~~~^~~~~~~~~~~~~~
cake3.cpp:55:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 |  scanf("%d%d",&arr[i].second,&arr[i].first);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...