Submission #1075316

# Submission time Handle Problem Language Result Execution time Memory
1075316 2024-08-26T02:14:29 Z Neco_arc Zamjene (COCI16_zamjene) C++17
98 / 140
2715 ms 141112 KB
#include <bits/stdc++.h>
#ifdef LOCAL
#include <bits/debug.hpp>
#endif // LOCAL

#define ll long long
#define all(x) x.begin(), x.end()
#define Neco "Zamjene"
#define resp(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define getbit(x,i) ((x >> i)&1)
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define cntbit(x) __builtin_popcountll(x)
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
#define maxn (int) 1e6 + 7
#define int long long

using namespace std;

const ll mod = 1e9 + 7; //972663749
const ll base = 911382323;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r) {
    return uniform_int_distribution<ll> (l, r)(rng);
}

int n, q;
int a[maxn], b[maxn];
ll Rnd[maxn];

struct DSU {
    int lab[maxn], tplt;
    ll S[maxn], Val;
    map<ll, int> P;

    void init() {
        tplt = n;
        fi(i, 1, n) Change(0, 1);
        fi(i, 1, n) {
            lab[i] = -1;
            update(i, Rnd[a[i]] - Rnd[b[i]]);
        }
    }

    void Change(ll val, ll w) {
        if(val != 0) Val += P[-val] * w;
        P[val] += w;
    }

    void update(int u, ll w) {
        Change(S[u], lab[u]);
        S[u] += w;
        Change(S[u], -lab[u]);
    }

    bool good() { return P[0] == n; }

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

    void uni(int u, int v) {
        int p = get_rt(u), q = get_rt(v); if(p == q) return;
        if(lab[p] > lab[q]) swap(p, q);

        --tplt;
        ll Sum = S[p] + S[q];
        update(q, -S[q]), update(p, -S[p]);
        P[0] -= -(lab[p] + lab[q]);

        lab[p] += lab[q], lab[q] = p;
        S[p] = Sum, Change(S[p], -lab[p]);
    }

} Dsu;

void solve()
{

    cin >> n >> q;
    fi(i, 1, n) cin >> a[i], b[i] = a[i];
    sort(b + 1, b + 1 + n);

    Dsu.init();

    fi(i, 1, q) {
        int type, x, y; cin >> type;
        if(type <= 2) cin >> x >> y;

        if(type == 1) {
            int u = Dsu.get_rt(x), v = Dsu.get_rt(y);
            Dsu.update(u, Rnd[a[y]] - Rnd[a[x]]);
            Dsu.update(v, Rnd[a[x]] - Rnd[a[y]]);
            swap(a[x], a[y]);
        }

        if(type == 2) {
            Dsu.uni(x, y);
        }

        if(type == 3) {
            cout << (Dsu.good() ? "DA\n" : "NE\n");
        }

        if(type == 4) {
            cout << Dsu.Val << '\n';
        }

    }


}


main()
{

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    if(fopen(Neco".inp", "r")) {
        freopen(Neco".inp", "r", stdin);
        freopen(Neco".out", "w", stdout);
    }

    fi(i, 1, maxn - 2) Rnd[i] = GetRandom(1, 1e9);

    int nTest = 1;
//    cin >> nTest;


    while(nTest--)
    {
        solve();
    }


    return 0;
}

Compilation message

zamjene.cpp:117:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  117 | main()
      | ^~~~
zamjene.cpp: In function 'int main()':
zamjene.cpp:125:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  125 |         freopen(Neco".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
zamjene.cpp:126:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  126 |         freopen(Neco".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 8284 KB Output is correct
2 Correct 11 ms 8284 KB Output is correct
3 Correct 9 ms 8284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 8284 KB Output is correct
2 Correct 11 ms 8240 KB Output is correct
3 Correct 10 ms 8284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 8284 KB Output is correct
2 Correct 10 ms 8284 KB Output is correct
3 Correct 10 ms 8284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 8284 KB Output is correct
2 Correct 11 ms 8220 KB Output is correct
3 Correct 10 ms 8284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 8284 KB Output is correct
2 Correct 10 ms 8284 KB Output is correct
3 Correct 11 ms 8284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 8796 KB Output is correct
2 Correct 15 ms 8796 KB Output is correct
3 Correct 13 ms 8796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 12636 KB Output is correct
2 Correct 65 ms 14420 KB Output is correct
3 Correct 111 ms 17608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 627 ms 49492 KB Output is correct
2 Incorrect 2715 ms 141112 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1260 ms 92676 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 539 ms 58044 KB Output is correct
2 Incorrect 1410 ms 97680 KB Output isn't correct
3 Halted 0 ms 0 KB -