이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
}
컴파일 시 표준 에러 (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... |