#include <stdio.h>
#include <stdlib.h>
#define N 300000
#define Q 300000
#define L_ 19 /* L_ = ceil(log2(Q)) */
#define N_ (1 << L_)
#define INF 0x3f3f3f3f
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int xx[N], gg[N], n;
int yy[Q], yy_[Q], tt_[Q], tt1[Q], q_;
int compareh_y(int h1, int h2) {
return yy[h1] - yy[h2];
}
int compareh_t(int h1, int h2) {
return tt_[h1] - tt_[h2];
}
int compare_i(int i, int j) {
return gg[i] != gg[j] ? gg[i] - gg[j] : xx[i] - xx[j];
}
int (*compare)(int, int);
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) {
int c = compare(ii[j], i_);
if (c == 0)
j++;
else if (c < 0) {
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 idx(int *xx, int x) {
int lower = -1, upper = q_;
while (upper - lower > 1) {
int h = (lower + upper) / 2;
if (xx[h] <= x)
lower = h;
else
upper = h;
}
return lower;
}
int stmn[N_ * 2], stmx[N_ * 2], *ei[N_ * 2], eo[N_ * 2], n_;
char upd[L_ + 2][N_ * 2]; int qu[L_ + 2][N_ * 2], cnt[L_ + 2];
int oldmn[L_ + 2][N_ * 2], oldmx[L_ + 2][N_ * 2];
void push(int i, int x, int t) {
if (!upd[t][i]) {
upd[t][i] = 1, qu[t][cnt[t]++] = i;
oldmn[t][i] = stmn[i], oldmx[t][i] = stmx[i];
}
stmn[i] = min(stmn[i], x);
stmx[i] = max(stmx[i], x);
}
void pop(int t) {
while (cnt[t]--) {
int i = qu[t][cnt[t]];
upd[t][i] = 0;
stmn[i] = oldmn[t][i], stmx[i] = oldmx[t][i];
}
cnt[t] = 0;
}
void build() {
int i;
n_ = 1;
while (n_ < q_)
n_ <<= 1;
for (i = 1; i < n_ * 2; i++)
stmn[i] = INF, stmx[i] = -INF;
for (i = 1; i < n_ * 2; i++)
ei[i] = (int *) malloc(2 * sizeof *ei[i]);
}
void update(int l, int r, int x, int t) {
for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1) {
if ((l & 1) == 1)
push(l++, x, t);
if ((r & 1) == 0)
push(r--, x, t);
}
}
int query(int i) {
int y, l, r;
l = r = y = yy_[i];
for (i += n_; i > 0; i >>= 1)
l = min(l, stmn[i]), r = max(r, stmx[i]);
return l == -INF || r == INF ? -1 : max(y - l, r - y);
}
int mid(int a, int b) {
return a + b > 0 ? (a + b) / 2 : -(-a + b + 1) / 2;
}
int pp[N], qq[N];
void remove_(int i, int t) {
int p = pp[i], q = qq[i];
if (p != -1)
qq[p] = q;
if (q != -1)
pp[q] = p;
if (p == -1 && q == -1)
update(0, q_ - 1, INF, t), update(0, q_ - 1, -INF, t);
else if (p == -1)
update(0, idx(yy_, xx[q]), xx[q], t);
else if (q == -1)
update(idx(yy_, xx[p] - 1) + 1, q_ - 1, xx[p], t);
else {
int l = idx(yy_, xx[p] - 1) + 1, h = idx(yy_, mid(xx[p], xx[q])), r = idx(yy_, xx[q]);
update(l, h, xx[p], t), update(h + 1, r, xx[q], t);
}
}
void add(int i) {
int p = pp[i], q = qq[i];
if (p != -1)
qq[p] = i;
if (q != -1)
pp[q] = i;
}
void append_(int i, int i_) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
ei[i] = (int *) realloc(ei[i], o * 2 * sizeof *ei[i]);
ei[i][o] = i_;
}
void update_(int l, int r, int i_) {
for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1) {
if ((l & 1) == 1)
append_(l++, i_);
if ((r & 1) == 0)
append_(r--, i_);
}
}
int hh[Q], inv[Q], ans[Q];
void solve(int i, int t) {
int o;
for (o = eo[i]; o--; ) {
int i_ = ei[i][o];
remove_(i_, t);
}
if (i >= n_) {
int h = i - n_;
if (h < q_)
ans[hh[h]] = query(inv[hh[h]]);
} else
solve(i << 1 | 0, t + 1), solve(i << 1 | 1, t + 1);
for (o = 0; o < eo[i]; o++) {
int i_ = ei[i][o];
add(i_);
}
pop(t);
}
int main() {
static int tt[N * 2], ii[N], ll[N], rr[N];
static char used[N];
int k, g, h, i;
scanf("%d%d%d", &n, &k, &q_);
for (i = 0; i < n; i++) {
scanf("%d%d%d%d", &xx[i], &gg[i], &tt[i << 1 | 0], &tt[i << 1 | 1]), gg[i]--;
used[gg[i]] = 1;
}
for (g = 0; g < k; g++)
if (!used[g]) {
for (h = 0; h < q_; h++)
printf("-1\n");
return 0;
}
for (h = 0; h < q_; h++)
scanf("%d%d", &yy[h], &tt_[h]);
for (h = 0; h < q_; h++)
hh[h] = h;
compare = compareh_y, sort(hh, 0, q_);
for (h = 0; h < q_; h++)
yy_[h] = yy[hh[h]], inv[hh[h]] = h;
for (i = 0; i < n; i++)
ii[i] = i;
compare = compare_i, sort(ii, 0, n);
pp[ii[0]] = -1, qq[ii[n - 1]] = -1;
ll[0] = 0, rr[n - 1] = q_ - 1;
for (h = 0; h + 1 < n; h++)
if (gg[ii[h]] == gg[ii[h + 1]]) {
int h_ = idx(yy_, mid(xx[ii[h]], xx[ii[h + 1]]));
qq[ii[h]] = ii[h + 1], pp[ii[h + 1]] = ii[h];
rr[h] = h_, ll[h + 1] = h_ + 1;
} else {
qq[ii[h]] = -1, pp[ii[h + 1]] = -1;
rr[h] = q_ - 1, ll[h + 1] = 0;
}
build();
for (h = 0; h < n; h++)
if (ll[h] <= rr[h])
update(ll[h], rr[h], xx[ii[h]], 0);
compare = compareh_t, sort(hh, 0, q_);
for (h = 0; h < q_; h++)
tt1[h] = tt_[hh[h]];
for (i = 0; i < n; i++)
update_(0, idx(tt1, tt[i << 1 | 0] - 1), i), update_(idx(tt1, tt[i << 1 | 1]) + 1, q_ - 1, i);
solve(1, 1);
for (h = 0; h < q_; h++)
printf("%d\n", ans[h]);
return 0;
}
Compilation message
new_home.c: In function 'append_':
new_home.c:163:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
163 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
new_home.c: In function 'main':
new_home.c:207:2: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
207 | scanf("%d%d%d", &n, &k, &q_);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
new_home.c:209:3: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
209 | scanf("%d%d%d%d", &xx[i], &gg[i], &tt[i << 1 | 0], &tt[i << 1 | 1]), gg[i]--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
new_home.c:219:3: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
219 | scanf("%d%d", &yy[h], &tt_[h]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
2 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
620 KB |
Output is correct |
5 |
Correct |
2 ms |
876 KB |
Output is correct |
6 |
Correct |
3 ms |
876 KB |
Output is correct |
7 |
Correct |
2 ms |
748 KB |
Output is correct |
8 |
Correct |
3 ms |
896 KB |
Output is correct |
9 |
Correct |
2 ms |
748 KB |
Output is correct |
10 |
Correct |
3 ms |
876 KB |
Output is correct |
11 |
Correct |
3 ms |
876 KB |
Output is correct |
12 |
Correct |
3 ms |
876 KB |
Output is correct |
13 |
Correct |
2 ms |
876 KB |
Output is correct |
14 |
Correct |
3 ms |
876 KB |
Output is correct |
15 |
Correct |
3 ms |
876 KB |
Output is correct |
16 |
Correct |
2 ms |
876 KB |
Output is correct |
17 |
Correct |
3 ms |
876 KB |
Output is correct |
18 |
Correct |
3 ms |
876 KB |
Output is correct |
19 |
Correct |
3 ms |
876 KB |
Output is correct |
20 |
Correct |
3 ms |
876 KB |
Output is correct |
21 |
Correct |
1 ms |
492 KB |
Output is correct |
22 |
Correct |
2 ms |
748 KB |
Output is correct |
23 |
Correct |
2 ms |
876 KB |
Output is correct |
24 |
Correct |
3 ms |
876 KB |
Output is correct |
25 |
Correct |
3 ms |
876 KB |
Output is correct |
26 |
Correct |
3 ms |
876 KB |
Output is correct |
27 |
Correct |
3 ms |
876 KB |
Output is correct |
28 |
Correct |
3 ms |
876 KB |
Output is correct |
29 |
Correct |
3 ms |
876 KB |
Output is correct |
30 |
Correct |
2 ms |
876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
2 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
620 KB |
Output is correct |
5 |
Correct |
2 ms |
876 KB |
Output is correct |
6 |
Correct |
3 ms |
876 KB |
Output is correct |
7 |
Correct |
2 ms |
748 KB |
Output is correct |
8 |
Correct |
3 ms |
896 KB |
Output is correct |
9 |
Correct |
2 ms |
748 KB |
Output is correct |
10 |
Correct |
3 ms |
876 KB |
Output is correct |
11 |
Correct |
3 ms |
876 KB |
Output is correct |
12 |
Correct |
3 ms |
876 KB |
Output is correct |
13 |
Correct |
2 ms |
876 KB |
Output is correct |
14 |
Correct |
3 ms |
876 KB |
Output is correct |
15 |
Correct |
3 ms |
876 KB |
Output is correct |
16 |
Correct |
2 ms |
876 KB |
Output is correct |
17 |
Correct |
3 ms |
876 KB |
Output is correct |
18 |
Correct |
3 ms |
876 KB |
Output is correct |
19 |
Correct |
3 ms |
876 KB |
Output is correct |
20 |
Correct |
3 ms |
876 KB |
Output is correct |
21 |
Correct |
1 ms |
492 KB |
Output is correct |
22 |
Correct |
2 ms |
748 KB |
Output is correct |
23 |
Correct |
2 ms |
876 KB |
Output is correct |
24 |
Correct |
3 ms |
876 KB |
Output is correct |
25 |
Correct |
3 ms |
876 KB |
Output is correct |
26 |
Correct |
3 ms |
876 KB |
Output is correct |
27 |
Correct |
3 ms |
876 KB |
Output is correct |
28 |
Correct |
3 ms |
876 KB |
Output is correct |
29 |
Correct |
3 ms |
876 KB |
Output is correct |
30 |
Correct |
2 ms |
876 KB |
Output is correct |
31 |
Correct |
1266 ms |
37228 KB |
Output is correct |
32 |
Correct |
462 ms |
23148 KB |
Output is correct |
33 |
Correct |
1063 ms |
37960 KB |
Output is correct |
34 |
Correct |
1312 ms |
38220 KB |
Output is correct |
35 |
Correct |
1236 ms |
37484 KB |
Output is correct |
36 |
Correct |
983 ms |
37200 KB |
Output is correct |
37 |
Correct |
815 ms |
37484 KB |
Output is correct |
38 |
Correct |
688 ms |
37228 KB |
Output is correct |
39 |
Correct |
622 ms |
37100 KB |
Output is correct |
40 |
Correct |
622 ms |
36972 KB |
Output is correct |
41 |
Correct |
1048 ms |
38380 KB |
Output is correct |
42 |
Correct |
1053 ms |
38500 KB |
Output is correct |
43 |
Correct |
71 ms |
10988 KB |
Output is correct |
44 |
Correct |
1090 ms |
38480 KB |
Output is correct |
45 |
Correct |
1093 ms |
38508 KB |
Output is correct |
46 |
Correct |
1073 ms |
38328 KB |
Output is correct |
47 |
Correct |
548 ms |
35940 KB |
Output is correct |
48 |
Correct |
534 ms |
35820 KB |
Output is correct |
49 |
Correct |
615 ms |
36780 KB |
Output is correct |
50 |
Correct |
651 ms |
37228 KB |
Output is correct |
51 |
Correct |
640 ms |
36588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
810 ms |
78000 KB |
Output is correct |
2 |
Correct |
694 ms |
76780 KB |
Output is correct |
3 |
Correct |
640 ms |
71404 KB |
Output is correct |
4 |
Correct |
791 ms |
77908 KB |
Output is correct |
5 |
Correct |
668 ms |
75284 KB |
Output is correct |
6 |
Correct |
683 ms |
76392 KB |
Output is correct |
7 |
Correct |
609 ms |
71428 KB |
Output is correct |
8 |
Correct |
698 ms |
78060 KB |
Output is correct |
9 |
Correct |
756 ms |
78180 KB |
Output is correct |
10 |
Correct |
727 ms |
77420 KB |
Output is correct |
11 |
Correct |
561 ms |
76908 KB |
Output is correct |
12 |
Correct |
603 ms |
77548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5075 ms |
200656 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
2 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
620 KB |
Output is correct |
5 |
Correct |
2 ms |
876 KB |
Output is correct |
6 |
Correct |
3 ms |
876 KB |
Output is correct |
7 |
Correct |
2 ms |
748 KB |
Output is correct |
8 |
Correct |
3 ms |
896 KB |
Output is correct |
9 |
Correct |
2 ms |
748 KB |
Output is correct |
10 |
Correct |
3 ms |
876 KB |
Output is correct |
11 |
Correct |
3 ms |
876 KB |
Output is correct |
12 |
Correct |
3 ms |
876 KB |
Output is correct |
13 |
Correct |
2 ms |
876 KB |
Output is correct |
14 |
Correct |
3 ms |
876 KB |
Output is correct |
15 |
Correct |
3 ms |
876 KB |
Output is correct |
16 |
Correct |
2 ms |
876 KB |
Output is correct |
17 |
Correct |
3 ms |
876 KB |
Output is correct |
18 |
Correct |
3 ms |
876 KB |
Output is correct |
19 |
Correct |
3 ms |
876 KB |
Output is correct |
20 |
Correct |
3 ms |
876 KB |
Output is correct |
21 |
Correct |
1 ms |
492 KB |
Output is correct |
22 |
Correct |
2 ms |
748 KB |
Output is correct |
23 |
Correct |
2 ms |
876 KB |
Output is correct |
24 |
Correct |
3 ms |
876 KB |
Output is correct |
25 |
Correct |
3 ms |
876 KB |
Output is correct |
26 |
Correct |
3 ms |
876 KB |
Output is correct |
27 |
Correct |
3 ms |
876 KB |
Output is correct |
28 |
Correct |
3 ms |
876 KB |
Output is correct |
29 |
Correct |
3 ms |
876 KB |
Output is correct |
30 |
Correct |
2 ms |
876 KB |
Output is correct |
31 |
Correct |
1266 ms |
37228 KB |
Output is correct |
32 |
Correct |
462 ms |
23148 KB |
Output is correct |
33 |
Correct |
1063 ms |
37960 KB |
Output is correct |
34 |
Correct |
1312 ms |
38220 KB |
Output is correct |
35 |
Correct |
1236 ms |
37484 KB |
Output is correct |
36 |
Correct |
983 ms |
37200 KB |
Output is correct |
37 |
Correct |
815 ms |
37484 KB |
Output is correct |
38 |
Correct |
688 ms |
37228 KB |
Output is correct |
39 |
Correct |
622 ms |
37100 KB |
Output is correct |
40 |
Correct |
622 ms |
36972 KB |
Output is correct |
41 |
Correct |
1048 ms |
38380 KB |
Output is correct |
42 |
Correct |
1053 ms |
38500 KB |
Output is correct |
43 |
Correct |
71 ms |
10988 KB |
Output is correct |
44 |
Correct |
1090 ms |
38480 KB |
Output is correct |
45 |
Correct |
1093 ms |
38508 KB |
Output is correct |
46 |
Correct |
1073 ms |
38328 KB |
Output is correct |
47 |
Correct |
548 ms |
35940 KB |
Output is correct |
48 |
Correct |
534 ms |
35820 KB |
Output is correct |
49 |
Correct |
615 ms |
36780 KB |
Output is correct |
50 |
Correct |
651 ms |
37228 KB |
Output is correct |
51 |
Correct |
640 ms |
36588 KB |
Output is correct |
52 |
Correct |
271 ms |
17516 KB |
Output is correct |
53 |
Correct |
281 ms |
17108 KB |
Output is correct |
54 |
Correct |
771 ms |
36204 KB |
Output is correct |
55 |
Correct |
774 ms |
37228 KB |
Output is correct |
56 |
Correct |
638 ms |
36460 KB |
Output is correct |
57 |
Correct |
963 ms |
37612 KB |
Output is correct |
58 |
Correct |
794 ms |
37868 KB |
Output is correct |
59 |
Correct |
666 ms |
37228 KB |
Output is correct |
60 |
Correct |
974 ms |
38124 KB |
Output is correct |
61 |
Correct |
73 ms |
10988 KB |
Output is correct |
62 |
Correct |
241 ms |
17132 KB |
Output is correct |
63 |
Correct |
549 ms |
36972 KB |
Output is correct |
64 |
Correct |
664 ms |
37100 KB |
Output is correct |
65 |
Correct |
894 ms |
37996 KB |
Output is correct |
66 |
Correct |
1046 ms |
38636 KB |
Output is correct |
67 |
Correct |
584 ms |
30720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
2 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
620 KB |
Output is correct |
5 |
Correct |
2 ms |
876 KB |
Output is correct |
6 |
Correct |
3 ms |
876 KB |
Output is correct |
7 |
Correct |
2 ms |
748 KB |
Output is correct |
8 |
Correct |
3 ms |
896 KB |
Output is correct |
9 |
Correct |
2 ms |
748 KB |
Output is correct |
10 |
Correct |
3 ms |
876 KB |
Output is correct |
11 |
Correct |
3 ms |
876 KB |
Output is correct |
12 |
Correct |
3 ms |
876 KB |
Output is correct |
13 |
Correct |
2 ms |
876 KB |
Output is correct |
14 |
Correct |
3 ms |
876 KB |
Output is correct |
15 |
Correct |
3 ms |
876 KB |
Output is correct |
16 |
Correct |
2 ms |
876 KB |
Output is correct |
17 |
Correct |
3 ms |
876 KB |
Output is correct |
18 |
Correct |
3 ms |
876 KB |
Output is correct |
19 |
Correct |
3 ms |
876 KB |
Output is correct |
20 |
Correct |
3 ms |
876 KB |
Output is correct |
21 |
Correct |
1 ms |
492 KB |
Output is correct |
22 |
Correct |
2 ms |
748 KB |
Output is correct |
23 |
Correct |
2 ms |
876 KB |
Output is correct |
24 |
Correct |
3 ms |
876 KB |
Output is correct |
25 |
Correct |
3 ms |
876 KB |
Output is correct |
26 |
Correct |
3 ms |
876 KB |
Output is correct |
27 |
Correct |
3 ms |
876 KB |
Output is correct |
28 |
Correct |
3 ms |
876 KB |
Output is correct |
29 |
Correct |
3 ms |
876 KB |
Output is correct |
30 |
Correct |
2 ms |
876 KB |
Output is correct |
31 |
Correct |
1266 ms |
37228 KB |
Output is correct |
32 |
Correct |
462 ms |
23148 KB |
Output is correct |
33 |
Correct |
1063 ms |
37960 KB |
Output is correct |
34 |
Correct |
1312 ms |
38220 KB |
Output is correct |
35 |
Correct |
1236 ms |
37484 KB |
Output is correct |
36 |
Correct |
983 ms |
37200 KB |
Output is correct |
37 |
Correct |
815 ms |
37484 KB |
Output is correct |
38 |
Correct |
688 ms |
37228 KB |
Output is correct |
39 |
Correct |
622 ms |
37100 KB |
Output is correct |
40 |
Correct |
622 ms |
36972 KB |
Output is correct |
41 |
Correct |
1048 ms |
38380 KB |
Output is correct |
42 |
Correct |
1053 ms |
38500 KB |
Output is correct |
43 |
Correct |
71 ms |
10988 KB |
Output is correct |
44 |
Correct |
1090 ms |
38480 KB |
Output is correct |
45 |
Correct |
1093 ms |
38508 KB |
Output is correct |
46 |
Correct |
1073 ms |
38328 KB |
Output is correct |
47 |
Correct |
548 ms |
35940 KB |
Output is correct |
48 |
Correct |
534 ms |
35820 KB |
Output is correct |
49 |
Correct |
615 ms |
36780 KB |
Output is correct |
50 |
Correct |
651 ms |
37228 KB |
Output is correct |
51 |
Correct |
640 ms |
36588 KB |
Output is correct |
52 |
Correct |
810 ms |
78000 KB |
Output is correct |
53 |
Correct |
694 ms |
76780 KB |
Output is correct |
54 |
Correct |
640 ms |
71404 KB |
Output is correct |
55 |
Correct |
791 ms |
77908 KB |
Output is correct |
56 |
Correct |
668 ms |
75284 KB |
Output is correct |
57 |
Correct |
683 ms |
76392 KB |
Output is correct |
58 |
Correct |
609 ms |
71428 KB |
Output is correct |
59 |
Correct |
698 ms |
78060 KB |
Output is correct |
60 |
Correct |
756 ms |
78180 KB |
Output is correct |
61 |
Correct |
727 ms |
77420 KB |
Output is correct |
62 |
Correct |
561 ms |
76908 KB |
Output is correct |
63 |
Correct |
603 ms |
77548 KB |
Output is correct |
64 |
Execution timed out |
5075 ms |
200656 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |