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>
using namespace std;
typedef long long ll;
struct node {
ll v, s;
int l, r;
};
const int MAXN=2e5+5;
const int MAIOR=1e9+3;
const ll INF=1e18+9;
pair<ll, ll> v[MAXN]; //c, v
vector<node> seg;
ll dp[MAXN];
int opt[MAXN], raiz[MAXN];
int n, m;
int novo() {
node cur;
cur.l=cur.r=cur.v=cur.s=0;
seg.push_back(cur);
return seg.size()-1;
}
int update(int sind, int ini, int fim, int qind) {
int nsind=novo(); seg[nsind]=seg[sind];
if(ini==fim) {
seg[nsind].v++;
seg[nsind].s+=ini;
return nsind;
}
int m=(ini+fim)/2;
if(qind<=m) {
int ne=update(seg[sind].l, ini, m, qind);
seg[nsind].l=ne;
}
else {
int nd=update(seg[sind].r, m+1, fim, qind);
seg[nsind].r=nd;
}
seg[nsind].s=seg[seg[nsind].l].s+seg[seg[nsind].r].s;
seg[nsind].v=seg[seg[nsind].l].v+seg[seg[nsind].r].v;
return nsind;
}
ll query(int sind, int sind2, int ini, int fim, int val) {
if(val<=0) return 0;
if(seg[sind].v-seg[sind2].v<=val) return seg[sind].s-seg[sind2].s;
if(ini==fim&&seg[sind].v-seg[sind2].v>=val) return (ll)ini*val;
int m=(ini+fim)/2;
int direita=seg[seg[sind].r].v-seg[seg[sind2].r].v;
return query(seg[sind].l, seg[sind2].l, ini, m, val-direita)
+ query(seg[sind].r, seg[sind2].r, m+1, fim, val);
}
void solve(int ini, int fim, int rini, int rfim) {
if(fim<ini) return;
int mid=(ini+fim)/2;
dp[mid]=-INF; opt[mid]=rini;
for(int i=rini; i<=min(rfim, mid-m+1); i++) {
ll val=query(raiz[mid-1], raiz[i], 1, MAIOR, m-2);
val+=v[mid].second+v[i].second;
val-=(v[mid].first-v[i].first)*2LL;
if(val>dp[mid]) dp[mid]=val, opt[mid]=i;
}
solve(ini, mid-1, rini, opt[mid]);
solve(mid+1, fim, opt[mid], rfim);
}
int main() {
scanf("%d %d", &n, &m);
for(int i=1; i<=n; i++) scanf("%lld %lld", &v[i].second, &v[i].first);
sort(v+1, v+1+n);
novo(); raiz[0]=novo();
for(int i=1; i<=n; i++) raiz[i]=update(raiz[i-1], 1, MAIOR, v[i].second);
// while(1) scanf("%d %d", &n, &m), printf(">> %lld\n", query(raiz[m], raiz[n-1], 1, MAIOR, 3));
solve(m, n, 1, n); ll respf=-INF;
for(int i=m; i<=n; i++) respf=max(respf, dp[i]);
printf("%lld\n", respf);
}
Compilation message (stderr)
cake3.cpp: In function 'int main()':
cake3.cpp:71: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:72:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1; i<=n; i++) scanf("%lld %lld", &v[i].second, &v[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... |