Submission #1004421

# Submission time Handle Problem Language Result Execution time Memory
1004421 2024-06-21T08:47:53 Z Nelt Sirni (COCI17_sirni) C++17
84 / 140
862 ms 786432 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#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)];
    }
};
ll freq[MAX], ind[MAX];
vector<ll> consider[MAX];
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;
    ll ans = 0;
    {
        set<ll> s;
        for (ll i : p) s.insert(i);
        n = 0;
        for (ll i : s) p[n++] = i;
    }
    for (ll i = 0; i <= p[n - 1]; i++) ind[i] = -1;
    for (ll i : p) freq[i]++;
    for (ll i = 0; i < n; i++) ind[p[i]] = i;
    for (ll i = 1; i <= p[n - 1]; i++) freq[i] += freq[i - 1];
    for (ll i = 0; i < n; i++)
        for (ll j = p[i]; j <= p[n - 1]; j += p[i]) consider[j].push_back(i);
    Dsu dsu(n);
    for (ll i = 0; i < n - 1; i++) e[f(p[i], p[i + 1])].push_back(make_pair(i, i + 1));
    for (ll w = p[n - 1]; w > 0; w--)
    {
        ll pos = lower_bound(p, p + n, w) - p;
        if (pos < n)
        for (ll i : consider[w]) e[f(p[pos], p[i])].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 330 ms 626828 KB Output is correct
2 Correct 729 ms 751700 KB Output is correct
3 Correct 340 ms 627348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 162 ms 470612 KB Output is correct
2 Runtime error 357 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 332 ms 626920 KB Output is correct
2 Correct 361 ms 626516 KB Output is correct
3 Correct 338 ms 627024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 339 ms 529004 KB Output is correct
2 Correct 610 ms 623636 KB Output is correct
3 Correct 486 ms 575368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 226 ms 495084 KB Output is correct
2 Correct 400 ms 564940 KB Output is correct
3 Correct 314 ms 518616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 489 ms 567880 KB Output is correct
2 Correct 781 ms 686188 KB Output is correct
3 Correct 482 ms 562068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 233 ms 487196 KB Output is correct
2 Correct 862 ms 690552 KB Output is correct
3 Correct 505 ms 569308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 683 ms 693436 KB Output is correct
2 Runtime error 428 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 728 ms 716100 KB Output is correct
2 Runtime error 417 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 400 ms 637612 KB Output is correct
2 Runtime error 763 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -