Submission #568047

# Submission time Handle Problem Language Result Execution time Memory
568047 2022-05-24T14:33:17 Z Zanite Swapping Cities (APIO20_swap) C++17
100 / 100
1190 ms 50868 KB
// You can't change other people; you can only change yourself.

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#include "swap.h"

// Pragmas
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

// Namespaces
#define yume using
#define wo namespace
#define kanaeyo std
#define nazotte __gnu_pbds
yume wo kanaeyo;
yume wo nazotte;

// Data types
using i8	= __int128;
using ll	= long long;
using si	= short int;
using ld	= long double;

// Pairs
using pi8	= pair<i8, i8>;
using pll	= pair<ll, ll>;
using pii	= pair<int, int>;
using psi	= pair<si, si>;
using pld	= pair<ld, ld>;
#define fi first
#define se second

// PBDS
template<typename T>
using ordered_set	= tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

// Quick macro functions
#define sqr(x) ((x)*(x))
#define pow2(x) (1ll << (x))
#define debug(x) cout << #x << " = " << (x) << '\n'
#define debugV(x, a) cout << #x << "[" << (a) << "] = " << (x[a]) << '\n'

// Check min and max
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
 
// Modular arithmetic
template<typename T> void maddto(T &a, T b, T mod) {a += b; a %= mod;}
template<typename T> void msubto(T &a, T b, T mod) {a -= b; while (a < 0) a += mod; a %= mod;}
template<typename T> void mmulto(T &a, T b, T mod) {a *= b; a %= mod;}
 
template<typename T> T madd(T a, T b, T mod) {a += b; a %= mod; return a;}
template<typename T> T msub(T a, T b, T mod) {a -= b; while (a < 0) a += mod; return a;}
template<typename T> T mmul(T a, T b, T mod) {a *= b; a %= mod; return a;}

// Constants
const ll ModA	= 998244353;
const ll ModC	= 1e9 + 7;
const ll INF	= 1e18;
const ll iINF	= 1e9;
const ld EPS	= 1e-9;
const ld iEPS	= 1e-6;

struct OnlineDSU {
	int n;
	vector<vector<pii>> par, sz, degree, maxdegree, edgecount;

	OnlineDSU(int _n) {
		n = _n;
		for (int i = 0; i < n; i++) {
			par.push_back({{0, i}});
		}
		sz.assign(n, {{0, 1}});
		degree.assign(n, {{0, 0}});
		maxdegree.assign(n, {{0, 0}});
		edgecount.assign(n, {{0, 0}});
	}

	void insert(vector<pii> &v, int val, int t) {
		if (v.back().se == val) {return;}
		if (v.back().fi == t) {
			v.back().se = val;
		} else {
			v.push_back({t, val});
		}
	}

	int query(vector<pii> &v, int t) {
		pii search = {t+1, -iINF};
		auto it = upper_bound(v.begin(), v.end(), search);
		it--;
		return it->second;
	}

	int parent_latest(int x) {return par[x].back().se;}
	int size_latest(int x) {return sz[x].back().se;}
	int degree_latest(int x) {return degree[x].back().se;}
	int maxdegree_latest(int x) {return maxdegree[x].back().se;}
	int edgecount_latest(int x) {return edgecount[x].back().se;}

	int get_degree(int x, int t) {return query(degree[x], t);}
	int get_maxdegree(int x, int t) {return query(maxdegree[x], t);}
	int get_edgecount(int x, int t) {return query(edgecount[x], t);}
	int get_size(int x, int t) {return query(sz[x], t);}

	int rep_latest(int x, int t) {
		if (x == parent_latest(x)) {return x;}
		insert(par[x], rep_latest(parent_latest(x), t), t);
		return parent_latest(x);
	}

	int rep(int x, int t) {
		int p = query(par[x], t);
		if (x == p) {return x;}
		return rep(p, t);
	}

	void join_latest(int u, int v, int t) {
		insert(degree[u], degree_latest(u)+1, t);
		insert(degree[v], degree_latest(v)+1, t);

		int x = rep_latest(u, t), y = rep_latest(v, t);
		if (x != y) {
			if (size_latest(x) < size_latest(y)) {swap(x, y);}

			insert(par[y], x, t);
			insert(sz[x], size_latest(x) + size_latest(y), t);

			int mx = maxdegree_latest(x), my = maxdegree_latest(y);
			insert(maxdegree[x], max(max(mx, my), max(degree_latest(u), degree_latest(v))), t);

			int ex = edgecount_latest(x), ey = edgecount_latest(y);
			insert(edgecount[x], ex+ey+1, t);
		} else {
			insert(maxdegree[x], max(maxdegree_latest(x), max(degree_latest(u), degree_latest(v))), t);
			int e = edgecount_latest(x);
			insert(edgecount[x], e+1, t);
		}
	}

