Submission #1004571

# Submission time Handle Problem Language Result Execution time Memory
1004571 2024-06-21T09:55:59 Z Nelt Sirni (COCI17_sirni) C++17
140 / 140
2460 ms 535444 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll int
#define endl "\n"
 
using namespace std;
using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll N = 1e5 + 5, MAX = 1e7 + 5;
struct Dsu
{
    ll n, ans;
    vector<ll> dsu;
    Dsu(ll sz = 1)
    {
        n = sz;
        ans = n;
        dsu.resize(n + 1, 0);
        init();
    }
    void init()
    {
        for (ll i = 0; i <= n; i++)
            dsu[i] = -1;
    }
    bool Union(ll x, ll y)
    {
        x = repr(x), y = repr(y);
        if (x == y)
            return false;
        if (dsu[x] < dsu[y])
            swap(x, y);
        ans--;
        dsu[y] += dsu[x];
        dsu[x] = y;
        return true;
    }
    ll repr(ll x)
    {
        if (dsu[x] < 0)
            return x;
        return dsu[x] = repr(dsu[x]);
    }
    ll query()
    {
        return ans;
    }
    ll size(ll x)
    {
        return -dsu[repr(x)];
    }
};
vector<pair<ll, ll>> e[MAX];
ll best[N];
ll f(ll x, ll y)
{
    return min(x % y, y % x);
}
void solve()
{
    ll n;
    cin >> n;
    ll p[n];
    for (ll &i : p) cin >> i;
    long long ans = 0;
    {
        set<ll> s;
        for (ll i : p) s.insert(i);
        n = 0;
        for (ll i : s) p[n++] = i;
    }
    Dsu dsu(n);
    for (ll i = 0; i < n - 1; i++) if (f(p[i], p[i + 1]) < MAX) e[f(p[i], p[i + 1])].push_back(make_pair(i, i + 1));
	for (ll i = 0; i < n; i++)
        for (ll w = p[i], pos; w <= p[n - 1]; w += p[i]) 
    {
        pos = lower_bound(p, p + n, w) - p;
        if (pos < n and p[pos] < w + p[i] and p[pos] - w < MAX) e[p[pos] - w].push_back(make_pair(pos, i));
    }
    // for (ll i = 0; i < n; i++) for (ll j = 0; j < n; j++) e[f(p[i], p[j])].push_back(make_pair(i, j));
    for (ll i = 0; i <= p[n - 1]; i++) for (auto [a, b] : e[i]) ans += dsu.Union(a, b) * i;
    cout << ans << endl;
}
 
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll t = 1;
    // precomp();
    // cin >> t;
    for (ll cs = 1; cs <= t; cs++)
        solve();
    // cerr << "\nTime elapsed: " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\n";
}
# Verdict Execution time Memory Grader output
1 Correct 109 ms 235344 KB Output is correct
2 Correct 137 ms 237644 KB Output is correct
3 Correct 112 ms 235368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 235344 KB Output is correct
2 Correct 436 ms 235856 KB Output is correct
3 Correct 112 ms 235344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 108 ms 235348 KB Output is correct
2 Correct 110 ms 235132 KB Output is correct
3 Correct 108 ms 235352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 197 ms 246552 KB Output is correct
2 Correct 404 ms 272340 KB Output is correct
3 Correct 231 ms 250756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 105 ms 237136 KB Output is correct
2 Correct 171 ms 257488 KB Output is correct
3 Correct 195 ms 246588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 298 ms 258740 KB Output is correct
2 Correct 506 ms 286160 KB Output is correct
3 Correct 210 ms 248560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 126 ms 239196 KB Output is correct
2 Correct 443 ms 281632 KB Output is correct
3 Correct 248 ms 248796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 232 ms 248816 KB Output is correct
2 Correct 1659 ms 444984 KB Output is correct
3 Correct 259 ms 250908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 221 ms 249168 KB Output is correct
2 Correct 2460 ms 535444 KB Output is correct
3 Correct 354 ms 255028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 124 ms 237904 KB Output is correct
2 Correct 2262 ms 530696 KB Output is correct
3 Correct 240 ms 251028 KB Output is correct