Submission #659041

# Submission time Handle Problem Language Result Execution time Memory
659041 2022-11-16T04:29:40 Z SansPapyrus683 Zamjene (COCI16_zamjene) C++17
140 / 140
4917 ms 187088 KB
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>

using std::cout;
using std::endl;
using std::vector;

class DominikArray {
    private:
        const long long POW = 1e6 + 3;

        vector<int> arr;
        vector<int> sorted;
        
        vector<int> parent;
        vector<int> size;
        int bad_num = 0;

        std::map<int, long long> elem_pow;  // raise to the power of the value
        // the current hash of a component
        vector<long long> hash;
        // the needed hash for a component to be able to be sorted
        vector<long long> req_hash;
        // the hash differences of the bad components
        std::map<long long, long long> bad_diff;
        long long cloud_pairs = 0;

        int get_top(int n) {
            return parent[n] == n ? n : (parent[n] = get_top(parent[n]));
        }

        inline bool is_unsortable(int n) {
            return hash[n] != req_hash[n];
        }

        /** if a component is bad, add it to the bad log */
        void add_if_bad(int n) {
            if (is_unsortable(n)) {
                bad_num++;
                long long diff = req_hash[n] - hash[n];
                bad_diff[diff] += size[n];

                long long pair_amt = bad_diff[-diff];
                cloud_pairs += pair_amt * size[n];
            }
        }

        void remove_if_bad(int n) {
            if (is_unsortable(n)) {
                bad_num--;
                long long diff = req_hash[n] - hash[n];
                bad_diff[diff] -= size[n];

                cloud_pairs -= bad_diff[-diff] * size[n];
            }
        }
    public:
        DominikArray(vector<int> arr)
            : arr(arr), parent(arr.size()), size(arr.size(), 1),
              hash(arr.size()), req_hash(arr.size()) {
            sorted = arr;
            std::sort(sorted.begin(), sorted.end());

            // perform coordinate compression
            long long curr_pow = 1;
            for (int i : sorted) {
                if (!elem_pow.count(i)) {
                    elem_pow[i] = curr_pow;
                    curr_pow *= POW;
                }
            }
            
            // set up DSU and the hashes
            for (int i = 0; i < arr.size(); i++) {
                parent[i] = i;
                hash[i] = elem_pow[arr[i]];
                req_hash[i] = elem_pow[sorted[i]];
                add_if_bad(i);
            }
        }

        void swap(int a, int b) {
            int top_a = get_top(a);
            int top_b = get_top(b);

            // temporarily take them out of the bad log (if applicable)
            remove_if_bad(top_a);
            remove_if_bad(top_b);

            // change the hashes of the two components
            hash[top_a] += elem_pow[arr[b]] - elem_pow[arr[a]];
            hash[top_b] += elem_pow[arr[a]] - elem_pow[arr[b]];

            // add the back (if applicable)
            add_if_bad(top_a);
            add_if_bad(top_b);

            std::swap(arr[a], arr[b]);
        }

        void link(int a, int b) {
            a = get_top(a);
            b = get_top(b);
            if (a == b) {
                return;
            }

            if (size[a] < size[b]) {
                return link(b, a);
            }
            
            remove_if_bad(a);
            remove_if_bad(b);
            
            size[a] += size[b];
            parent[b] = a;
            
            // just add the hash of the smaller component to the bigger one
            hash[a] += hash[b];
            req_hash[a] += req_hash[b];

            add_if_bad(a);
        }

        bool sortable() {
            // for everything to be sortable, there can't be any bad components
            return bad_num == 0;
        }

        long long needed_pair_num() {
            return cloud_pairs;
        }
};

// https://oj.uz/problem/view/COCI16_zamjene (input omitted due to length)
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(NULL);

    int arr_len;
    int query_num;
    std::cin >> arr_len >> query_num;
    vector<int> arr(arr_len);
    for (int& i : arr) {
        std::cin >> i;
    }
    
    DominikArray array(arr);
    for (int q = 0; q < query_num; q++) {
        int type;
        std::cin >> type;
        int a, b;  // not necessarily used (queries of type 3 & 4)
        switch (type) {
            case 1:
                std::cin >> a >> b;
                array.swap(--a, --b);
                break;
            case 2:
                std::cin >> a >> b;
                array.link(--a, --b);
                break;
            case 3:
                cout << (array.sortable() ? "DA" : "NE") << '\n';
                break;
            case 4:
                cout << array.needed_pair_num() << '\n';
                break;
        };
    }
}

Compilation message

zamjene.cpp: In constructor 'DominikArray::DominikArray(std::vector<int>)':
zamjene.cpp:76:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |             for (int i = 0; i < arr.size(); i++) {
      |                             ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 848 KB Output is correct
2 Correct 5 ms 832 KB Output is correct
3 Correct 6 ms 848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 5300 KB Output is correct
2 Correct 92 ms 7004 KB Output is correct
3 Correct 129 ms 10112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 978 ms 43736 KB Output is correct
2 Correct 3975 ms 135364 KB Output is correct
3 Correct 4917 ms 187088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2088 ms 88736 KB Output is correct
2 Correct 3494 ms 110844 KB Output is correct
3 Correct 1643 ms 99984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 855 ms 53804 KB Output is correct
2 Correct 2276 ms 93852 KB Output is correct
3 Correct 1695 ms 100112 KB Output is correct