Submission #135829

#TimeUsernameProblemLanguageResultExecution timeMemory
135829dndhkCake 3 (JOI19_cake3)C++14
0 / 100
2 ms376 KiB
#include <bits/stdc++.h>

#define pb push_back
#define all(v) ((v).begin(), (v).end())
#define sortv(v) sort(all(v))
#define sz(v) ((int)(v).size())
#define uniqv(v) (v).erase(unique(all(v)), (v).end())
#define umax(a, b) (a)=max((a), (b))
#define umin(a, b) (a)=min((a), (b))
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define rep(i,n) FOR(i,1,n)
#define rep0(i,n) FOR(i,0,(int)(n)-1)
#define FI first
#define SE second
#define INF 2000000000
#define INFLL 1000000000000000000LL


using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int N, M;
vector<pll> vt;
ll ans = -INFLL;
priority_queue<ll> pq1, pq2;
int sz = 0;

struct PST{
	struct NODE{
		int l, r;
		int cnt;
		ll sum;
	};
	int SZ;
	vector<NODE> seg;
	vector<int> Tree;
	void Init(int x){
		SZ = x;
		Tree.pb(0);
		seg.pb({-1, -1, 0, 0});
		init(Tree[0], 0, SZ-1);
	}
	void init(int idx, int s, int e){
		if(s==e)	return;
		seg[idx].l = seg.size(); seg.pb({-1, -1, 0, 0});
		seg[idx].r = seg.size(); seg.pb({-1, -1, 0, 0});
		init(seg[idx].l, s, (s+e)/2); init(seg[idx].r, (s+e)/2+1, e);
	}
	void Update(int x, ll y){
		Tree.pb(update(Tree.back(), 0, SZ-1, x, y));
	}
	int update(int pidx, int s, int e, int x, ll y){
		if(x<s || x>e)	return pidx;
		int idx = seg.size(); seg.pb({-1, -1, 0, 0});
		if(s==e){
			seg[idx].sum = y; seg[idx].cnt = 1;
			return idx;
		}
		int k = update(seg[pidx].l, s, (s+e)/2, x, y); seg[idx].l = k;
		k = update(seg[pidx].r, (s+e)/2+1, e, x, y); seg[idx].r = k;
		seg[idx].sum = seg[seg[idx].l].sum + seg[seg[idx].r].sum;
		seg[idx].cnt = seg[seg[idx].l].cnt + seg[seg[idx].r].cnt;
		return idx;
	}
	int Cnt(int x, int y, int z){
		return calc_cnt(Tree[x], 0, SZ-1, y, z);
	}
	int calc_cnt(int idx, int s, int e, int x, int y){
		if(x<=s && e<=y)	return seg[idx].cnt;
		if(x>e || y<s)	return 0;
		return calc_cnt(seg[idx].l, s, (s+e)/2, x, y) + calc_cnt(seg[idx].r, (s+e)/2+1, e, x, y);
	}
	ll Sum(int x, int y, int z){
		return calc_sum(Tree[x], 0, SZ-1, y, z);
	}
	ll calc_sum(int idx, int s, int e, int x, int y){
		if(x<=s && e<=y)	return seg[idx].sum;
		if(x>e || y<s)	return 0;
		return calc_sum(seg[idx].l, s, (s+e)/2, x, y) + calc_sum(seg[idx].r, (s+e)/2+1, e, x, y);
	}
};

PST Pst;

ll csum(int x, int y){
	int s = 1, e = N, m;
	while(s<e){
		m = (s+e)/2;
		int k = Pst.Cnt(m, x, y);
		if(k==M){
			//cout<<m<<" "<<x<<" "<<y<<" "<<Pst.Sum(m, x, y)<<endl;
			return Pst.Sum(m, x, y);
		}
		if(k<M){
			s = m+1;
		}else{
			e = m-1;
		}
	}
	return Pst.Cnt(s, x, y);
}

void calc(int x, int y, int s, int e){
	if(x>y)	return;
	int m = (x+y)/2;
	ll mx = -INFLL, idx = 0;
	for(int i=s; i<=e; i++){
		if(m-i+1<M)	continue;
		ll c = csum(i, m);
		if(mx < c - 2 * (vt[m].first - vt[i].first)){
			mx = c - 2 * (vt[m].first - vt[i].first);
			idx = i;
		}
	}
	ans = max(ans, mx);
	calc(x, m-1, s, idx); calc(m+1, y, idx, e);
}

vector<pll> upd;

int main(){
	scanf("%d%d", &N, &M);
	for(int i=0; i<N; i++){
		ll a, b;
		scanf("%lld%lld", &a, &b);
		vt.pb({b, a});
	}
	sort(vt.begin(), vt.end());
	for(int i=0; i<N; i++){
		upd.pb({vt[i].second, i});
	}
	sort(upd.begin(), upd.end());
	Pst.Init(N);
	while(!upd.empty()){
		Pst.Update(upd.back().second, upd.back().first);
		upd.pop_back();
	}
	calc(0, N-1, 0, N-1);
	cout<<ans;
	return 0;
}

Compilation message (stderr)

cake3.cpp: In function 'int main()':
cake3.cpp:124:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &N, &M);
  ~~~~~^~~~~~~~~~~~~~~~
cake3.cpp:127:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld%lld", &a, &b);
   ~~~~~^~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...