Submission #255553

# Submission time Handle Problem Language Result Execution time Memory
255553 2020-08-01T08:24:23 Z Vladikus004 Zamjene (COCI16_zamjene) C++14
126 / 140
6000 ms 166452 KB
#include <bits/stdc++.h>
#define inf 2e9
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;

mt19937_64 rnd(time(0));

const int N = 1000000 + 3;
int n, q, sz[N], a[N], b[N], p[N], cnt, ans3;
ll sum[N], nsum[N], ans;
map <ll, int> mp;
unordered_map <int, ll> code;

void init(){
    for (int i = 0; i < N; i++)
        code[i] = rnd();
    for (int i = 0; i < n; i++){
        p[i] = i;
        sz[i] = 1;
        sum[i] = code[a[i]];
        nsum[i] = code[b[i]];
        if (sum[i] != nsum[i]){
            ans += mp[nsum[i] - sum[i]];
            mp[sum[i] - nsum[i]]++;
        }else ans3++;
    }
}

int get_anc(int x){
    if (p[x] == x) return x;
    return p[x] = get_anc(p[x]);
}

void er(int x){
    if (nsum[x] != sum[x]){
        ans -= mp[nsum[x] - sum[x]] * sz[x];
        mp[sum[x] - nsum[x]] -= sz[x];
        if (!mp[sum[x] - nsum[x]]) mp.erase(sum[x] - nsum[x]);
    }else ans3--;
}

void add(int x){
    if (nsum[x] != sum[x]){
        ans += mp[nsum[x] - sum[x]] * sz[x];
        mp[sum[x] - nsum[x]] += sz[x];
    }else ans3++;
}

void unite(int x, int y){
    int px = get_anc(x);
    int py = get_anc(y);
    if (px == py) return;
    cnt--;
    er(px);
    er(py);
    if (sz[px] < sz[py]) swap(px, py);
    p[py] = px;
    sz[px] += sz[py];
    sum[px] += sum[py];
    nsum[px] += nsum[py];
    add(px);
}

void sw(int x, int y){
    int px = get_anc(x);
    int py = get_anc(y);
    if (px == py) {
        swap(a[x], a[y]);
        return;
    }
    er(px);
    er(py);
    sum[px] += code[a[y]] - code[a[x]];
    sum[py] += code[a[x]] - code[a[y]];
    add(px);
    add(py);
    swap(a[x], a[y]);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
    #endif // LOCAL
    cin >> n >> q;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        b[i] = a[i];
    }
    sort(b, b + n);
    init();
    cnt = n;
    for (int i = 0; i < q; i++){
        int tp, x, y;
        cin >> tp;
        if (tp == 1){
            cin >> x >> y;
            --x; --y;
            sw(x, y);
        }else
        if (tp == 2){
            cin >> x >> y;
            --x; --y;
            unite(x, y);
        }else
        if (tp == 3){
            if (cnt==ans3) cout << "DA\n";
            else cout << "NE\n";
        }else{
            cout << ans << "\n";
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 95 ms 50032 KB Output is correct
2 Correct 103 ms 50156 KB Output is correct
3 Correct 100 ms 50048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 99 ms 50024 KB Output is correct
2 Correct 102 ms 50032 KB Output is correct
3 Correct 102 ms 50032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 108 ms 50076 KB Output is correct
2 Correct 98 ms 50028 KB Output is correct
3 Correct 96 ms 50036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 102 ms 50024 KB Output is correct
2 Correct 99 ms 50028 KB Output is correct
3 Correct 107 ms 50008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 99 ms 50024 KB Output is correct
2 Correct 99 ms 50000 KB Output is correct
3 Correct 102 ms 50028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 109 ms 50156 KB Output is correct
2 Correct 104 ms 50284 KB Output is correct
3 Correct 104 ms 50156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 157 ms 51180 KB Output is correct
2 Correct 212 ms 52176 KB Output is correct
3 Correct 259 ms 54236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1386 ms 74476 KB Output is correct
2 Correct 5466 ms 139028 KB Output is correct
3 Execution timed out 6056 ms 166452 KB Time limit exceeded
# Verdict Execution time Memory Grader output
1 Correct 2594 ms 109936 KB Output is correct
2 Correct 4283 ms 138624 KB Output is correct
3 Correct 2039 ms 131664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1135 ms 85712 KB Output is correct
2 Correct 2938 ms 123080 KB Output is correct
3 Correct 2019 ms 131540 KB Output is correct