shogo-library

競技プログラミング用の C++ ライブラリ

View the Project on GitHub shogo314/shogo-library

:heavy_check_mark: test/yosupo/point-set-range-frequency.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/point_set_range_frequency"

#include <iostream>
#include <vector>

#include "../../src/data_structures/counter-segtree.hpp"

int main() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    int n, q; std::cin >> n >> q;
    std::vector<int> a(n);
    for (int& x : a) std::cin >> x;
    CounterSegTree<int> seg(a);
    while (q--) {
        int t; std::cin >> t;
        if (t == 0) {
            int k, v; std::cin >> k >> v;
            seg.set(k, v);
        } else {
            int l, r, x; std::cin >> l >> r >> x;
            std::cout << seg.find(l, r, x).second << '\n';
        }
    }
}
#line 1 "test/yosupo/point-set-range-frequency.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/point_set_range_frequency"

#include <iostream>
#include <vector>

#line 2 "src/data_structures/counter-segtree.hpp"

/**
 * @file counter-segtree.hpp
 * @brief 区間内の値の個数を数えるセグメント木
 */

#include <cassert>
#include <map>
#include <utility>
#line 12 "src/data_structures/counter-segtree.hpp"

template <typename T>
class CounterSegTree {
   public:
    using key_type = T;
    using mapped_type = int;

   private:
    int n, sz, height;
    std::vector<std::map<key_type, mapped_type>> data;

   public:
    explicit CounterSegTree(const std::vector<key_type>& v) {
        n = v.size();
        sz = 1;
        height = 0;
        while (sz < n) {
            sz <<= 1;
            height++;
        }
        data.assign(sz * 2, {});
        for (int i = 0; i < n; i++) {
            data[i + sz][v[i]] = 1;
        }
        for (int i = sz - 1; i > 0; i--) {
            data[i] = data[2 * i];
            for (auto [s, c] : data[2 * i + 1]) {
                data[i][s] += c;
            }
        }
    }
    key_type get(int k) const {
        assert(0 <= k and k < n);
        return data[k + sz].begin()->first;
    }
    void set(int k, const key_type& x) {
        assert(0 <= k and k < n);
        k += sz;
        key_type y = data[k].begin()->first;
        while (k > 0) {
            auto itr = data[k].find(y);
            if (itr->second == 1) {
                data[k].erase(itr);
            } else {
                itr->second--;
            }
            data[k][x]++;
            k >>= 1;
        }
    }
    std::pair<key_type, mapped_type> lower_bound(int l, int r, key_type x) const {
        std::pair<key_type, mapped_type> ret = {{}, 0};
        l += sz;
        r += sz;
        while (l < r) {
            if (l & 1) {
                auto itr = data[l].lower_bound(x);
                if (itr != data[l].end()) {
                    if (ret.second == 0) {
                        ret = *itr;
                    } else if (itr->first == ret.first) {
                        ret.second += itr->second;
                    } else if (itr->first < ret.first) {
                        ret = *itr;
                    }
                }
                ++l;
            }
            if (r & 1) {
                --r;
                auto itr = data[r].lower_bound(x);
                if (itr != data[r].end()) {
                    if (ret.second == 0) {
                        ret = *itr;
                    } else if (itr->first == ret.first) {
                        ret.second += itr->second;
                    } else if (itr->first < ret.first) {
                        ret = *itr;
                    }
                }
            }
            l >>= 1;
            r >>= 1;
        }
        return ret;
    }
    std::pair<key_type, mapped_type> find(int l, int r, key_type x) const {
        std::pair<key_type, mapped_type> ret = {x, 0};
        l += sz;
        r += sz;
        while (l < r) {
            if (l & 1) {
                auto itr = data[l].find(x);
                if (itr != data[l].end()) {
                    ret.second += itr->second;
                }
                ++l;
            }
            if (r & 1) {
                --r;
                auto itr = data[r].find(x);
                if (itr != data[r].end()) {
                    ret.second += itr->second;
                }
            }
            l >>= 1;
            r >>= 1;
        }
        return ret;
    }
};
#line 7 "test/yosupo/point-set-range-frequency.test.cpp"

int main() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    int n, q; std::cin >> n >> q;
    std::vector<int> a(n);
    for (int& x : a) std::cin >> x;
    CounterSegTree<int> seg(a);
    while (q--) {
        int t; std::cin >> t;
        if (t == 0) {
            int k, v; std::cin >> k >> v;
            seg.set(k, v);
        } else {
            int l, r, x; std::cin >> l >> r >> x;
            std::cout << seg.find(l, r, x).second << '\n';
        }
    }
}
Back to top page