답안 #446368

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
446368 2021-07-21T17:20:02 Z rainboy Sky Walking (IOI19_walk) C++14
100 / 100
668 ms 67372 KB
/* https://ioi2019.az/source/Tasks/Day2/Walk/review.pdf */
#include "walk.h"
#include <stdlib.h>
#include <string.h>

using namespace std;

typedef vector<int> vi;

const int N = 100000, M = 100000, N_ = 1 << 17, N1 = N * 13, M1 = N1 * 2;	/* N_ = pow2(ceil(log2(N))) */
const long long INF = 0x3f3f3f3f3f3f3f3f;

int max(int a, int b) { return a > b ? a : b; }

unsigned int X = 12345;

int rand_() {
	return (X *= 3) >> 1;
}

int *aa;

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 (aa[ii[j]] == aa[i_])
				j++;
			else if (aa[ii[j]] < aa[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 xx[N], yy[N], n;
int hh[M], ii[M], jj[M], zz[M], *ei[M], eo[M], m;
int *eh[N1], eo_[N1], n1;
int ij[M1], ww[M1], m1;

void append(int **eh, int *eo, 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 st[N_ * 2], n_;

void update(int l, int r, int h) {
	for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1) {
		if ((l & 1) == 1)
			st[l++] = h;
		if ((r & 1) == 0)
			st[r--] = h;
	}
}

int query(int i) {
	int h = -1;

	for (i += n_; i > 0; i >>= 1)
		h = max(h, st[i]);
	return h == -1 ? -1 : hh[h];
}

void add(int h, int i) {
	int h_;

	append(ei, eo, h, i);
	if ((h_ = query(i)) != -1)
		append(ei, eo, h_, i);
}

int top[N], yy1[N];

int newnode() {
	eh[n1] = (int *) malloc(2 * sizeof *eh[n1]);
	return n1++;
}

void add_(int i, int j, int w) {
	int h = m1++;

	ij[h] = i ^ j, ww[h] = w;
	append(eh, eo_, i, h), append(eh, eo_, j, h);
}

void push(int i, int y) {
	int t = top[i], t_ = newnode();

	add_(t, t_, y - yy1[i]);
	top[i] = t_, yy1[i] = y;
}

long long dd[N1]; int pq[N1], iq[1 + N1], cnt;

int lt(int i, int j) { return dd[i] < dd[j]; }

int p2(int p) {
	return (p *= 2) > cnt ? 0 : (p < cnt && lt(iq[p + 1], iq[p]) ? p + 1 : p);
}

void pq_up(int i) {
	int p, q, j;

	for (p = pq[i]; (q = p / 2) && lt(i, j = iq[q]); p = q)
		iq[pq[j] = p] = j;
	iq[pq[i] = p] = i;
}

void pq_dn(int i) {
	int p, q, j;

	for (p = pq[i]; (q = p2(p)) && lt(j = iq[q], i); p = q)
		iq[pq[j] = p] = j;
	iq[pq[i] = p] = i;
}

void pq_add_last(int i) {
	iq[pq[i] = ++cnt] = i;
}

int pq_remove_first() {
	int i = iq[1], j = iq[cnt--];

	if (j != i)
		pq[j] = 1, pq_dn(j);
	pq[i] = 0;
	return i;
}

long long dijkstra(int s, int t) {
	memset(dd, 0x3f, n1 * sizeof *dd);
	dd[s] = 0, pq_add_last(s);
	while (cnt) {
		int i = pq_remove_first(), o;

		if (i == t)
			return dd[i];
		for (o = eo_[i]; o--; ) {
			int h = eh[i][o], j = i ^ ij[h];
			long long d = dd[i] + ww[h];

			if (dd[j] > d) {
				if (dd[j] == INF)
					pq_add_last(j);
				dd[j] = d, pq_up(j);
			}
		}
	}
	return -1;
}

long long min_distance(vi xx_, vi yy_, vi ii_, vi jj_, vi zz_, int s, int t) {
	int h, i, j, p1, q1, p2, q2, tmp;

	n = xx_.size(), m = zz_.size();
	for (i = 0; i < n; i++)
		xx[i] = xx_[i], yy[i] = yy_[i];
	for (h = 0; h < m; h++) {
		ii[h] = ii_[h], jj[h] = jj_[h], zz[h] = zz_[h];
		hh[h] = h;
	}
	aa = zz, sort(hh, 0, m);
	if (s > t)
		tmp = s, s = t, t = tmp;
	for (h = 0; h < m; h++)
		ei[h] = (int *) malloc(2 * sizeof *ei[h]);
	n_ = 1;
	while (n_ < n)
		n_ <<= 1;
	memset(st, -1, n_ * 2 * sizeof *st);
	for (h = 0, p1 = s, q1 = s, p2 = t, q2 = t; h < m; h++) {
		int h_ = hh[h];

		while (p1 >= 0 && yy[p1] < zz[h_])
			p1--;
		while (q1 < n && yy[q1] < zz[h_])
			q1++;
		while (p2 >= 0 && yy[p2] < zz[h_])
			p2--;
		while (q2 < n && yy[q2] < zz[h_])
			q2++;
		add(h_, ii[h_]), add(h_, jj[h_]);
		if (ii[h_] < s && s < jj[h_])
			add(h_, p1), add(h_, q1);
		if (ii[h_] < t && t < jj[h_])
			add(h_, p2), add(h_, q2);
		update(ii[h_], jj[h_], h);
	}
	for (i = 0; i < n; i++)
		top[i] = newnode(), yy1[i] = 0;
	for (h = 0, p1 = s, q1 = s, p2 = t, q2 = t; h < m; h++) {
		int h_ = hh[h], o, o_;

		aa = xx, sort(ei[h_], 0, eo[h_]);
		for (o = 0, o_ = 0; o < eo[h_]; o++)
			if (o_ == 0 || ei[h_][o_ - 1] != ei[h_][o])
				ei[h_][o_++] = ei[h_][o];
		eo[h_] = o_;
		for (o = 0; o < eo[h_]; o++) {
			i = ei[h_][o];
			push(i, zz[h_]);
		}
		for (o = 0; o + 1 < eo[h_]; o++) {
			i = ei[h_][o], j = ei[h_][o + 1];
			add_(top[i], top[j], xx[j] - xx[i]);
		}
	}
	return dijkstra(s, t);
}

Compilation message

walk.cpp: In function 'void append(int**, int*, int, int)':
walk.cpp:50:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   50 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 420 KB Output is correct
6 Correct 1 ms 412 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 158 ms 33684 KB Output is correct
4 Correct 312 ms 45232 KB Output is correct
5 Correct 170 ms 35468 KB Output is correct
6 Correct 169 ms 34980 KB Output is correct
7 Correct 178 ms 36080 KB Output is correct
8 Correct 184 ms 35664 KB Output is correct
9 Correct 236 ms 41812 KB Output is correct
10 Correct 333 ms 45600 KB Output is correct
11 Correct 205 ms 34292 KB Output is correct
12 Correct 243 ms 35376 KB Output is correct
13 Correct 377 ms 46748 KB Output is correct
14 Correct 187 ms 34836 KB Output is correct
15 Correct 194 ms 35680 KB Output is correct
16 Correct 184 ms 34372 KB Output is correct
17 Correct 195 ms 33240 KB Output is correct
18 Correct 184 ms 33632 KB Output is correct
19 Correct 7 ms 2088 KB Output is correct
20 Correct 68 ms 17956 KB Output is correct
21 Correct 206 ms 32056 KB Output is correct
22 Correct 131 ms 34180 KB Output is correct
23 Correct 246 ms 38112 KB Output is correct
24 Correct 180 ms 34156 KB Output is correct
25 Correct 184 ms 33336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 63 ms 13124 KB Output is correct
2 Correct 267 ms 37756 KB Output is correct
3 Correct 402 ms 39620 KB Output is correct
4 Correct 568 ms 50384 KB Output is correct
5 Correct 668 ms 50420 KB Output is correct
6 Correct 535 ms 48964 KB Output is correct
7 Correct 243 ms 31616 KB Output is correct
8 Correct 235 ms 35500 KB Output is correct
9 Correct 555 ms 47044 KB Output is correct
10 Correct 150 ms 33988 KB Output is correct
11 Correct 15 ms 5792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 63 ms 13124 KB Output is correct
2 Correct 267 ms 37756 KB Output is correct
3 Correct 402 ms 39620 KB Output is correct
4 Correct 568 ms 50384 KB Output is correct
5 Correct 668 ms 50420 KB Output is correct
6 Correct 535 ms 48964 KB Output is correct
7 Correct 243 ms 31616 KB Output is correct
8 Correct 235 ms 35500 KB Output is correct
9 Correct 555 ms 47044 KB Output is correct
10 Correct 150 ms 33988 KB Output is correct
11 Correct 15 ms 5792 KB Output is correct
12 Correct 376 ms 39536 KB Output is correct
13 Correct 253 ms 48328 KB Output is correct
14 Correct 606 ms 50336 KB Output is correct
15 Correct 383 ms 45348 KB Output is correct
16 Correct 420 ms 45640 KB Output is correct
17 Correct 465 ms 50584 KB Output is correct
18 Correct 403 ms 45460 KB Output is correct
19 Correct 420 ms 45640 KB Output is correct
20 Correct 249 ms 30968 KB Output is correct
21 Correct 34 ms 12192 KB Output is correct
22 Correct 191 ms 42140 KB Output is correct
23 Correct 203 ms 40924 KB Output is correct
24 Correct 174 ms 35448 KB Output is correct
25 Correct 192 ms 38596 KB Output is correct
26 Correct 153 ms 32312 KB Output is correct
27 Correct 656 ms 50636 KB Output is correct
28 Correct 260 ms 48048 KB Output is correct
29 Correct 561 ms 48744 KB Output is correct
30 Correct 249 ms 31760 KB Output is correct
31 Correct 487 ms 46764 KB Output is correct
32 Correct 168 ms 33248 KB Output is correct
33 Correct 148 ms 34340 KB Output is correct
34 Correct 192 ms 36144 KB Output is correct
35 Correct 217 ms 37492 KB Output is correct
36 Correct 216 ms 35180 KB Output is correct
37 Correct 184 ms 32068 KB Output is correct
38 Correct 127 ms 34224 KB Output is correct
39 Correct 235 ms 38328 KB Output is correct
40 Correct 173 ms 34160 KB Output is correct
41 Correct 188 ms 33328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 420 KB Output is correct
6 Correct 1 ms 412 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 158 ms 33684 KB Output is correct
21 Correct 312 ms 45232 KB Output is correct
22 Correct 170 ms 35468 KB Output is correct
23 Correct 169 ms 34980 KB Output is correct
24 Correct 178 ms 36080 KB Output is correct
25 Correct 184 ms 35664 KB Output is correct
26 Correct 236 ms 41812 KB Output is correct
27 Correct 333 ms 45600 KB Output is correct
28 Correct 205 ms 34292 KB Output is correct
29 Correct 243 ms 35376 KB Output is correct
30 Correct 377 ms 46748 KB Output is correct
31 Correct 187 ms 34836 KB Output is correct
32 Correct 194 ms 35680 KB Output is correct
33 Correct 184 ms 34372 KB Output is correct
34 Correct 195 ms 33240 KB Output is correct
35 Correct 184 ms 33632 KB Output is correct
36 Correct 7 ms 2088 KB Output is correct
37 Correct 68 ms 17956 KB Output is correct
38 Correct 206 ms 32056 KB Output is correct
39 Correct 131 ms 34180 KB Output is correct
40 Correct 246 ms 38112 KB Output is correct
41 Correct 180 ms 34156 KB Output is correct
42 Correct 184 ms 33336 KB Output is correct
43 Correct 63 ms 13124 KB Output is correct
44 Correct 267 ms 37756 KB Output is correct
45 Correct 402 ms 39620 KB Output is correct
46 Correct 568 ms 50384 KB Output is correct
47 Correct 668 ms 50420 KB Output is correct
48 Correct 535 ms 48964 KB Output is correct
49 Correct 243 ms 31616 KB Output is correct
50 Correct 235 ms 35500 KB Output is correct
51 Correct 555 ms 47044 KB Output is correct
52 Correct 150 ms 33988 KB Output is correct
53 Correct 15 ms 5792 KB Output is correct
54 Correct 376 ms 39536 KB Output is correct
55 Correct 253 ms 48328 KB Output is correct
56 Correct 606 ms 50336 KB Output is correct
57 Correct 383 ms 45348 KB Output is correct
58 Correct 420 ms 45640 KB Output is correct
59 Correct 465 ms 50584 KB Output is correct
60 Correct 403 ms 45460 KB Output is correct
61 Correct 420 ms 45640 KB Output is correct
62 Correct 249 ms 30968 KB Output is correct
63 Correct 34 ms 12192 KB Output is correct
64 Correct 191 ms 42140 KB Output is correct
65 Correct 203 ms 40924 KB Output is correct
66 Correct 174 ms 35448 KB Output is correct
67 Correct 192 ms 38596 KB Output is correct
68 Correct 153 ms 32312 KB Output is correct
69 Correct 656 ms 50636 KB Output is correct
70 Correct 260 ms 48048 KB Output is correct
71 Correct 561 ms 48744 KB Output is correct
72 Correct 249 ms 31760 KB Output is correct
73 Correct 487 ms 46764 KB Output is correct
74 Correct 168 ms 33248 KB Output is correct
75 Correct 148 ms 34340 KB Output is correct
76 Correct 192 ms 36144 KB Output is correct
77 Correct 217 ms 37492 KB Output is correct
78 Correct 216 ms 35180 KB Output is correct
79 Correct 184 ms 32068 KB Output is correct
80 Correct 127 ms 34224 KB Output is correct
81 Correct 235 ms 38328 KB Output is correct
82 Correct 173 ms 34160 KB Output is correct
83 Correct 188 ms 33328 KB Output is correct
84 Correct 49 ms 10568 KB Output is correct
85 Correct 362 ms 40832 KB Output is correct
86 Correct 502 ms 58052 KB Output is correct
87 Correct 49 ms 14752 KB Output is correct
88 Correct 43 ms 14636 KB Output is correct
89 Correct 46 ms 14756 KB Output is correct
90 Correct 16 ms 4180 KB Output is correct
91 Correct 1 ms 460 KB Output is correct
92 Correct 10 ms 2776 KB Output is correct
93 Correct 108 ms 24620 KB Output is correct
94 Correct 34 ms 12100 KB Output is correct
95 Correct 208 ms 43040 KB Output is correct
96 Correct 205 ms 41028 KB Output is correct
97 Correct 183 ms 36420 KB Output is correct
98 Correct 187 ms 38432 KB Output is correct
99 Correct 394 ms 67372 KB Output is correct
100 Correct 451 ms 50792 KB Output is correct
101 Correct 288 ms 54472 KB Output is correct
102 Correct 148 ms 31456 KB Output is correct
103 Correct 169 ms 33216 KB Output is correct
104 Correct 139 ms 34144 KB Output is correct
105 Correct 190 ms 36012 KB Output is correct
106 Correct 172 ms 35000 KB Output is correct
107 Correct 211 ms 37152 KB Output is correct
108 Correct 23 ms 4336 KB Output is correct
109 Correct 450 ms 41728 KB Output is correct
110 Correct 282 ms 49568 KB Output is correct
111 Correct 231 ms 48352 KB Output is correct
112 Correct 206 ms 37060 KB Output is correct
113 Correct 221 ms 36280 KB Output is correct