Submission #1091730

#TimeUsernameProblemLanguageResultExecution timeMemory
1091730JeffLegendPowerSirni (COCI17_sirni)C++17
140 / 140
2317 ms477440 KiB
// https://oj.uz/problem/view/COCI17_sirni #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #define ll long long #define plli pair<ll, int> #define pll pair<ll, ll> #define pii pair<int, int> // Usage: FOR(i, 0, N) {...} #define FOR(i, start, end) for(int i = start; i < end; i++) void setIO(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); } struct comp { bool operator() (plli a, plli b) { return a < b; } }; typedef tree<plli, null_type, comp, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset; // Segtree start const int Nmax = 1e5; // limit for array size int N; // array size int t[2 * Nmax]; int oper(int a, int b) { return a + b; } void build() { // build the tree for (int i = N - 1; i > 0; --i) t[i] = oper(t[i<<1], t[i<<1|1]); } void modify(int p, int value) { // set value at position p for (t[p += N] = value; p > 1; p >>= 1) t[p>>1] = oper(t[p], t[p^1]); } int query(int l, int r) { // on interval [l, r) int res = 0; for (l += N, r += N; l < r; l >>= 1, r >>= 1) { if (l&1) res = oper(res, t[l++]); if (r&1) res = oper(res, t[--r]); } return res; } // Segtree end class DSU { private: vector<int> parents; vector<int> sizes; public: DSU(int size) : parents(size), sizes(size, 1) { for (int i = 0; i < size; i++) { parents[i] = i; } } /** @return the "representative" node in x's component */ int find(int x) { return parents[x] == x ? x : (parents[x] = find(parents[x])); } /** @return whether the merge changed connectivity */ bool unite(int x, int y) { int x_root = find(x); int y_root = find(y); if (x_root == y_root) { return false; } if (sizes[x_root] < sizes[y_root]) { swap(x_root, y_root); } sizes[x_root] += sizes[y_root]; parents[y_root] = x_root; return true; } /** @return whether x and y are in the same connected component */ bool connected(int x, int y) { return find(x) == find(y); } }; int main() { // Comment out for interactive problems ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; int M = 1e7 + 1; set<int> Pset; for (int i = 0; i < N; i++) { int P; cin >> P; Pset.insert(P); } int n = Pset.size(); int P[n]; int idx = 0; for (auto itr = Pset.begin(); itr != Pset.end(); itr++) P[idx++] = *itr; vector<vector<pii>> edges; edges.reserve(M); for (int i = 0; i < n; i++) { int j = i + 1; for (int p = P[i]; p < M; p += P[i]) { while (j < n && P[j] < p) j++; if (j < n) edges[P[j] % P[i]].push_back({i, j}); } } DSU dsu(N); ll candies = 0; for (int m = 0; m < M; m++) { for (pii edge : edges[m]) { if (dsu.connected(edge.first, edge.second)) continue; dsu.unite(edge.first, edge.second); candies += m; } } cout << candies; }

Compilation message (stderr)

sirni.cpp: In function 'void setIO(std::string)':
sirni.cpp:18:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |  freopen((s + ".in").c_str(), "r", stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sirni.cpp:19:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |  freopen((s + ".out").c_str(), "w", stdout);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...