Submission #770599

# Submission time Handle Problem Language Result Execution time Memory
770599 2023-07-01T13:59:40 Z Nhoksocqt1 Zamjene (COCI16_zamjene) C++17
70 / 140
6000 ms 136440 KB
#include<bits/stdc++.h>
using namespace std;

#define inf 0x3f3f3f3f
#define sz(x) int((x).size())
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;

template<class X, class Y>
    inline bool maximize(X &x, const Y &y) {return (x < y ? x = y, 1 : 0);}
template<class X, class Y>
    inline bool minimize(X &x, const Y &y) {return (x > y ? x = y, 1 : 0);}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Random(int l, int r) {
    return uniform_int_distribution<int>(l, r)(rng);
}

const int MAXN = 1000006;

int lab[MAXN];
set<int> S[MAXN];
vector<int> adj[MAXN], idx;
map<int, int> cnt[MAXN];
int p[MAXN], q[MAXN], cntBadComp, nArr, numQuery;
ll result;

int root(int u) {
    return (lab[u] < 0) ? u : (lab[u] = root(lab[u]));
}

int getSize(int u) {
    return -lab[root(u)];
}

bool isBadComp(int u) {
    u = root(u);
    for (auto it : S[u]) {
        if(cnt[u][it] != 0)
            return true;
    }

    return false;
}

bool checkFor(int u, int v) {
    u = root(u), v = root(v);
    //cout << '.' << u << ' ' << v << '\n';
    if(u == v || !isBadComp(u) || !isBadComp(v))
        return false;

    multiset<int> SP, SQ;
    for (int it = 0; it < sz(adj[u]); ++it) SP.insert(p[adj[u][it]]), SQ.insert(q[adj[u][it]]);
    for (int it = 0; it < sz(adj[v]); ++it) SP.insert(p[adj[v][it]]), SQ.insert(q[adj[v][it]]);

    auto itp(SP.begin());
    auto itq(SQ.begin()); bool check(1);
    for (int it = 0; it < sz(adj[u]) + sz(adj[v]); ++it, ++itp, ++itq) if(*itp != *itq) {check = 0; break; }

    for (auto it : S[v]) {
        if(cnt[v][it] != -cnt[u][it]) {
            if(check) {
                auto itp(SP.begin());
                auto itq(SQ.begin());
                for (int it = 0; it < sz(adj[u]) + sz(adj[v]); ++it, ++itp, ++itq) cout << *itp << ' ' << *itq << '\n'; 
                cout << ',' << u << ' ' << v << '.'; for (int i = 1; i <= nArr; ++i) cout << root(i) << " \n"[i == nArr]; exit(1);
            }
            return false;
        }
    }

    for (auto it : S[u]) {
        if(cnt[u][it] != -cnt[v][it]) {
            if(check) {
                auto itp(SP.begin());
                auto itq(SQ.begin());
                for (int it = 0; it < sz(adj[u]) + sz(adj[v]); ++it, ++itp, ++itq) cout << *itp << ' ' << *itq << '\n';
                cout << ',' << u << ' ' << v << '.'; for (int i = 1; i <= nArr; ++i) cout << root(i) << " \n"[i == nArr]; exit(1);
            }
            return false;
        }
    }

    if(!check) {
        auto itp(SP.begin());
        auto itq(SQ.begin());
        for (int it = 0; it < sz(adj[u]) + sz(adj[v]); ++it, ++itp, ++itq) cout << *itp << ' ' << *itq << '\n';
        cout << u << ' ' << v << '.'; for (int i = 1; i <= nArr; ++i) cout << root(i) << " \n"[i == nArr]; exit(1);
    }
    return true;
}

ll calc(int u) {
    u = root(u);
    if(!isBadComp(u))
        return 0;

    ll res(0);
    for (int v = 1; v <= nArr; ++v) {
        if(root(v) != v)
            continue;
            
        res += 1LL * getSize(v) * checkFor(u, v);
    }
    
    return res;
}

void swapPos(int u, int v) {
    int ru(root(u)), rv(root(v));
    if(ru == rv) {
        swap(p[u], p[v]);
        return;
    }

    int szu(getSize(ru)), szv(getSize(rv));
    result -= 1LL * szu * calc(ru) + 1LL * szv * calc(rv) - 1LL * szu * szv * checkFor(ru, rv);
    cntBadComp -= 1LL * isBadComp(ru) + isBadComp(rv);
    --cnt[ru][p[u]], --cnt[rv][p[v]];

    swap(p[u], p[v]);
    ++cnt[ru][p[u]], ++cnt[rv][p[v]];
    S[ru].insert(p[u]), S[rv].insert(p[v]);
    result += 1LL * szu * calc(ru) + 1LL * szv * calc(rv) - 1LL * szu * szv * checkFor(ru, rv);
    cntBadComp += 1LL * isBadComp(ru) + isBadComp(rv);
}

bool join(int u, int v) {
    u = root(u), v = root(v);
    if(u == v)
        return (false);

    if(lab[u] > lab[v])
        swap(u, v);

    result -= -1LL * lab[u] * calc(u) + -1LL * lab[v] * calc(v) - 1LL * lab[u] * lab[v] * checkFor(u, v);
    //cout << u << ' ' << isBadComp(u) << ' ' << v << ' ' << isBadComp(v) << ' ' << calc(u) << ' ' << calc(v) << '\n';
    cntBadComp -= 1LL * isBadComp(u) + isBadComp(v);
    lab[u] += lab[v];
    lab[v] = u;

    for (int it = 0; it < sz(adj[v]); ++it) {
        int c(adj[v][it]);
        adj[u].push_back(c);
        ++cnt[u][p[c]], --cnt[u][q[c]];
        S[u].insert(p[c]);
        S[u].insert(q[c]);
    }

    cntBadComp += isBadComp(u);
    result += -1LL * lab[u] * calc(u);
    adj[v].clear();
    S[v].clear(), cnt[v].clear();
    return (true);
}

void process() {
    cin >> nArr >> numQuery; //nArr = 6, numQuery = 1000;
    for (int i = 1; i <= nArr; ++i) {
        cin >> p[i];
        //p[i] = Random(1, nArr); cout << p[i] << " \n"[i == nArr];
        idx.push_back(p[i]);
        lab[i] = -1;
    }

    sort(idx.begin(), idx.end());
    idx.erase(unique(idx.begin(), idx.end()), idx.end());
    for (int i = 1; i <= nArr; ++i)
        p[i] = q[i] = upper_bound(idx.begin(), idx.end(), p[i]) - idx.begin();

    sort(q + 1, q + nArr + 1);
    for (int i = 1; i <= nArr; ++i) {
        cntBadComp += (p[i] != q[i]);
        ++cnt[i][p[i]], --cnt[i][q[i]];
        adj[i].push_back(i);
        S[i].insert(p[i]);
        S[i].insert(q[i]);
        result += calc(i);
    }

    for (int t = 0; t < numQuery; ++t) {
        int type, A, B;
        cin >> type;
        //type = Random(1, 4); cout << type << ' ';
        if(type <= 2) {
            cin >> A >> B;
            //A = Random(1, nArr - 1), B = Random(A + 1, nArr); cout << A << ' ' << B << '\n';
        }

        if(type == 1) {
            swapPos(A, B);
        } else
            if(type == 2) {
                join(A, B);
            } else
                if(type == 3) {
                    cout << (cntBadComp == 0 ? "DA" : "NE") << '\n';
                } else {
                    cout << result << '\n';
                }
    }
}

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

    //freopen("main.inp", "r", stdin);
    //freopen("main.out", "w", stdout);

    process();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 54 ms 117712 KB Output is correct
2 Correct 57 ms 117928 KB Output is correct
3 Correct 52 ms 117720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 117708 KB Output is correct
2 Correct 54 ms 117720 KB Output is correct
3 Correct 54 ms 117676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 70 ms 117720 KB Output is correct
2 Correct 75 ms 117812 KB Output is correct
3 Correct 78 ms 117880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 180 ms 117812 KB Output is correct
2 Correct 166 ms 117852 KB Output is correct
3 Correct 210 ms 117972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 248 ms 117996 KB Output is correct
2 Correct 346 ms 118132 KB Output is correct
3 Correct 480 ms 118744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 6045 ms 121404 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 6080 ms 120668 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 6031 ms 127244 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 6106 ms 136440 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 6034 ms 135752 KB Time limit exceeded
2 Halted 0 ms 0 KB -