Submission #445856

# Submission time Handle Problem Language Result Execution time Memory
445856 2021-07-19T22:55:20 Z rainboy Sky Walking (IOI19_walk) C++14
57 / 100
554 ms 280500 KB
#include "walk.h"
#include <stdlib.h>
#include <string.h>

using namespace std;

typedef vector<int> vi;

const int N = 100000, M = 100000, N_ = 1 << 18, N1 = 4000000, M_ = 4000000;	/* N_ = pow2(ceil(log2(N))) */
const long long INF = 0x3f3f3f3f3f3f3f3f;

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 ii[N], hh[M * 2];

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 ij[M_], ww[M_], m_;
int *eh[N1], eo[N1], 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 newnode() {
	eh[n_] = (int *) malloc(2 * sizeof *eh[n_]);
	return n_++;
}

void add(int i, int j, int w) {
	int h = m_++;

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

int pp[N], qq[N], top[N], yy1[N];

void push(int i, int y) {
	int t = newnode();

	add(top[i], 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, n_ * 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;
}

int xx[N], yy[N], ll[M], rr[M], xx1[M * 2], zz[M], idx[M], zz1[M];

long long solve_using_dijkstra(vi xx_, vi yy_, vi ll_, vi rr_, vi zz_, int s, int t) {
	int n = xx_.size(), m = zz_.size(), h, i;

	for (i = 0; i < n; i++) {
		xx[i] = xx_[i], yy[i] = yy_[i];
		ii[i] = i;
	}
	aa = yy, sort(ii, 0, n);
	for (h = 0; h < m; h++) {
		ll[h] = ll_[h], rr[h] = rr_[h], zz[h] = zz_[h];
		hh[h] = h;
	}
	aa = zz, sort(hh, 0, m);
	for (i = 0; i < n; i++) {
		pp[i] = i - 1, qq[i] = i + 1;
		top[i] = newnode(), yy1[i] = 0;
	}
	for (h = 0, i = 0; h < m; h++) {
		int h_ = hh[h], i_;

		while (i < n && yy[i_ = ii[i]] < zz[h_]) {
			if (pp[i_] != -1)
				qq[pp[i_]] = qq[i_];
			if (qq[i_] != n)
				pp[qq[i_]] = pp[i_];
			i++;
		}
		for (i_ = ll[h_]; i_ != rr[h_]; i_ = qq[i_])
			push(i_, zz[h_]);
		push(i_, zz[h_]);
		for (i_ = ll[h_]; i_ != rr[h_]; i_ = qq[i_])
			add(top[i_], top[qq[i_]], xx[qq[i_]] - xx[i_]);
	}
	for (i = 0; i < n; i++)
		push(i, yy[i]);
	return dijkstra(s, t);
}

long long st1[N_ * 2], st2[N_ * 2];

void pul(int i) {
	int l = i << 1, r = l | 1;

	st1[i] = min(st1[l], st1[r]), st2[i] = min(st2[l], st2[r]);
}

void update(int i, long long x) {
	if (x == INF)
		st1[n_ + i] = st2[n_ + i] = INF;
	else
		st1[n_ + i] = x - zz1[i], st2[n_ + i] = x + zz1[i];
	i += n_;
	while (i > 1)
		pul(i >>= 1);
}

long long query(int i) {
	long long x, y;
	int l, r;

	x = INF;
	for (l = 0 + n_, r = i + n_; l <= r; l >>= 1, r >>= 1) {
		if ((l & 1) == 1)
			x = min(x, st1[l++]);
		if ((r & 1) == 0)
			x = min(x, st1[r--]);
	}
	y = INF;
	for (l = i + n_, r = n_ - 1 + n_; l <= r; l >>= 1, r >>= 1) {
		if ((l & 1) == 1)
			y = min(y, st2[l++]);
		if ((r & 1) == 0)
			y = min(y, st2[r--]);
	}
	return min(x == INF ? INF : x + zz1[i], y == INF ? INF : y - zz1[i]);
}

long long dp[M];

long long solve_using_segtree(vi xx_, vi ll_, vi rr_, vi zz_) {
	long long ans;
	int n = xx_.size(), m = zz_.size(), h, i;

	for (i = 0; i < n; i++)
		xx[i] = xx_[i];
	for (h = 0; h < m; h++) {
		ll[h] = ll_[h], rr[h] = rr_[h], zz[h] = zz_[h];
		hh[h] = h;
	}
	aa = zz, sort(hh, 0, m);
	for (h = 0; h < m; h++)
		idx[hh[h]] = h, zz1[h] = zz[hh[h]];
	for (h = 0; h < m * 2; h++) {
		xx1[h] = (h & 1) == 0 ? ll[h >> 1] << 1 | 0 : rr[h >> 1] << 1 | 1;
		hh[h] = h;
	}
	aa = xx1, sort(hh, 0, m * 2);
	n_ = 1;
	while (n_ < m)
		n_ <<= 1;
	memset(st1, 0x3f, n_ * 2 * sizeof *st1), memset(st2, 0x3f, n_ * 2 * sizeof *st2);
	ans = INF;
	for (h = 0; h < m * 2; h++) {
		int h_ = hh[h] >> 1;

		if ((hh[h] & 1) == 0)
			dp[h_] = xx1[hh[h]] == 0 ? zz[h_] : query(idx[h_]), update(idx[h_], dp[h_]);
		else {
			update(idx[h_], INF);
			if (xx1[hh[h]] == (n - 1 << 1 | 1))
				ans = min(ans, dp[h_] + zz[h_] + xx[n - 1] - xx[0]);
		}
	}
	return ans == INF ? -1 : ans;
}

long long min_distance(vi xx_, vi yy_, vi ll_, vi rr_, vi zz_, int s, int t) {
	int n = xx_.size();

	return s == 0 && t == n - 1 ? solve_using_segtree(xx_, ll_, rr_, zz_) : solve_using_dijkstra(xx_, yy_, ll_, rr_, zz_, s, t);
}

Compilation message

walk.cpp: In function 'void append(int, int)':
walk.cpp:49:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   49 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
walk.cpp: In function 'long long int solve_using_segtree(vi, vi, vi, vi)':
walk.cpp:247:25: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
  247 |    if (xx1[hh[h]] == (n - 1 << 1 | 1))
      |                       ~~^~~
# Verdict Execution time Memory 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 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 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 1 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 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
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 144 ms 53464 KB Output is correct
4 Correct 449 ms 70848 KB Output is correct
5 Correct 198 ms 59424 KB Output is correct
6 Correct 225 ms 54328 KB Output is correct
7 Correct 205 ms 59768 KB Output is correct
8 Correct 216 ms 69052 KB Output is correct
9 Correct 247 ms 60028 KB Output is correct
10 Correct 554 ms 91744 KB Output is correct
11 Correct 167 ms 35836 KB Output is correct
12 Correct 170 ms 14608 KB Output is correct
13 Correct 195 ms 14636 KB Output is correct
14 Correct 131 ms 32816 KB Output is correct
15 Correct 148 ms 35524 KB Output is correct
16 Correct 172 ms 34788 KB Output is correct
17 Correct 166 ms 33860 KB Output is correct
18 Correct 102 ms 14576 KB Output is correct
19 Correct 6 ms 1996 KB Output is correct
20 Correct 57 ms 17220 KB Output is correct
21 Correct 147 ms 14032 KB Output is correct
22 Correct 138 ms 14512 KB Output is correct
23 Correct 143 ms 14532 KB Output is correct
24 Correct 142 ms 14436 KB Output is correct
25 Correct 134 ms 14152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 6356 KB Output is correct
2 Correct 131 ms 12268 KB Output is correct
3 Correct 133 ms 12552 KB Output is correct
4 Correct 167 ms 14544 KB Output is correct
5 Correct 167 ms 14600 KB Output is correct
6 Correct 178 ms 14640 KB Output is correct
7 Correct 95 ms 8624 KB Output is correct
8 Correct 180 ms 14532 KB Output is correct
9 Correct 161 ms 14516 KB Output is correct
10 Correct 135 ms 14704 KB Output is correct
11 Correct 16 ms 1484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 6356 KB Output is correct
2 Correct 131 ms 12268 KB Output is correct
3 Correct 133 ms 12552 KB Output is correct
4 Correct 167 ms 14544 KB Output is correct
5 Correct 167 ms 14600 KB Output is correct
6 Correct 178 ms 14640 KB Output is correct
7 Correct 95 ms 8624 KB Output is correct
8 Correct 180 ms 14532 KB Output is correct
9 Correct 161 ms 14516 KB Output is correct
10 Correct 135 ms 14704 KB Output is correct
11 Correct 16 ms 1484 KB Output is correct
12 Correct 146 ms 14568 KB Output is correct
13 Correct 194 ms 18624 KB Output is correct
14 Correct 175 ms 18724 KB Output is correct
15 Correct 172 ms 18360 KB Output is correct
16 Correct 152 ms 18676 KB Output is correct
17 Correct 152 ms 18592 KB Output is correct
18 Correct 156 ms 18560 KB Output is correct
19 Correct 168 ms 18628 KB Output is correct
20 Correct 96 ms 11540 KB Output is correct
21 Correct 27 ms 4780 KB Output is correct
22 Correct 143 ms 17100 KB Output is correct
23 Correct 150 ms 17576 KB Output is correct
24 Correct 149 ms 17688 KB Output is correct
25 Correct 135 ms 17104 KB Output is correct
26 Correct 142 ms 17556 KB Output is correct
27 Correct 183 ms 18576 KB Output is correct
28 Correct 164 ms 18628 KB Output is correct
29 Correct 178 ms 18612 KB Output is correct
30 Correct 94 ms 11712 KB Output is correct
31 Correct 172 ms 18608 KB Output is correct
32 Correct 139 ms 17348 KB Output is correct
33 Correct 144 ms 17864 KB Output is correct
34 Correct 135 ms 17700 KB Output is correct
35 Correct 143 ms 17660 KB Output is correct
36 Correct 141 ms 17316 KB Output is correct
37 Correct 134 ms 16744 KB Output is correct
38 Correct 142 ms 17824 KB Output is correct
39 Correct 150 ms 17464 KB Output is correct
40 Correct 140 ms 17400 KB Output is correct
41 Correct 133 ms 16832 KB Output is correct
# Verdict Execution time Memory 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 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 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 1 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 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 0 ms 332 KB Output is correct
20 Correct 144 ms 53464 KB Output is correct
21 Correct 449 ms 70848 KB Output is correct
22 Correct 198 ms 59424 KB Output is correct
23 Correct 225 ms 54328 KB Output is correct
24 Correct 205 ms 59768 KB Output is correct
25 Correct 216 ms 69052 KB Output is correct
26 Correct 247 ms 60028 KB Output is correct
27 Correct 554 ms 91744 KB Output is correct
28 Correct 167 ms 35836 KB Output is correct
29 Correct 170 ms 14608 KB Output is correct
30 Correct 195 ms 14636 KB Output is correct
31 Correct 131 ms 32816 KB Output is correct
32 Correct 148 ms 35524 KB Output is correct
33 Correct 172 ms 34788 KB Output is correct
34 Correct 166 ms 33860 KB Output is correct
35 Correct 102 ms 14576 KB Output is correct
36 Correct 6 ms 1996 KB Output is correct
37 Correct 57 ms 17220 KB Output is correct
38 Correct 147 ms 14032 KB Output is correct
39 Correct 138 ms 14512 KB Output is correct
40 Correct 143 ms 14532 KB Output is correct
41 Correct 142 ms 14436 KB Output is correct
42 Correct 134 ms 14152 KB Output is correct
43 Correct 54 ms 6356 KB Output is correct
44 Correct 131 ms 12268 KB Output is correct
45 Correct 133 ms 12552 KB Output is correct
46 Correct 167 ms 14544 KB Output is correct
47 Correct 167 ms 14600 KB Output is correct
48 Correct 178 ms 14640 KB Output is correct
49 Correct 95 ms 8624 KB Output is correct
50 Correct 180 ms 14532 KB Output is correct
51 Correct 161 ms 14516 KB Output is correct
52 Correct 135 ms 14704 KB Output is correct
53 Correct 16 ms 1484 KB Output is correct
54 Correct 146 ms 14568 KB Output is correct
55 Correct 194 ms 18624 KB Output is correct
56 Correct 175 ms 18724 KB Output is correct
57 Correct 172 ms 18360 KB Output is correct
58 Correct 152 ms 18676 KB Output is correct
59 Correct 152 ms 18592 KB Output is correct
60 Correct 156 ms 18560 KB Output is correct
61 Correct 168 ms 18628 KB Output is correct
62 Correct 96 ms 11540 KB Output is correct
63 Correct 27 ms 4780 KB Output is correct
64 Correct 143 ms 17100 KB Output is correct
65 Correct 150 ms 17576 KB Output is correct
66 Correct 149 ms 17688 KB Output is correct
67 Correct 135 ms 17104 KB Output is correct
68 Correct 142 ms 17556 KB Output is correct
69 Correct 183 ms 18576 KB Output is correct
70 Correct 164 ms 18628 KB Output is correct
71 Correct 178 ms 18612 KB Output is correct
72 Correct 94 ms 11712 KB Output is correct
73 Correct 172 ms 18608 KB Output is correct
74 Correct 139 ms 17348 KB Output is correct
75 Correct 144 ms 17864 KB Output is correct
76 Correct 135 ms 17700 KB Output is correct
77 Correct 143 ms 17660 KB Output is correct
78 Correct 141 ms 17316 KB Output is correct
79 Correct 134 ms 16744 KB Output is correct
80 Correct 142 ms 17824 KB Output is correct
81 Correct 150 ms 17464 KB Output is correct
82 Correct 140 ms 17400 KB Output is correct
83 Correct 133 ms 16832 KB Output is correct
84 Correct 44 ms 10796 KB Output is correct
85 Runtime error 426 ms 280500 KB Execution killed with signal 11
86 Halted 0 ms 0 KB -