Submission #718493

# Submission time Handle Problem Language Result Execution time Memory
718493 2023-04-04T08:25:04 Z Radin_Zahedi2 Swapping Cities (APIO20_swap) C++17
100 / 100
199 ms 32072 KB
#include<bits/stdc++.h>
#include "swap.h"
//#include <vector>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (int)x.size()
#define endl '\n'
const int inf = 1e9 + 1;


const int N = 1e5;
const int L = 17;
int par[N];
vector<int> have[N];
int endps[N][2];

int path[N];

int edge[N];
int ind[N];
int sprc[L][N];

void cresprc(vector<int> &v) {
	int n = sz(v);

	for (int ind = 0; ind < n; ind++) {
		sprc[0][ind] = edge[v[ind]];
	}

	for (int lvl = 1; lvl < L; lvl++) {
		int len = (1 << lvl);

		for (int i = 0; n - i >= len; i++) {
			sprc[lvl][i] = max(sprc[lvl - 1][i], sprc[lvl - 1][i + (len >> 1)]);
		}
	}
}

int dist(int x, int y) {
	if (x == y) {
		return 0;
	}

	if (ind[x] > ind[y]) {
		swap(x, y);
	}

	x = ind[x] + 1;
	y = ind[y];

	int len = y - x + 1;
	int lg = __lg(len);

	return max(sprc[lg][x], sprc[lg][y - (1 << lg) + 1]);
}

void unio(int u, int v, int w) {
	int p1 = par[u], p2 = par[v];
	if (p1 == p2) {
		if (endps[p1][0] != -1) {
			endps[p1][0] = -1;
			endps[p1][1] = -1;
			for (auto x : have[p1]) {
				path[x] = w;
			}
		}
		return;
	}

	if (sz(have[p1]) < sz(have[p2])) {
		swap(p1, p2);
		swap(u, v);
	}


	bool ipath = true;
	if (endps[p1][0] == -1 || endps[p1][1] == -1 || endps[p2][0] == -1 || endps[p2][1] == -1) {
		ipath = false;
	}
	else {
		ipath = false;
		for (int x = 0; x < 2; x++) {
			for (int y = 0; y < 2; y++) {
				if (endps[p1][x] == u && endps[p2][y] == v) {
					int e1 = endps[p1][!x];
					int e2 = endps[p2][!y];
					endps[p1][0] = e1;
					endps[p1][1] = e2;

					ipath = true;
					break;
				}
			}
			if (ipath) {
				break;
			}
		}
	}

	if (!ipath && endps[p1][0] != -1) {
		for (auto x : have[p1]) {
			path[x] = w;
		}
		endps[p1][0] = -1;
		endps[p1][1] = -1;
	}
	if (!ipath && endps[p2][0] != -1) {
		for (auto x : have[p2]) {
			path[x] = w;
		}
		endps[p2][0] = -1;
		endps[p2][1] = -1;
	}


	edge[have[p2][0]] = w;
	for (auto x : have[p2]) {
		have[p1].pb(x);
		par[x] = p1;
	}
	have[p2].clear();
}

void init(int N, int M,
          std::vector<int> U, std::vector<int> V, std::vector<int> W) {

	pair<int, int> edges[M];
	for (int e = 0; e < M; e++) {
		edges[e] = mp(W[e], e);
	}
	sort(edges + 0, edges + M);

	

	for (int v = 0; v < N; v++) {
		par[v] = v;
		have[v].pb(v);
		endps[v][0] = v;
		endps[v][1] = v;
	}


	for (int eind = 0; eind < M; eind++) {
		int e = edges[eind].se;
		int u = U[e];
		int v = V[e];
		int w = W[e];

		unio(u, v, w);
	}

	for (int v = 0; v < N - 1; v++) {
		unio(v, v + 1, inf);
	}

	for (int v = 0; v < N; v++) {
		if (!path[v]) {
			path[v] = inf;
		}
	}


	int p = par[0];
	for (int i = 0; i < N; i++) {
		ind[have[p][i]] = i;
	}

	cresprc(have[p]);
}

