/* https://oj.uz/submission/216252 (Benq) */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 30000
#define M 30000
#define M_ (M * 2 + 1)
int min(int a, int b) { return a < b ? a : b; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int *eh[N], eo[N];
void append(int i, int h) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
eh[i] = (int *) realloc(eh[i], o * 2 * sizeof *eh[i]);
eh[i][o] = h;
}
int ii[M], jj[M], rr[M];
int compare(int h1, int h2) {
return jj[h1] != jj[h2] ? jj[h1] - jj[h2] : (rr[h1] != rr[h2] ? rr[h1] - rr[h2] : ii[h1] - ii[h2]);
}
void sort(int *hh, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, h = hh[l + rand_() % (r - l)], tmp;
while (j < k) {
int c = compare(hh[j], h);
if (c == 0)
j++;
else if (c < 0) {
tmp = hh[i], hh[i] = hh[j], hh[j] = tmp;
i++, j++;
} else {
k--;
tmp = hh[j], hh[j] = hh[k], hh[k] = tmp;
}
}
sort(hh, l, i);
l = k;
}
}
int main() {
static int hh[M], kl[M], kr[M], qu[M_], dd[M_];
int n, m, h, i, j, s, t, o, head, tail;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++)
eh[i] = (int *) malloc(2 * sizeof *eh[i]);
for (h = 0; h < m; h++) {
scanf("%d%d", &i, &j);
ii[h] = i, jj[h] = j, rr[h] = i % j;
append(i, h);
}
for (h = 0; h < m; h++)
hh[h] = h;
sort(hh, 0, m);
for (h = 0; h < m; h++) {
int h_, p, q, r;
h_ = hh[h], p = h == 0 ? -1 : hh[h - 1], q = h + 1 == m ? -1 : hh[h + 1];
i = ii[h_], j = jj[h_], r = rr[h_];
kl[h_] = h == 0 || jj[p] != j || rr[p] != r ? i / j : (i - ii[p]) / j;
kr[h_] = h + 1 == m || jj[q] != j || rr[q] != r ? (n - 1 - i) / j : (ii[q] - i) / j;
}
s = ii[0], t = ii[1];
if (s == t) {
printf("0\n");
return 0;
}
head = tail = 0;
i = s;
for (o = eo[i]; o--; ) {
h = eh[i][o];
if (kl[h] > 0)
qu[tail] = h * N + kl[h] - 1, tail = tail + 1 == M_ ? 0 : tail + 1;
if (kr[h] > 0)
qu[tail] = h * N + kl[h] + 1, tail = tail + 1 == M_ ? 0 : tail + 1;
}
eo[i] = 0;
while (head != tail) {
int hk, k, d;
hk = qu[head], d = dd[head] + 1, head = head + 1 == M_ ? 0 : head + 1;
h = hk / N, k = hk % N - kl[h], i = ii[h] + k * jj[h];
if (i == t) {
printf("%d\n", d);
return 0;
}
if (k < 0 && k > -kl[h])
dd[tail] = d, qu[tail] = h * N + kl[h] + k - 1, tail = tail + 1 == M_ ? 0 : tail + 1;
else if (k > 0 && k < kr[h])
dd[tail] = d, qu[tail] = h * N + kl[h] + k + 1, tail = tail + 1 == M_ ? 0 : tail + 1;
if (eo[i]) {
for (o = eo[i]; o--; ) {
h = eh[i][o];
if (kl[h] > 0)
dd[tail] = d, qu[tail] = h * N + kl[h] - 1, tail = tail + 1 == M_ ? 0 : tail + 1;
if (kr[h] > 0)
dd[tail] = d, qu[tail] = h * N + kl[h] + 1, tail = tail + 1 == M_ ? 0 : tail + 1;
}
eo[i] = 0;
}
}
printf("-1\n");
return 0;
}
Compilation message
skyscraper.c: In function 'append':
skyscraper.c:23:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
23 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
skyscraper.c: In function 'main':
skyscraper.c:60:2: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
60 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
skyscraper.c:64:3: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
64 | scanf("%d%d", &i, &j);
| ^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
0 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
2 ms |
364 KB |
Output is correct |
14 |
Correct |
2 ms |
492 KB |
Output is correct |
15 |
Correct |
2 ms |
392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
0 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
492 KB |
Output is correct |
24 |
Correct |
2 ms |
492 KB |
Output is correct |
25 |
Correct |
1 ms |
492 KB |
Output is correct |
26 |
Correct |
1 ms |
492 KB |
Output is correct |
27 |
Correct |
1 ms |
492 KB |
Output is correct |
28 |
Correct |
2 ms |
620 KB |
Output is correct |
29 |
Correct |
1 ms |
876 KB |
Output is correct |
30 |
Correct |
1 ms |
492 KB |
Output is correct |
31 |
Correct |
1 ms |
748 KB |
Output is correct |
32 |
Correct |
1 ms |
620 KB |
Output is correct |
33 |
Correct |
2 ms |
1004 KB |
Output is correct |
34 |
Correct |
2 ms |
1004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
0 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
492 KB |
Output is correct |
24 |
Correct |
1 ms |
492 KB |
Output is correct |
25 |
Correct |
2 ms |
492 KB |
Output is correct |
26 |
Correct |
2 ms |
492 KB |
Output is correct |
27 |
Correct |
1 ms |
492 KB |
Output is correct |
28 |
Correct |
2 ms |
620 KB |
Output is correct |
29 |
Correct |
1 ms |
876 KB |
Output is correct |
30 |
Correct |
1 ms |
492 KB |
Output is correct |
31 |
Correct |
1 ms |
748 KB |
Output is correct |
32 |
Correct |
1 ms |
620 KB |
Output is correct |
33 |
Correct |
2 ms |
1004 KB |
Output is correct |
34 |
Correct |
3 ms |
1004 KB |
Output is correct |
35 |
Correct |
12 ms |
1388 KB |
Output is correct |
36 |
Correct |
2 ms |
492 KB |
Output is correct |
37 |
Correct |
9 ms |
1132 KB |
Output is correct |
38 |
Correct |
14 ms |
1388 KB |
Output is correct |
39 |
Correct |
14 ms |
1260 KB |
Output is correct |
40 |
Correct |
14 ms |
1388 KB |
Output is correct |
41 |
Correct |
14 ms |
1516 KB |
Output is correct |
42 |
Correct |
7 ms |
1272 KB |
Output is correct |
43 |
Correct |
8 ms |
1260 KB |
Output is correct |
44 |
Correct |
7 ms |
1260 KB |
Output is correct |
45 |
Correct |
17 ms |
1772 KB |
Output is correct |
46 |
Correct |
15 ms |
1772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
2 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
2 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
492 KB |
Output is correct |
24 |
Correct |
1 ms |
492 KB |
Output is correct |
25 |
Correct |
1 ms |
492 KB |
Output is correct |
26 |
Correct |
1 ms |
492 KB |
Output is correct |
27 |
Correct |
1 ms |
492 KB |
Output is correct |
28 |
Correct |
2 ms |
620 KB |
Output is correct |
29 |
Correct |
1 ms |
876 KB |
Output is correct |
30 |
Correct |
1 ms |
492 KB |
Output is correct |
31 |
Correct |
1 ms |
748 KB |
Output is correct |
32 |
Correct |
1 ms |
620 KB |
Output is correct |
33 |
Correct |
3 ms |
1004 KB |
Output is correct |
34 |
Correct |
2 ms |
1004 KB |
Output is correct |
35 |
Correct |
14 ms |
1420 KB |
Output is correct |
36 |
Correct |
2 ms |
492 KB |
Output is correct |
37 |
Correct |
9 ms |
1152 KB |
Output is correct |
38 |
Correct |
18 ms |
1388 KB |
Output is correct |
39 |
Correct |
14 ms |
1260 KB |
Output is correct |
40 |
Correct |
14 ms |
1388 KB |
Output is correct |
41 |
Correct |
17 ms |
1644 KB |
Output is correct |
42 |
Correct |
7 ms |
1260 KB |
Output is correct |
43 |
Correct |
7 ms |
1260 KB |
Output is correct |
44 |
Correct |
7 ms |
1260 KB |
Output is correct |
45 |
Correct |
17 ms |
1772 KB |
Output is correct |
46 |
Correct |
17 ms |
1772 KB |
Output is correct |
47 |
Correct |
16 ms |
1644 KB |
Output is correct |
48 |
Correct |
12 ms |
1900 KB |
Output is correct |
49 |
Correct |
12 ms |
1900 KB |
Output is correct |
50 |
Correct |
8 ms |
1900 KB |
Output is correct |
51 |
Correct |
16 ms |
2540 KB |
Output is correct |
52 |
Correct |
17 ms |
2668 KB |
Output is correct |
53 |
Correct |
16 ms |
2412 KB |
Output is correct |
54 |
Correct |
2 ms |
1644 KB |
Output is correct |
55 |
Correct |
2 ms |
1644 KB |
Output is correct |
56 |
Correct |
21 ms |
2796 KB |
Output is correct |
57 |
Correct |
4 ms |
1644 KB |
Output is correct |
58 |
Correct |
8 ms |
2796 KB |
Output is correct |
59 |
Correct |
9 ms |
2796 KB |
Output is correct |
60 |
Correct |
9 ms |
2924 KB |
Output is correct |
61 |
Correct |
9 ms |
2924 KB |
Output is correct |
62 |
Correct |
20 ms |
2796 KB |
Output is correct |
63 |
Correct |
37 ms |
2924 KB |
Output is correct |
64 |
Correct |
42 ms |
2796 KB |
Output is correct |
65 |
Correct |
55 ms |
3052 KB |
Output is correct |
66 |
Correct |
105 ms |
2924 KB |
Output is correct |
67 |
Correct |
50 ms |
2828 KB |
Output is correct |