#include <stdio.h>
#include <string.h>
#define N 100000
#define N3 (N * 3 + 2)
#define N_ (1 << 19) /* N_ = pow2(ceil(log2(N3))) */
#define INF 0x3f3f3f3f3f3f3f3fLL
long long min(long long a, long long b) { return a < b ? a : b; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int xx[N3];
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 n_;
void update(long long *st, int i, long long x) {
for (i += n_; i > 0; i >>= 1)
st[i] = min(st[i], x);
}
long long query(long long *st, int l, int r) {
long long x = INF;
for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1) {
if ((l & 1) == 1)
x = min(x, st[l++]);
if ((r & 1) == 0)
x = min(x, st[r--]);
}
return x;
}
int main() {
static int cc[N], ii[N3];
static long long st1[N_ * 2], st2[N_ * 2];
int n, n3, m, i, x;
long long ans;
scanf("%d%d", &n, &m), n3 = n * 3 + 2;
for (i = 0; i < n; i++)
scanf("%d%d%d%d", &xx[i * 3 + 0], &xx[i * 3 + 1], &xx[i * 3 + 2], &cc[i]);
xx[n * 3] = 1, xx[n * 3 + 1] = m;
for (i = 0; i < n3; i++)
ii[i] = i;
sort(ii, 0, n3);
for (i = 0, x = 0; i < n3; i++)
xx[ii[i]] = i + 1 == n3 || xx[ii[i + 1]] != xx[ii[i]] ? x++ : x;
n_ = 1;
while (n_ < n3)
n_ <<= 1;
memset(st1, 0x3f, n_ * 2 * sizeof *st1), memset(st2, 0x3f, n_ * 2 * sizeof *st2);
update(st1, xx[n * 3], 0), update(st2, xx[n * 3 + 1], 0);
ans = INF;
for (i = 0; i < n; i++) {
long long x1 = query(st1, xx[i * 3 + 0], xx[i * 3 + 1]);
long long x2 = query(st2, xx[i * 3 + 0], xx[i * 3 + 1]);
if (x1 != INF && x2 != INF)
ans = min(ans, x1 + x2 + cc[i]);
if (x1 != INF)
update(st1, xx[i * 3 + 2], x1 + cc[i]);
if (x2 != INF)
update(st2, xx[i * 3 + 2], x2 + cc[i]);
}
if (ans == INF)
ans = -1;
printf("%lld\n", ans);
return 0;
}
Compilation message
pinball.c: In function 'main':
pinball.c:63:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | scanf("%d%d", &n, &m), n3 = n * 3 + 2;
| ^~~~~~~~~~~~~~~~~~~~~
pinball.c:65:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | scanf("%d%d%d%d", &xx[i * 3 + 0], &xx[i * 3 + 1], &xx[i * 3 + 2], &cc[i]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
304 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
304 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
476 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
440 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
304 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
476 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
440 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
12 ms |
1888 KB |
Output is correct |
26 |
Correct |
31 ms |
5952 KB |
Output is correct |
27 |
Correct |
91 ms |
12936 KB |
Output is correct |
28 |
Correct |
98 ms |
23216 KB |
Output is correct |
29 |
Correct |
62 ms |
11736 KB |
Output is correct |
30 |
Correct |
107 ms |
23280 KB |
Output is correct |
31 |
Correct |
131 ms |
23244 KB |
Output is correct |
32 |
Correct |
129 ms |
23312 KB |
Output is correct |
33 |
Correct |
18 ms |
3512 KB |
Output is correct |
34 |
Correct |
59 ms |
11668 KB |
Output is correct |
35 |
Correct |
90 ms |
23268 KB |
Output is correct |
36 |
Correct |
136 ms |
23188 KB |
Output is correct |
37 |
Correct |
116 ms |
23372 KB |
Output is correct |
38 |
Correct |
110 ms |
23364 KB |
Output is correct |