Submission #1004198

# Submission time Handle Problem Language Result Execution time Memory
1004198 2024-06-21T06:23:08 Z TahirAliyev Sirni (COCI17_sirni) C++17
140 / 140
2074 ms 731176 KB
#include <bits/stdc++.h>

using namespace std;

// #define int long long
#define ll long long
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
#define oo 1e9

const int MAX = 1e5 + 5, MX = 1e7 + 7;
int n;
int arr[MAX];
int nxt[MX];
vector<pii> E[MX];

struct DSU{
    int par[MAX];
    void init(){
        for(int i = 0; i < MAX; i++) par[i] = -1;
    }
    int get(int i){
        if(par[i] < 0) return i;
        return par[i] = get(par[i]);
    }
    bool unite(int u, int v){
        u = get(u), v = get(v);
        if(u == v) return 0;
        if(-par[u] < -par[v]) swap(u, v);
        par[u] += par[v];
        par[v] = u;
        return 1;
    }
};

DSU dsu;

void solve(){
    cin >> n;
    dsu.init();
    for(int i = 1; i <= n; i++){
        cin >> arr[i];
        if(nxt[arr[i]]) dsu.unite(i, nxt[arr[i]]);
        nxt[arr[i]] = i;
    } 
    int cur = 0;
    for(int i = MX - 1; i >= 1; i--){
        if(nxt[i]) cur = nxt[i];
        nxt[i] = cur;
    }
    for(int i = 1; i <= n; i++){
        if(nxt[arr[i] + 1] && arr[nxt[arr[i] + 1]] < 2 * arr[i]) E[arr[nxt[arr[i] + 1]] - arr[i]].push_back({i, nxt[arr[i] + 1]});
        for(int j = 2 * arr[i]; j < MX; j += arr[i]){
            if(nxt[j] && (arr[nxt[j]] < j + arr[i])) E[arr[nxt[j]] - j].push_back({i, nxt[j]});
        }
    }
    ll ans = 0;
    for(int i = 0; i < MX; i++){
        for(auto a : E[i]){
            if(dsu.unite(a.first, a.second)) ans += i;
        }
    }
    cout << ans << '\n';
}

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    while(t--){
        solve();
    }
}
# Verdict Execution time Memory Grader output
1 Correct 123 ms 274772 KB Output is correct
2 Correct 155 ms 278096 KB Output is correct
3 Correct 143 ms 275028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 167 ms 274800 KB Output is correct
2 Correct 352 ms 275816 KB Output is correct
3 Correct 123 ms 274924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 122 ms 274916 KB Output is correct
2 Correct 121 ms 274680 KB Output is correct
3 Correct 125 ms 274856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 202 ms 284392 KB Output is correct
2 Correct 446 ms 321796 KB Output is correct
3 Correct 253 ms 294416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 132 ms 276304 KB Output is correct
2 Correct 2074 ms 545836 KB Output is correct
3 Correct 264 ms 286600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 292 ms 299480 KB Output is correct
2 Correct 615 ms 351336 KB Output is correct
3 Correct 219 ms 288900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 230 ms 278148 KB Output is correct
2 Correct 654 ms 343740 KB Output is correct
3 Correct 240 ms 291336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 157 ms 287316 KB Output is correct
2 Correct 846 ms 570908 KB Output is correct
3 Correct 168 ms 291012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 172 ms 287148 KB Output is correct
2 Correct 1111 ms 695432 KB Output is correct
3 Correct 187 ms 297920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 158 ms 277072 KB Output is correct
2 Correct 1136 ms 731176 KB Output is correct
3 Correct 264 ms 295220 KB Output is correct