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 100002
#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 rr[N];
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 (rr[ii[j]] == rr[i_])
j++;
else if (rr[ii[j]] < rr[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;
}
}
void update(long long *ft, int i, int n, long long x) {
while (i < n) {
ft[i] = max(ft[i], x);
i |= i + 1;
}
}
long long query(long long *ft, int i) {
long long x = -INF;
while (i >= 0) {
x = max(x, ft[i]);
i &= i + 1, i--;
}
return x;
}
int main() {
static int xx[N], bb[N], ii[N];
static long long dp[N], ft1[N], ft2[N];
int n, xl, xr, d, a, i, r;
scanf("%d%d%d%d%d", &xl, &xr, &d, &a, &n);
xx[0] = xl;
for (i = 1; i <= n; i++)
scanf("%d%d", &xx[i], &bb[i]);
xx[n + 1] = xr;
n += 2;
for (i = 0; i < n; i++) {
rr[i] = xx[i] % d;
ii[i] = i;
}
sort(ii, 0, n);
for (i = 0, r = 0; i < n; i++)
rr[ii[i]] = i + 1 == n || rr[ii[i + 1]] != rr[ii[i]] ? r++ : r;
for (i = 0; i < n; i++)
ft1[i] = ft2[i] = -INF;
for (i = 0; i < n; i++) {
dp[i] = i == 0 ? 0 : max(query(ft1, rr[i] - 1) - a, query(ft2, n - 1 - rr[i])) + bb[i];
update(ft1, rr[i], n, dp[i]);
update(ft2, n - 1 - rr[i], n, dp[i]);
}
printf("%lld\n", dp[n - 1] - (long long) (xx[n - 1] / d - xx[0] / d) * a);
return 0;
}
Compilation message (stderr)
koala.c: In function 'main':
koala.c:57:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | scanf("%d%d%d%d%d", &xl, &xr, &d, &a, &n);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
koala.c:60:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | scanf("%d%d", &xx[i], &bb[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... |