Submission #872676

# Submission time Handle Problem Language Result Execution time Memory
872676 2023-11-13T14:17:07 Z ace_in_the_hole Sirni (COCI17_sirni) C++17
140 / 140
3952 ms 580540 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

#include<bits/stdc++.h>
using namespace std;

#define all(v) (v).begin(), (v).end()
#define cst(T) const T&

template<class A, class B> bool umin(A& var, cst(B) val) {
	return (val < var) ? (var = val, true) : false;
}
template<class A, class B> bool umax(A& var, cst(B) val) {
	return (var < val) ? (var = val, true) : false;
}

typedef long long Int;
typedef long double Real;
const int MOD = 2004010501;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class X, class Y> Int random(const X& l, const Y& r) {
	return uniform_int_distribution<Int>(l,r)(rng);
}
#define DBG(x) cerr << #x << " = " << x << ' ';
#define DBGn(x) cerr << #x << " = " << x << '\n';

const int N = 1e5 + 50;
const int V = 1e7 + 10;
const int INF = 1e9;

int v2pos[V];

class DisjointSet {
private :
	int size; int* label = nullptr;
public :
	DisjointSet(int size) : size(size) {
		if (label != nullptr) delete label;
		label = new int[size + 1];
		for (int i = 0; i <= size; i++) label[i] = -1;
	}
  	
	int find_root(int u) {
		if (label[u] < 0) return u;
		return label[u] = find_root(label[u]);
	}
  
	bool merge_set(int u, int v) {
    	u = find_root(u), v = find_root(v);
		if (u == v) return false;
		if (-(label[u]) < -(label[v])) swap(u,v);
		return label[u] += label[v], label[v] = u, true;
	}
  	
  	bool same_set(int a, int b) { 
		return find_root(a) == find_root(b);
	}
}; 

struct Edge {
	int u,v;
};
vector<Edge> edges[V];

void solve(int testID) {
	// DBGn(testID);
	int n;
	cin >> n;
	vector<int> p(n);
	for (auto &pi : p) cin >> pi;
	sort(all(p));
	p.resize(unique(all(p)) - begin(p));
	n = int(size(p));
	for (int i = 0; i < n; i++) v2pos[p[i]] = i;
	set<int> values(all(p));
	auto find_edges = [&] (int a, int threshold = 0) {
		if (!values.empty()) {
			int mq = max(0, threshold / a) + 1;
			do {
				auto it = values.lower_bound(mq * a);
				if (it == end(values)) break;
				edges[(*it) % a].push_back({a,*it});
				mq = (*it) / a + 1;
			} while (1);
		}
	};
	for (auto p_i : p) {
		values.erase(p_i);
		find_edges(p_i);
	}
	DisjointSet dsu(n);
	Int ans = 0;
	for (int w = 0; w < p.back(); w++) {
		for (auto [u,v] : edges[w]) {
			u = v2pos[u], v = v2pos[v];
			if (dsu.merge_set(u,v)) ans += w;
		}
	}
	cout << ans;
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	#define task "WF"
	if (fopen(task".inp", "r")) {
		freopen(task".inp", "r", stdin);
		freopen(task".out", "w", stdout);
	}
	int numTests = 1;
	// cin >> numTests;
	for (int i = 1; i <= numTests; i++) solve(i);
}

Compilation message

sirni.cpp: In function 'int main()':
sirni.cpp:110:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  110 |   freopen(task".inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sirni.cpp:111:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  111 |   freopen(task".out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 95 ms 273744 KB Output is correct
2 Correct 99 ms 276052 KB Output is correct
3 Correct 96 ms 273856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 65 ms 236488 KB Output is correct
2 Correct 92 ms 274264 KB Output is correct
3 Correct 89 ms 273744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 92 ms 273756 KB Output is correct
2 Correct 88 ms 267276 KB Output is correct
3 Correct 107 ms 273748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 240 ms 254072 KB Output is correct
2 Correct 605 ms 285104 KB Output is correct
3 Correct 262 ms 259296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 84 ms 242848 KB Output is correct
2 Correct 201 ms 263000 KB Output is correct
3 Correct 228 ms 252052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 444 ms 265696 KB Output is correct
2 Correct 814 ms 299792 KB Output is correct
3 Correct 287 ms 258680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 115 ms 241260 KB Output is correct
2 Correct 647 ms 291384 KB Output is correct
3 Correct 255 ms 257732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 306 ms 290560 KB Output is correct
2 Correct 2765 ms 494720 KB Output is correct
3 Correct 339 ms 293440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 297 ms 290056 KB Output is correct
2 Correct 3952 ms 580540 KB Output is correct
3 Correct 399 ms 297412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 118 ms 277080 KB Output is correct
2 Correct 3566 ms 572380 KB Output is correct
3 Correct 296 ms 260380 KB Output is correct