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 <stdio.h>
#define N 200000
#define LN 18
#define N_ (N * (LN + 1) + 1)
#define INF 0x3f3f3f3f3f3f3f3fLL
long long max(long long a, long long b) { return a > b ? a : b; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int vv[N], cc[N], ii[N], pp[N], n, m;
int *xx;
void sort(int *ii, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
while (j < k)
if (xx[ii[j]] == xx[i_])
j++;
else if (xx[ii[j]] < xx[i_]) {
tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
i++, j++;
} else {
k--;
tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
}
sort(ii, l, i);
l = k;
}
}
int ll[N_], rr[N_], kk[N_]; long long ss[N_];
int update(int t, int l, int r, int i, int x) {
static int _ = 1;
int t_, m;
t_ = _++;
ll[t_] = ll[t], rr[t_] = rr[t], kk[t_] = kk[t] + 1, ss[t_] = ss[t] + x;
if (r - l > 1) {
m = (l + r) / 2;
if (i < m)
ll[t_] = update(ll[t_], l, m, i, x);
else
rr[t_] = update(rr[t_], m, r, i, x);
}
return t_;
}
long long query(int t1, int t2, int k) {
if (k >= kk[t2] - kk[t1])
return ss[t2] - ss[t1];
return k < kk[rr[t2]] - kk[rr[t1]] ? query(rr[t1], rr[t2], k) : ss[rr[t2]] - ss[rr[t1]] + query(ll[t1], ll[t2], k - (kk[rr[t2]] - kk[rr[t1]]));
}
int tt[N + 1]; long long ans;
long long solve(int l1, int r1, int l2, int r2) {
int i, i_, j;
long long x_;
if (l1 > r1)
return -INF;
j = (l1 + r1) / 2;
i_ = -1, x_ = -INF;
for (i = l2; i <= r2; i++)
if (j - i + 1 >= m) {
long long x = query(tt[i], tt[j + 1], m) - (cc[ii[j]] - cc[ii[i]]) * 2;
if (x_ < x)
x_ = x, i_ = i;
}
return max(x_, max(solve(l1, j - 1, l2, i_), solve(j + 1, r1, i_, r2)));
}
int main() {
int i;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++)
scanf("%d%d", &vv[i], &cc[i]);
for (i = 0; i < n; i++)
ii[i] = i;
xx = vv, sort(ii, 0, n);
for (i = 0; i < n; i++)
pp[ii[i]] = i;
xx = cc, sort(ii, 0, n);
for (i = 0; i < n; i++)
tt[i + 1] = update(tt[i], 0, n, pp[ii[i]], vv[ii[i]]);
printf("%lld\n", solve(m - 1, n - 1, 0, n - 1));
return 0;
}
Compilation message (stderr)
cake3.c: In function 'main':
cake3.c:86:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
86 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
cake3.c:88:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
88 | scanf("%d%d", &vv[i], &cc[i]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |