kyopro_library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub dyktr06/kyopro_library

:heavy_check_mark: test/library_checker/data_structure/static_range_mode_query.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/static_range_mode_query"
#include <bits/stdc++.h>
using namespace std;

#include "../../../lib/data_structure/mo.hpp"
#include "../../../lib/others/compression.hpp"

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, q; cin >> n >> q;
    vector<int> a(n);
    for(int i = 0; i < n; i++){
        cin >> a[i];
    }
    compress<int> comp(a);
    Mo mo(n);
    for(int i = 0; i < q; i++){
        int l, r; cin >> l >> r;
        mo.add(l, r);
    }
    vector<unordered_set<int>> val(n + 1);
    vector<int> cnt(n), reach(n + 1);
    for(int i = 0; i < comp.size(); i++){
        val[0].insert(i);
    }
    reach[0] = n;
    vector<pair<int, int>> res(q);
    int now = 0;
    auto add = [&](int i){
        int v = comp.compressed[i];
        val[cnt[v]].erase(v);
        reach[cnt[v]]--;
        ++cnt[v];
        val[cnt[v]].insert(v);
        reach[cnt[v]]++;

        if(now < cnt[v]){
            now = cnt[v];
        }
    };
    auto erase = [&](int i){
        int v = comp.compressed[i];
        val[cnt[v]].erase(v);
        reach[cnt[v]]--;
        --cnt[v];
        val[cnt[v]].insert(v);
        reach[cnt[v]]++;

        if(reach[now] == 0){
            now--;
        }
    };
    auto output = [&](int q){
        res[q] = {comp.inv(*val[now].begin()), now};
    };
    mo.build(add, erase, output);
 
    for(auto ans : res){
        cout << ans.first << " " << ans.second << "\n";
    }
}
#line 1 "test/library_checker/data_structure/static_range_mode_query.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/static_range_mode_query"
#include <bits/stdc++.h>
using namespace std;

#line 2 "lib/data_structure/mo.hpp"

// [0 , N) 上の区間に対する Q 個のクエリを計算します。 : O(N√Q) (区間の伸縮が O(1) で行える場合)
struct Mo{
    int n;
    vector<pair<int, int>> lr;

    Mo(const int n) : n(n) {}

    /* [l, r) */
    void add(const int l, const int r){
        lr.emplace_back(l, r);
    }

    template <typename AL, typename AR, typename EL, typename ER, typename O>
    void build(const AL &add_left, const AR &add_right, const EL &erase_left, const ER &erase_right, const O &out){
        int q = (int) lr.size();
        int border = max<int>(1, 1.0 * n / max<double>(1.0, sqrt(q * 2.0 / 3.0)));
        vector<int> ord(q);
        iota(ord.begin(), ord.end(), 0);
        sort(ord.begin(), ord.end(), [&](int a, int b){
            int ablock = lr[a].first / border, bblock = lr[b].first / border;
            if(ablock != bblock){
                return ablock < bblock;
            }
            return (ablock & 1) ? lr[a].second > lr[b].second : lr[a].second < lr[b].second;
        });
        int l = 0, r = 0;
        for(const auto &k : ord){
            while(l > lr[k].first) add_left(--l);
            while(r < lr[k].second) add_right(r++);
            while(l < lr[k].first) erase_left(l++);
            while(r > lr[k].second) erase_right(--r);
            out(k);
        }
    }

    template <typename A, typename E, typename O>
    void build(const A &add, const E &erase, const O &out){
        build(add, add, erase, erase, out);
    }
};
#line 2 "lib/others/compression.hpp"

/**
 * @brief Compression (座標圧縮)
 * @docs docs/others/compression.md
 */

#line 10 "lib/others/compression.hpp"

template <typename T>
struct compress{
    std::vector<T> sorted;
    std::vector<int> compressed;

    compress(const std::vector<T> &vec){
        int n = vec.size();
        compressed.resize(n);
        for(T x : vec){
            sorted.emplace_back(x);
        }
        std::sort(sorted.begin(), sorted.end());
        sorted.erase(std::unique(sorted.begin(), sorted.end()), sorted.end());
        for(int i = 0; i < n; ++i){
            compressed[i] = std::lower_bound(sorted.begin(), sorted.end(), vec[i]) - sorted.begin();
        }
    }

    int get(const T &x) const{
        return std::lower_bound(sorted.begin(), sorted.end(), x) - sorted.begin();
    }

    T inv(const int x) const{
        return sorted[x];
    }

    size_t size() const{
        return sorted.size();
    }

    std::vector<int> getCompressed() const{
        return compressed;
    }
};
#line 7 "test/library_checker/data_structure/static_range_mode_query.test.cpp"

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, q; cin >> n >> q;
    vector<int> a(n);
    for(int i = 0; i < n; i++){
        cin >> a[i];
    }
    compress<int> comp(a);
    Mo mo(n);
    for(int i = 0; i < q; i++){
        int l, r; cin >> l >> r;
        mo.add(l, r);
    }
    vector<unordered_set<int>> val(n + 1);
    vector<int> cnt(n), reach(n + 1);
    for(int i = 0; i < comp.size(); i++){
        val[0].insert(i);
    }
    reach[0] = n;
    vector<pair<int, int>> res(q);
    int now = 0;
    auto add = [&](int i){
        int v = comp.compressed[i];
        val[cnt[v]].erase(v);
        reach[cnt[v]]--;
        ++cnt[v];
        val[cnt[v]].insert(v);
        reach[cnt[v]]++;

        if(now < cnt[v]){
            now = cnt[v];
        }
    };
    auto erase = [&](int i){
        int v = comp.compressed[i];
        val[cnt[v]].erase(v);
        reach[cnt[v]]--;
        --cnt[v];
        val[cnt[v]].insert(v);
        reach[cnt[v]]++;

        if(reach[now] == 0){
            now--;
        }
    };
    auto output = [&](int q){
        res[q] = {comp.inv(*val[now].begin()), now};
    };
    mo.build(add, erase, output);
 
    for(auto ans : res){
        cout << ans.first << " " << ans.second << "\n";
    }
}
Back to top page