Submission #986002

# Submission time Handle Problem Language Result Execution time Memory
986002 2024-05-19T15:09:39 Z LOLOLO Sirni (COCI17_sirni) C++17
140 / 140
4259 ms 578120 KB
#include <bits/stdc++.h>
#define ll long long
using namespace std;
 
#define           f     first
#define           s     second
#define           pb    push_back
#define           ep    emplace
#define           eb    emplace_back
#define           lb    lower_bound
#define           ub    upper_bound
#define       all(x)    x.begin(), x.end()
#define      rall(x)    x.rbegin(), x.rend()
#define   uniquev(v)    sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define     mem(f,x)    memset(f , x , sizeof(f))
#define        sz(x)    (int)(x).size()
#define  __lcm(a, b)    (1ll * ((a) / __gcd((a), (b))) * (b))
#define          mxx    *max_element
#define          mnn    *min_element
#define    cntbit(x)    __builtin_popcountll(x)
#define       len(x)    (int)(x.length())
 
const int N = 2e5 + 10;
const int M = 1e7 + 10;

vector <pair <int, int>> save[M + 1];
int a[N], pos[N], p[N], sz[N], mx[M + 1];

int get(int a) {
    return p[a] ? get(p[a]) : a;
}

bool unite(int a, int b) {
    a = get(a), b = get(b);
    if (a == b)
        return 0;

    if (sz[a] < sz[b])
        swap(a, b);

    sz[a] += sz[b];
    p[b] = a;
    return 1;
}

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

    int n;
    cin >> n;

    for (int i = 1; i <= n; i++) {
        sz[i] = 1;
        cin >> a[i];
        mx[a[i]] = i;
    }

    if (a[mx[1]] == 1) {
        cout << 0 << '\n';
        return 0;
    }

    for (int i = M - 2; i >= 0; i--) {
        if (mx[i] == 0) {
            mx[i] = mx[i + 1];
        }
    }

    for (int i = 2; i < M; i++) {
        if (i == a[mx[i]]) {
            int j = i * 2, p = mx[i];
            while (j < M) {
                int nxt = mx[j];
                if (nxt == 0)
                    break;

                save[a[nxt] % i].pb({p, nxt});
                j = (a[nxt] / i + 1) * i;
            }

            if (mx[i + 1]) {
                save[a[mx[i + 1]] % i].pb({p, mx[i + 1]});
            }
        }
    }

    ll ans = 0;

    for (int i = 0; i < M; i++) {
        for (auto x : save[i]) {
            if (unite(x.f, x.s))
                ans += i;
        }
    }

    cout << ans << '\n';

    return 0;
} 
# Verdict Execution time Memory Grader output
1 Correct 229 ms 275540 KB Output is correct
2 Correct 123 ms 277840 KB Output is correct
3 Correct 108 ms 275760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 114 ms 275596 KB Output is correct
2 Correct 112 ms 276056 KB Output is correct
3 Correct 108 ms 275724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 109 ms 275544 KB Output is correct
2 Correct 109 ms 275300 KB Output is correct
3 Correct 105 ms 275536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 160 ms 287712 KB Output is correct
2 Correct 257 ms 321016 KB Output is correct
3 Correct 171 ms 292668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 123 ms 279376 KB Output is correct
2 Correct 238 ms 299700 KB Output is correct
3 Correct 156 ms 287596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 217 ms 299748 KB Output is correct
2 Correct 290 ms 332748 KB Output is correct
3 Correct 173 ms 291252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 129 ms 280884 KB Output is correct
2 Correct 299 ms 325916 KB Output is correct
3 Correct 171 ms 290944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 253 ms 290132 KB Output is correct
2 Correct 1819 ms 486672 KB Output is correct
3 Correct 273 ms 292560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 243 ms 289848 KB Output is correct
2 Correct 3727 ms 578120 KB Output is correct
3 Correct 320 ms 296428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 132 ms 279856 KB Output is correct
2 Correct 4259 ms 572696 KB Output is correct
3 Correct 173 ms 292424 KB Output is correct