	bool check(int u, int v, int t) {
		int x = rep(u, t), y = rep(v, t);
		return (x == y);
	}
};

void print(vector<vector<pii>> &v) {
	for (int i = 0; i < v.size(); i++) {
		cout << i << ": ";
		for (auto j : v[i]) {
			cout << "{" << j.fi << ", " << j.se << "} ";
		}
		cout << '\n';
	}
	cout << '\n';
}

using Edge	= pair<int, pii>;

OnlineDSU *D;

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
	vector<Edge> E;
	for (int i = 0; i < M; i++) {
		E.push_back({W[i], {U[i], V[i]}});
	}
	sort(E.begin(), E.end());

	D = new OnlineDSU(N);
	for (auto x : E) {
		D->join_latest(x.se.fi, x.se.se, x.fi);
		//printf("%d: %d %d\n", x.fi, x.se.fi, x.se.se);
	}

	/*
	cout << "par\n";
	print(D->par);
	cout << "sz\n";
	print(D->sz);
	cout << "degree\n";
	print(D->degree);
	cout << "maxdegree\n";
	print(D->maxdegree);
	cout << "edgecount\n";
	print(D->edgecount);
	*/
}

const int MAX = 1e9;

int getMinimumFuelCapacity(int X, int Y) {
	int L = 1, R = MAX, ans = -1;
	while (L <= R) {
		int M = (L + R) >> 1;

		bool pos = 1;
		if (!D->check(X, Y, M)) {
			pos = 0;
		} else {
			int R = D->rep(X, M);
			if (D->get_maxdegree(R, M) > 2 || (D->get_size(R, M) == D->get_edgecount(R, M))) {
				pos = 1;
			} else {
				pos = 0;
			}
		}

		if (pos) {
			R = M - 1;
			ans = M;
		} else {
			L = M + 1;
		}
	}
	return ans;
}

//int main() {}

Compilation message