int getMinimumFuelCapacity(int X, int Y) {
	int ans = max(dist(X, Y), max(path[X], path[Y]));

	if (ans == inf) {
		return -1;
	}
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2772 KB Output is correct
5 Correct 2 ms 2800 KB Output is correct
6 Correct 2 ms 2772 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 3 ms 2772 KB Output is correct
9 Correct 64 ms 18660 KB Output is correct
10 Correct 85 ms 22224 KB Output is correct
11 Correct 90 ms 21796 KB Output is correct
12 Correct 85 ms 23004 KB Output is correct
13 Correct 79 ms 20188 KB Output is correct
14 Correct 74 ms 19148 KB Output is correct
15 Correct 149 ms 26600 KB Output is correct
16 Correct 136 ms 26068 KB Output is correct
17 Correct 143 ms 27240 KB Output is correct
18 Correct 122 ms 24364 KB Output is correct
19 Correct 60 ms 10788 KB Output is correct
20 Correct 137 ms 27456 KB Output is correct
21 Correct 147 ms 27268 KB Output is correct
22 Correct 151 ms 28616 KB Output is correct
23 Correct 138 ms 25900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 120 ms 20128 KB Output is correct
4 Correct 118 ms 24628 KB Output is correct
5 Correct 119 ms 24464 KB Output is correct
6 Correct 120 ms 24408 KB Output is correct
7 Correct 117 ms 24656 KB Output is correct
8 Correct 114 ms 23924 KB Output is correct
9 Correct 120 ms 24396 KB Output is correct
10 Correct 125 ms 23836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2772 KB Output is correct
5 Correct 2 ms 2800 KB Output is correct
6 Correct 2 ms 2772 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 3 ms 2772 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2800 KB Output is correct
11 Correct 2 ms 2796 KB Output is correct
12 Correct 2 ms 2772 KB Output is correct
13 Correct 2 ms 2772 KB Output is correct
14 Correct 2 ms 2772 KB Output is correct
15 Correct 2 ms 2796 KB Output is correct
16 Correct 2 ms 2800 KB Output is correct
17 Correct 2 ms 2800 KB Output is correct
18 Correct 3 ms 2820 KB Output is correct
19 Correct 2 ms 2772 KB Output is correct
20 Correct 2 ms 2772 KB Output is correct
21 Correct 2 ms 2756 KB Output is correct
22 Correct 2 ms 2772 KB Output is correct
23 Correct 3 ms 2772 KB Output is correct
24 Correct 2 ms 2836 KB Output is correct
25 Correct 2 ms 2796 KB Output is correct
26 Correct 3 ms 2900 KB Output is correct
27 Correct 2 ms 2772 KB Output is correct
28 Correct 2 ms 2900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 2 ms 2800 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 3 ms 2772 KB Output is correct
10 Correct 64 ms 18660 KB Output is correct
11 Correct 85 ms 22224 KB Output is correct
12 Correct 90 ms 21796 KB Output is correct
13 Correct 85 ms 23004 KB Output is correct
14 Correct 79 ms 20188 KB Output is correct
15 Correct 2 ms 2800 KB Output is correct
16 Correct 2 ms 2796 KB Output is correct
17 Correct 2 ms 2772 KB Output is correct
18 Correct 2 ms 2772 KB Output is correct
19 Correct 2 ms 2772 KB Output is correct
20 Correct 2 ms 2796 KB Output is correct
21 Correct 2 ms 2800 KB Output is correct
22 Correct 2 ms 2800 KB Output is correct
23 Correct 3 ms 2820 KB Output is correct
24 Correct 2 ms 2772 KB Output is correct
25 Correct 2 ms 2772 KB Output is correct
26 Correct 2 ms 2756 KB Output is correct
27 Correct 2 ms 2772 KB Output is correct
28 Correct 3 ms 2772 KB Output is correct
29 Correct 2 ms 2836 KB Output is correct
30 Correct 2 ms 2796 KB Output is correct
31 Correct 3 ms 2900 KB Output is correct
32 Correct 2 ms 2772 KB Output is correct
33 Correct 2 ms 2900 KB Output is correct
34 Correct 10 ms 5132 KB Output is correct
35 Correct 81 ms 22824 KB Output is correct
36 Correct 79 ms 22620 KB Output is correct
37 Correct 80 ms 22368 KB Output is correct
38 Correct 79 ms 21872 KB Output is correct
39 Correct 73 ms 21456 KB Output is correct
40 Correct 66 ms 19868 KB Output is correct
41 Correct 83 ms 23108 KB Output is correct
42 Correct 75 ms 22832 KB Output is correct
43 Correct 66 ms 20000 KB Output is correct
44 Correct 80 ms 21736 KB Output is correct
45 Correct 87 ms 20128 KB Output is correct
46 Correct 78 ms 22632 KB Output is correct
47 Correct 81 ms 21064 KB Output is correct
48 Correct 76 ms 20416 KB Output is correct
49 Correct 59 ms 10516 KB Output is correct
50 Correct 48 ms 9564 KB Output is correct
51 Correct 72 ms 17328 KB Output is correct
52 Correct 111 ms 25664 KB Output is correct
53 Correct 115 ms 24772 KB Output is correct
54 Correct 141 ms 28140 KB Output is correct
55 Correct 73 ms 19960 KB Output is correct
56 Correct 105 ms 24324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2772 KB Output is correct
5 Correct 2 ms 2800 KB Output is correct
6 Correct 2 ms 2772 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 3 ms 2772 KB Output is correct
9 Correct 64 ms 18660 KB Output is correct
10 Correct 85 ms 22224 KB Output is correct
11 Correct 90 ms 21796 KB Output is correct
12 Correct 85 ms 23004 KB Output is correct
13 Correct 79 ms 20188 KB Output is correct
14 Correct 74 ms 19148 KB Output is correct
15 Correct 149 ms 26600 KB Output is correct
16 Correct 136 ms 26068 KB Output is correct
17 Correct 143 ms 27240 KB Output is correct
18 Correct 122 ms 24364 KB Output is correct
19 Correct 120 ms 20128 KB Output is correct
20 Correct 118 ms 24628 KB Output is correct
21 Correct 119 ms 24464 KB Output is correct
22 Correct 120 ms 24408 KB Output is correct
23 Correct 117 ms 24656 KB Output is correct
24 Correct 114 ms 23924 KB Output is correct
25 Correct 120 ms 24396 KB Output is correct
26 Correct 125 ms 23836 KB Output is correct
27 Correct 2 ms 2800 KB Output is correct
28 Correct 2 ms 2796 KB Output is correct
29 Correct 2 ms 2772 KB Output is correct
30 Correct 2 ms 2772 KB Output is correct
31 Correct 2 ms 2772 KB Output is correct
32 Correct 2 ms 2796 KB Output is correct
33 Correct 2 ms 2800 KB Output is correct
34 Correct 2 ms 2800 KB Output is correct
35 Correct 3 ms 2820 KB Output is correct
36 Correct 10 ms 5132 KB Output is correct
37 Correct 81 ms 22824 KB Output is correct
38 Correct 79 ms 22620 KB Output is correct
39 Correct 80 ms 22368 KB Output is correct
40 Correct 79 ms 21872 KB Output is correct
41 Correct 73 ms 21456 KB Output is correct
42 Correct 66 ms 19868 KB Output is correct
43 Correct 83 ms 23108 KB Output is correct
44 Correct 75 ms 22832 KB Output is correct
45 Correct 66 ms 20000 KB Output is correct
46 Correct 80 ms 21736 KB Output is correct
47 Correct 15 ms 5464 KB Output is correct
48 Correct 140 ms 27620 KB Output is correct
49 Correct 137 ms 27336 KB Output is correct
50 Correct 132 ms 27204 KB Output is correct
51 Correct 141 ms 26896 KB Output is correct
52 Correct 128 ms 25500 KB Output is correct
53 Correct 107 ms 21048 KB Output is correct
54 Correct 140 ms 28652 KB Output is correct
55 Correct 135 ms 27848 KB Output is correct
56 Correct 122 ms 24860 KB Output is correct
57 Correct 141 ms 27236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 2 ms 2800 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 3 ms 2772 KB Output is correct
10 Correct 64 ms 18660 KB Output is correct
11 Correct 85 ms 22224 KB Output is correct
12 Correct 90 ms 21796 KB Output is correct
13 Correct 85 ms 23004 KB Output is correct
14 Correct 79 ms 20188 KB Output is correct
15 Correct 74 ms 19148 KB Output is correct
16 Correct 149 ms 26600 KB Output is correct
17 Correct 136 ms 26068 KB Output is correct
18 Correct 143 ms 27240 KB Output is correct
19 Correct 122 ms 24364 KB Output is correct
20 Correct 120 ms 20128 KB Output is correct
21 Correct 118 ms 24628 KB Output is correct
22 Correct 119 ms 24464 KB Output is correct
23 Correct 120 ms 24408 KB Output is correct
24 Correct 117 ms 24656 KB Output is correct
25 Correct 114 ms 23924 KB Output is correct
26 Correct 120 ms 24396 KB Output is correct
27 Correct 125 ms 23836 KB Output is correct
28 Correct 2 ms 2800 KB Output is correct
29 Correct 2 ms 2796 KB Output is correct
30 Correct 2 ms 2772 KB Output is correct
31 Correct 2 ms 2772 KB Output is correct
32 Correct 2 ms 2772 KB Output is correct
33 Correct 2 ms 2796 KB Output is correct
34 Correct 2 ms 2800 KB Output is correct
35 Correct 2 ms 2800 KB Output is correct
36 Correct 3 ms 2820 KB Output is correct
37 Correct 10 ms 5132 KB Output is correct
38 Correct 81 ms 22824 KB Output is correct
39 Correct 79 ms 22620 KB Output is correct
40 Correct 80 ms 22368 KB Output is correct
41 Correct 79 ms 21872 KB Output is correct
42 Correct 73 ms 21456 KB Output is correct
43 Correct 66 ms 19868 KB Output is correct
44 Correct 83 ms 23108 KB Output is correct
45 Correct 75 ms 22832 KB Output is correct
46 Correct 66 ms 20000 KB Output is correct
47 Correct 80 ms 21736 KB Output is correct
48 Correct 15 ms 5464 KB Output is correct
49 Correct 140 ms 27620 KB Output is correct
50 Correct 137 ms 27336 KB Output is correct
51 Correct 132 ms 27204 KB Output is correct
52 Correct 141 ms 26896 KB Output is correct
53 Correct 128 ms 25500 KB Output is correct
54 Correct 107 ms 21048 KB Output is correct
55 Correct 140 ms 28652 KB Output is correct
56 Correct 135 ms 27848 KB Output is correct
57 Correct 122 ms 24860 KB Output is correct
58 Correct 141 ms 27236 KB Output is correct
59 Correct 60 ms 10788 KB Output is correct
60 Correct 137 ms 27456 KB Output is correct
61 Correct 147 ms 27268 KB Output is correct
62 Correct 151 ms 28616 KB Output is correct
63 Correct 138 ms 25900 KB Output is correct
64 Correct 2 ms 2772 KB Output is correct
65 Correct 2 ms 2772 KB Output is correct
66 Correct 2 ms 2756 KB Output is correct
67 Correct 2 ms 2772 KB Output is correct
68 Correct 3 ms 2772 KB Output is correct
69 Correct 2 ms 2836 KB Output is correct
70 Correct 2 ms 2796 KB Output is correct
71 Correct 3 ms 2900 KB Output is correct
72 Correct 2 ms 2772 KB Output is correct
73 Correct 2 ms 2900 KB Output is correct
74 Correct 87 ms 20128 KB Output is correct
75 Correct 78 ms 22632 KB Output is correct
76 Correct 81 ms 21064 KB Output is correct
77 Correct 76 ms 20416 KB Output is correct
78 Correct 59 ms 10516 KB Output is correct
79 Correct 48 ms 9564 KB Output is correct
80 Correct 72 ms 17328 KB Output is correct
81 Correct 111 ms 25664 KB Output is correct
82 Correct 115 ms 24772 KB Output is correct
83 Correct 141 ms 28140 KB Output is correct
84 Correct 73 ms 19960 KB Output is correct
85 Correct 105 ms 24324 KB Output is correct
86 Correct 41 ms 10196 KB Output is correct
87 Correct 137 ms 27084 KB Output is correct
88 Correct 135 ms 27084 KB Output is correct
89 Correct 140 ms 24184 KB Output is correct
90 Correct 118 ms 14924 KB Output is correct
91 Correct 112 ms 15988 KB Output is correct
92 Correct 122 ms 22120 KB Output is correct
93 Correct 159 ms 29952 KB Output is correct
94 Correct 164 ms 29520 KB Output is correct
95 Correct 199 ms 32072 KB Output is correct
96 Correct 125 ms 24932 KB Output is correct
97 Correct 158 ms 27464 KB Output is correct