Submission #336998

#TimeUsernameProblemLanguageResultExecution timeMemory
336998alishahali1382Cake 3 (JOI19_cake3)C++14
24 / 100
4065 ms10672 KiB
#include <bits/stdc++.h>
#pragma GCC optimize ("O2,unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;}
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;

const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod=1000000007;
const int MAXN=200010, LOG=20;

ll ans=-INF;
int n, m, k, u, v, x, y, t, a, b, shit;
pii A[MAXN];
int mark[MAXN];

struct Set{
	multiset<int> X, Y;
	ll sum=0;
	inline void add(int i){
		int x=A[i].second;
		if (shit++==10000000) cout<<1/0;
		X.insert(x);
		sum+=x;
		if (X.size()>k){
			auto it=X.begin();
			Y.insert(*it);
			sum-=*it;
			X.erase(it);
		}
	}
	inline void rem(int i){
		int x=A[i].second;
		if (shit++==10000000) cout<<1/0;
		auto it=Y.find(x);
		if (it!=Y.end()) Y.erase(it);
		else{
			it=X.find(x);
			sum-=*it;
			X.erase(it);
			if (Y.size()){
				it=Y.end();
				it--;
				sum+=*it;
				X.insert(*it);
				Y.erase(it);
			}
		}
	}
} S;
inline void Move(int L, int R, int l, int r){
	while (l<L) S.add(--L);
	while (R<r) S.add(R++);
	while (L<l) S.rem(L++);
	while (r<R) S.rem(--R);
}
void divide(int tl, int tr, int optl, int optr){
	if (tr<tl) return ;
	int mid=(tl+tr)>>1, opt=-1;
	ll val=-INF;
	for (int i=tl; i<=mid; i++) S.add(i);
	for (int i=optl; i<=min(mid-k+1, optr); i++){
		ll c=S.sum - 2*(A[mid].first-A[i].first);
		if (c>val){
			val=c;
			opt=i;
		}
		S.rem(i);
	}
	ans=max(ans, val);
	for (int i=optl; i<=min(mid-k+1, optr); i++) S.add(i);
	Move(optl, mid+1, opt, mid+1);
	divide(mid+1, tr, opt, optr);
	Move(opt, mid+1, optl, tl);
	divide(tl, mid-1, optl, opt);
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//	freopen("test.in", "r", stdin);
	//freopen("output.txt", "w", stdout);
	cin>>n>>k;
	for (int i=1; i<=n; i++) cin>>A[i].second>>A[i].first;
	sort(A+1, A+n+1);
	for (int i=1; i<k; i++) S.add(i);
	divide(k, n, 1, n-k+1);
	cout<<ans<<"\n";
	
	return 0;
}
/*
5 3
2 1
4 2
6 4
8 8
10 16



*/

Compilation message (stderr)

cake3.cpp: In member function 'void Set::add(int)':
cake3.cpp:35:32: warning: division by zero [-Wdiv-by-zero]
   35 |   if (shit++==10000000) cout<<1/0;
      |                               ~^~
cake3.cpp:38:15: warning: comparison of integer expressions of different signedness: 'std::multiset<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   38 |   if (X.size()>k){
      |       ~~~~~~~~^~
cake3.cpp: In member function 'void Set::rem(int)':
cake3.cpp:47:32: warning: division by zero [-Wdiv-by-zero]
   47 |   if (shit++==10000000) cout<<1/0;
      |                               ~^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...