swap.cpp: In function 'void print(std::vector<std::vector<std::pair<int, int> > >&)':
swap.cpp:152:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  152 |  for (int i = 0; i < v.size(); i++) {
      |                  ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 2 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 179 ms 29880 KB Output is correct
10 Correct 236 ms 36532 KB Output is correct
11 Correct 214 ms 35932 KB Output is correct
12 Correct 221 ms 37992 KB Output is correct
13 Correct 171 ms 35724 KB Output is correct
14 Correct 232 ms 29888 KB Output is correct
15 Correct 1041 ms 38312 KB Output is correct
16 Correct 971 ms 37312 KB Output is correct
17 Correct 916 ms 39476 KB Output is correct
18 Correct 647 ms 36928 KB Output is correct
19 Correct 546 ms 9556 KB Output is correct
20 Correct 901 ms 38740 KB Output is correct
21 Correct 918 ms 37572 KB Output is correct
22 Correct 1000 ms 39928 KB Output is correct
23 Correct 685 ms 37332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 532 ms 36368 KB Output is correct
4 Correct 513 ms 37960 KB Output is correct
5 Correct 533 ms 36900 KB Output is correct
6 Correct 513 ms 37940 KB Output is correct
7 Correct 562 ms 37424 KB Output is correct
8 Correct 564 ms 36108 KB Output is correct
9 Correct 511 ms 37208 KB Output is correct
10 Correct 513 ms 35780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 2 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 2 ms 596 KB Output is correct
12 Correct 2 ms 696 KB Output is correct
13 Correct 2 ms 596 KB Output is correct
14 Correct 2 ms 596 KB Output is correct
15 Correct 1 ms 580 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
17 Correct 1 ms 596 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 596 KB Output is correct
21 Correct 2 ms 596 KB Output is correct
22 Correct 2 ms 444 KB Output is correct
23 Correct 1 ms 596 KB Output is correct
24 Correct 2 ms 696 KB Output is correct
25 Correct 2 ms 724 KB Output is correct
26 Correct 2 ms 724 KB Output is correct
27 Correct 1 ms 596 KB Output is correct
28 Correct 3 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 2 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 2 ms 596 KB Output is correct
10 Correct 179 ms 29880 KB Output is correct
11 Correct 236 ms 36532 KB Output is correct
12 Correct 214 ms 35932 KB Output is correct
13 Correct 221 ms 37992 KB Output is correct
14 Correct 171 ms 35724 KB Output is correct
15 Correct 1 ms 596 KB Output is correct
16 Correct 2 ms 596 KB Output is correct
17 Correct 2 ms 696 KB Output is correct
18 Correct 2 ms 596 KB Output is correct
19 Correct 2 ms 596 KB Output is correct
20 Correct 1 ms 580 KB Output is correct
21 Correct 1 ms 596 KB Output is correct
22 Correct 1 ms 596 KB Output is correct
23 Correct 1 ms 596 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 596 KB Output is correct
26 Correct 2 ms 596 KB Output is correct
27 Correct 2 ms 444 KB Output is correct
28 Correct 1 ms 596 KB Output is correct
29 Correct 2 ms 696 KB Output is correct
30 Correct 2 ms 724 KB Output is correct
31 Correct 2 ms 724 KB Output is correct
32 Correct 1 ms 596 KB Output is correct
33 Correct 3 ms 724 KB Output is correct
34 Correct 17 ms 5328 KB Output is correct
35 Correct 217 ms 37884 KB Output is correct
36 Correct 215 ms 37980 KB Output is correct
37 Correct 238 ms 37976 KB Output is correct
38 Correct 202 ms 37520 KB Output is correct
39 Correct 205 ms 37024 KB Output is correct
40 Correct 183 ms 33920 KB Output is correct
41 Correct 209 ms 37936 KB Output is correct
42 Correct 226 ms 37968 KB Output is correct
43 Correct 147 ms 35228 KB Output is correct
44 Correct 194 ms 37316 KB Output is correct
45 Correct 243 ms 37944 KB Output is correct
46 Correct 209 ms 39828 KB Output is correct
47 Correct 231 ms 39624 KB Output is correct
48 Correct 190 ms 39840 KB Output is correct
49 Correct 74 ms 15180 KB Output is correct
50 Correct 66 ms 13696 KB Output is correct
51 Correct 162 ms 31564 KB Output is correct
52 Correct 287 ms 46232 KB Output is correct
53 Correct 330 ms 47448 KB Output is correct
54 Correct 337 ms 49268 KB Output is correct
55 Correct 164 ms 37532 KB Output is correct
56 Correct 259 ms 45896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 2 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 179 ms 29880 KB Output is correct
10 Correct 236 ms 36532 KB Output is correct
11 Correct 214 ms 35932 KB Output is correct
12 Correct 221 ms 37992 KB Output is correct
13 Correct 171 ms 35724 KB Output is correct
14 Correct 232 ms 29888 KB Output is correct
15 Correct 1041 ms 38312 KB Output is correct
16 Correct 971 ms 37312 KB Output is correct
17 Correct 916 ms 39476 KB Output is correct
18 Correct 647 ms 36928 KB Output is correct
19 Correct 532 ms 36368 KB Output is correct
20 Correct 513 ms 37960 KB Output is correct
21 Correct 533 ms 36900 KB Output is correct
22 Correct 513 ms 37940 KB Output is correct
23 Correct 562 ms 37424 KB Output is correct
24 Correct 564 ms 36108 KB Output is correct
25 Correct 511 ms 37208 KB Output is correct
26 Correct 513 ms 35780 KB Output is correct
27 Correct 1 ms 596 KB Output is correct
28 Correct 2 ms 596 KB Output is correct
29 Correct 2 ms 696 KB Output is correct
30 Correct 2 ms 596 KB Output is correct
31 Correct 2 ms 596 KB Output is correct
32 Correct 1 ms 580 KB Output is correct
33 Correct 1 ms 596 KB Output is correct
34 Correct 1 ms 596 KB Output is correct
35 Correct 1 ms 596 KB Output is correct
36 Correct 17 ms 5328 KB Output is correct
37 Correct 217 ms 37884 KB Output is correct
38 Correct 215 ms 37980 KB Output is correct
39 Correct 238 ms 37976 KB Output is correct
40 Correct 202 ms 37520 KB Output is correct
41 Correct 205 ms 37024 KB Output is correct
42 Correct 183 ms 33920 KB Output is correct
43 Correct 209 ms 37936 KB Output is correct
44 Correct 226 ms 37968 KB Output is correct
45 Correct 147 ms 35228 KB Output is correct
46 Correct 194 ms 37316 KB Output is correct
47 Correct 60 ms 5024 KB Output is correct
48 Correct 950 ms 39500 KB Output is correct
49 Correct 944 ms 39568 KB Output is correct
50 Correct 920 ms 39572 KB Output is correct
51 Correct 891 ms 39192 KB Output is correct
52 Correct 764 ms 36864 KB Output is correct
53 Correct 592 ms 27544 KB Output is correct
54 Correct 919 ms 39924 KB Output is correct
55 Correct 1022 ms 39500 KB Output is correct
56 Correct 447 ms 37196 KB Output is correct
57 Correct 701 ms 39316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 2 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 2 ms 596 KB Output is correct
10 Correct 179 ms 29880 KB Output is correct
11 Correct 236 ms 36532 KB Output is correct
12 Correct 214 ms 35932 KB Output is correct
13 Correct 221 ms 37992 KB Output is correct
14 Correct 171 ms 35724 KB Output is correct
15 Correct 232 ms 29888 KB Output is correct
16 Correct 1041 ms 38312 KB Output is correct
17 Correct 971 ms 37312 KB Output is correct
18 Correct 916 ms 39476 KB Output is correct
19 Correct 647 ms 36928 KB Output is correct
20 Correct 532 ms 36368 KB Output is correct
21 Correct 513 ms 37960 KB Output is correct
22 Correct 533 ms 36900 KB Output is correct
23 Correct 513 ms 37940 KB Output is correct
24 Correct 562 ms 37424 KB Output is correct
25 Correct 564 ms 36108 KB Output is correct
26 Correct 511 ms 37208 KB Output is correct
27 Correct 513 ms 35780 KB Output is correct
28 Correct 1 ms 596 KB Output is correct
29 Correct 2 ms 596 KB Output is correct
30 Correct 2 ms 696 KB Output is correct
31 Correct 2 ms 596 KB Output is correct
32 Correct 2 ms 596 KB Output is correct
33 Correct 1 ms 580 KB Output is correct
34 Correct 1 ms 596 KB Output is correct
35 Correct 1 ms 596 KB Output is correct
36 Correct 1 ms 596 KB Output is correct
37 Correct 17 ms 5328 KB Output is correct
38 Correct 217 ms 37884 KB Output is correct
39 Correct 215 ms 37980 KB Output is correct
40 Correct 238 ms 37976 KB Output is correct
41 Correct 202 ms 37520 KB Output is correct
42 Correct 205 ms 37024 KB Output is correct
43 Correct 183 ms 33920 KB Output is correct
44 Correct 209 ms 37936 KB Output is correct
45 Correct 226 ms 37968 KB Output is correct
46 Correct 147 ms 35228 KB Output is correct
47 Correct 194 ms 37316 KB Output is correct
48 Correct 60 ms 5024 KB Output is correct
49 Correct 950 ms 39500 KB Output is correct
50 Correct 944 ms 39568 KB Output is correct
51 Correct 920 ms 39572 KB Output is correct
52 Correct 891 ms 39192 KB Output is correct
53 Correct 764 ms 36864 KB Output is correct
54 Correct 592 ms 27544 KB Output is correct
55 Correct 919 ms 39924 KB Output is correct
56 Correct 1022 ms 39500 KB Output is correct
57 Correct 447 ms 37196 KB Output is correct
58 Correct 701 ms 39316 KB Output is correct
59 Correct 546 ms 9556 KB Output is correct
60 Correct 901 ms 38740 KB Output is correct
61 Correct 918 ms 37572 KB Output is correct
62 Correct 1000 ms 39928 KB Output is correct
63 Correct 685 ms 37332 KB Output is correct
64 Correct 1 ms 468 KB Output is correct
65 Correct 1 ms 596 KB Output is correct
66 Correct 2 ms 596 KB Output is correct
67 Correct 2 ms 444 KB Output is correct
68 Correct 1 ms 596 KB Output is correct
69 Correct 2 ms 696 KB Output is correct
70 Correct 2 ms 724 KB Output is correct
71 Correct 2 ms 724 KB Output is correct
72 Correct 1 ms 596 KB Output is correct
73 Correct 3 ms 724 KB Output is correct
74 Correct 243 ms 37944 KB Output is correct
75 Correct 209 ms 39828 KB Output is correct
76 Correct 231 ms 39624 KB Output is correct
77 Correct 190 ms 39840 KB Output is correct
78 Correct 74 ms 15180 KB Output is correct
79 Correct 66 ms 13696 KB Output is correct
80 Correct 162 ms 31564 KB Output is correct
81 Correct 287 ms 46232 KB Output is correct
82 Correct 330 ms 47448 KB Output is correct
83 Correct 337 ms 49268 KB Output is correct
84 Correct 164 ms 37532 KB Output is correct
85 Correct 259 ms 45896 KB Output is correct
86 Correct 136 ms 15328 KB Output is correct
87 Correct 856 ms 42576 KB Output is correct
88 Correct 902 ms 42596 KB Output is correct
89 Correct 620 ms 40128 KB Output is correct
90 Correct 410 ms 17600 KB Output is correct
91 Correct 422 ms 19592 KB Output is correct
92 Correct 540 ms 33324 KB Output is correct
93 Correct 903 ms 47584 KB Output is correct
94 Correct 853 ms 48816 KB Output is correct
95 Correct 1190 ms 50868 KB Output is correct
96 Correct 487 ms 40380 KB Output is correct
97 Correct 645 ms 44864 KB Output is correct