This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define INF (1ll << 60)
using namespace std;
typedef long long LL;
typedef pair<LL, LL> pii;
int N, M;
pii A[202020];
vector<int> comp;
LL ans = -INF;
struct Node{
int cnt;
LL sum;
Node *lc, *rc;
Node();
} *PST[202020], *NIL;
Node :: Node(){
lc = rc = NIL;
cnt = sum = 0;
}
void update(Node *pr, Node *nw, int s, int e, int t){
nw->cnt = pr->cnt+1;
nw->sum = pr->sum+comp[t];
if (s == e) return;
int mid = (s+e)/2;
if (t <= mid){
nw->lc = new Node;
nw->rc = pr->rc;
update(pr->lc, nw->lc, s, mid, t);
}
else{
nw->lc = pr->lc;
nw->rc = new Node;
update(pr->rc, nw->rc, mid+1, e, t);
}
}
LL query(Node *st, Node *ed, int s, int e, int k){
if (k <= 0) return 0;
if (s == e) return k * comp[s];
int mid = (s+e)/2;
if (ed->rc->cnt - st->rc->cnt > k) return query(st->rc, ed->rc, mid+1, e, k);
return ed->rc->sum - st->rc->sum + query(st->lc, ed->lc, s, mid, k - ed->rc->cnt + st->rc->cnt);
}
void DNC(int s, int e, int st, int ed){
if (s > e) return;
int mid = (s+e)/2;
LL Max=-INF, id=0;
for (int i=st; i<=min(ed, mid-M+1); i++){
LL ret = query(PST[i], PST[mid-1], 0, comp.size(), M-2);
ret += A[i].second + A[mid].second - 2*(A[mid].first - A[i].first);
if (Max < ret) Max = ret, id = i;
}
ans = max(ans, Max);
DNC(s, mid-1, st, id);
DNC(mid+1, e, id, ed);
}
int main(){
NIL = new Node;
NIL->lc = NIL->rc = NIL;
scanf("%d %d", &N, &M);
for (int i=1; i<=N; i++){
scanf("%lld %lld", &A[i].second, &A[i].first);
comp.push_back(A[i].second);
}
sort(A+1, A+N+1);
sort(comp.begin(), comp.end());
comp.erase(unique(comp.begin(), comp.end()), comp.end());
PST[0] = new Node;
for (int i=1; i<=N; i++){
PST[i] = new Node;
update(PST[i-1], PST[i], 0, comp.size(), lower_bound(comp.begin(), comp.end(), A[i].second)-comp.begin());
}
DNC(M, N, 1, N);
printf("%lld\n", ans);
return 0;
}
Compilation message (stderr)
cake3.cpp: In function 'int main()':
cake3.cpp:67:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &M);
~~~~~^~~~~~~~~~~~~~~~~
cake3.cpp:69:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld %lld", &A[i].second, &A[i].first);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |