답안 #255557

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
255557 2020-08-01T08:26:38 Z Vladikus004 Zamjene (COCI16_zamjene) C++14
126 / 140
6000 ms 172148 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;
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";
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 283 ms 62968 KB Output is correct
2 Correct 290 ms 62968 KB Output is correct
3 Correct 277 ms 62968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 283 ms 62988 KB Output is correct
2 Correct 280 ms 62968 KB Output is correct
3 Correct 278 ms 62968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 285 ms 62992 KB Output is correct
2 Correct 281 ms 62968 KB Output is correct
3 Correct 280 ms 62968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 279 ms 62968 KB Output is correct
2 Correct 279 ms 62968 KB Output is correct
3 Correct 303 ms 63096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 278 ms 62968 KB Output is correct
2 Correct 280 ms 63096 KB Output is correct
3 Correct 283 ms 63096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 303 ms 63356 KB Output is correct
2 Correct 299 ms 63352 KB Output is correct
3 Correct 287 ms 63480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 355 ms 66504 KB Output is correct
2 Correct 405 ms 67832 KB Output is correct
3 Correct 473 ms 70008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1655 ms 89464 KB Output is correct
2 Correct 5723 ms 145016 KB Output is correct
3 Execution timed out 6094 ms 172148 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Correct 3228 ms 125092 KB Output is correct
2 Correct 4630 ms 143276 KB Output is correct
3 Correct 2608 ms 135132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1571 ms 100984 KB Output is correct
2 Correct 3448 ms 128444 KB Output is correct
3 Correct 2805 ms 135500 KB Output is correct