cp-library-cpp

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

View the Project on GitHub suisen-cp/cp-library-cpp

:heavy_check_mark: test/src/datastructure/segment_tree/persistent_commutative_dual_segment_tree/abc253.test.cpp

Depends on

Code

#define PROBLEM "https://atcoder.jp/contests/abc253/tasks/abc253_f"

#include <iostream>

#include "library/datastructure/segment_tree/persistent_commutative_dual_segment_tree.hpp"

long long composition(long long f, long long g) {
    return f + g;
}
long long id() {
    return 0;
}
 
using Tree = suisen::PersistentCommutativeDualSegmentTree<long long, composition, id>;
 
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n, m, q;
    std::cin >> n >> m >> q;
 
    std::vector<std::pair<int, int>> last(n);
 
    Tree::init_pool(15000000);
 
    std::vector<Tree> segs(q + 1);
    segs[0] = Tree(m + 1);
 
    for (int t = 1; t <= q; ++t) {
        int query_type;
        std::cin >> query_type;
        segs[t] = segs[t - 1];
        if (query_type == 1) {
            int l, r, x;
            std::cin >> l >> r >> x;
            --l;
            segs[t] = segs[t].apply(l, r, x);
        } else if (query_type == 2) {
            int i, x;
            std::cin >> i >> x;
            --i;
            last[i] = { t - 1, x };
        } else {
            int i, j;
            std::cin >> i >> j;
            --i, --j;
            auto [tl, x] = last[i];
            std::cout << x + segs[t].get(j) - segs[tl].get(j) << '\n';
        }
    }
    return 0;
}
#line 1 "test/src/datastructure/segment_tree/persistent_commutative_dual_segment_tree/abc253.test.cpp"
#define PROBLEM "https://atcoder.jp/contests/abc253/tasks/abc253_f"

#include <iostream>

#line 1 "library/datastructure/segment_tree/persistent_commutative_dual_segment_tree.hpp"



#include <cassert>

#line 1 "library/util/object_pool.hpp"



#include <deque>
#include <vector>

namespace suisen {
    template <typename T, bool auto_extend = false>
    struct ObjectPool {
        using value_type = T;
        using value_pointer_type = T*;

        template <typename U>
        using container_type = std::conditional_t<auto_extend, std::deque<U>, std::vector<U>>;

        container_type<value_type> pool;
        container_type<value_pointer_type> stock;
        decltype(stock.begin()) it;

        ObjectPool() : ObjectPool(0) {}
        ObjectPool(int siz) : pool(siz), stock(siz) {
            clear();
        }

        int capacity() const { return pool.size(); }
        int size() const { return it - stock.begin(); }

        value_pointer_type alloc() {
            if constexpr (auto_extend) ensure();
            return *it++;
        }

        void free(value_pointer_type t) {
            *--it = t;
        }

        void clear() {
            int siz = pool.size();
            it = stock.begin();
            for (int i = 0; i < siz; i++) stock[i] = &pool[i];
        }

        void ensure() {
            if (it != stock.end()) return;
            int siz = stock.size();
            for (int i = siz; i <= siz * 2; ++i) {
                stock.push_back(&pool.emplace_back());
            }
            it = stock.begin() + siz;
        }
    };
} // namespace suisen


#line 7 "library/datastructure/segment_tree/persistent_commutative_dual_segment_tree.hpp"

namespace suisen {
    template <typename F, F(*composition)(F, F), F(*id)()>
    struct PersistentCommutativeDualSegmentTree {
        struct Node;

        using operator_type = F;

        using node_type = Node;
        using node_pointer_type = node_type*;

        struct Node {
            static inline ObjectPool<node_type> _pool;

            node_pointer_type _ch[2]{ nullptr, nullptr };
            operator_type _laz;

            Node() : _laz(id()) {}

            static node_pointer_type clone(node_pointer_type node) {
                return &(*_pool.alloc() = *node);
            }

            static node_pointer_type build(const int n) {
                auto rec = [&](auto rec, int l, int r) -> node_pointer_type {
                    node_pointer_type res = _pool.alloc();
                    res->_laz = id();
                    if (r - l > 1) {
                        int m = (l + r) >> 1;
                        res->_ch[0] = rec(rec, l, m), res->_ch[1] = rec(rec, m, r);
                    }
                    return res;
                };
                return rec(rec, 0, n);
            }

            static node_pointer_type apply_all(node_pointer_type node, const operator_type& f) {
                if (not node) return nullptr;
                node_pointer_type res = clone(node);
                res->_laz = composition(f, res->_laz);
                return res;
            }
            static node_pointer_type apply(node_pointer_type node, int tl, int tr, int ql, int qr, const operator_type& f) {
                if (tr <= ql or qr <= tl) return node;
                if (ql <= tl and tr <= qr) return apply_all(node, f);
                node_pointer_type res = clone(node);
                int tm = (tl + tr) >> 1;
                res->_ch[0] = apply(res->_ch[0], tl, tm, ql, qr, f);
                res->_ch[1] = apply(res->_ch[1], tm, tr, ql, qr, f);
                return res;
            }

            static operator_type get(node_pointer_type node, int siz, int i) {
                operator_type f = id();
                node_pointer_type cur = node;
                for (int l = 0, r = siz; r - l > 1;) {
                    f = composition(f, cur->_laz);
                    int m = (l + r) >> 1;
                    if (i < m) {
                        cur = cur->_ch[0];
                        r = m;
                    } else {
                        cur = cur->_ch[1];
                        l = m;
                    }
                }
                return composition(f, cur->_laz);
            }
        };

        PersistentCommutativeDualSegmentTree() : _n(0), _root(nullptr) {}
        explicit PersistentCommutativeDualSegmentTree(int n) : _n(n), _root(node_type::build(n)) {}

        static void init_pool(int siz) {
            node_type::_pool = ObjectPool<node_type>(siz);
        }
        static void clear_pool() {
            node_type::_pool.clear();
        }

        PersistentCommutativeDualSegmentTree apply_all(const operator_type& f) {
            return PersistentCommutativeDualSegmentTree(_n, node_type::apply_all(_root, f));
        }
        PersistentCommutativeDualSegmentTree apply(int l, int r, const operator_type& f) {
            return PersistentCommutativeDualSegmentTree(_n, node_type::apply(_root, 0, _n, l, r, f));
        }

        operator_type get(int i) {
            assert(0 <= i and i < _n);
            return node_type::get(_root, _n, i);
        }
        operator_type operator[](int i) {
            return get(i);
        }

    private:
        int _n;
        node_pointer_type _root;
        PersistentCommutativeDualSegmentTree(int n, node_pointer_type root) : _n(n), _root(root) {}
    };
}


#line 6 "test/src/datastructure/segment_tree/persistent_commutative_dual_segment_tree/abc253.test.cpp"

long long composition(long long f, long long g) {
    return f + g;
}
long long id() {
    return 0;
}
 
using Tree = suisen::PersistentCommutativeDualSegmentTree<long long, composition, id>;
 
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n, m, q;
    std::cin >> n >> m >> q;
 
    std::vector<std::pair<int, int>> last(n);
 
    Tree::init_pool(15000000);
 
    std::vector<Tree> segs(q + 1);
    segs[0] = Tree(m + 1);
 
    for (int t = 1; t <= q; ++t) {
        int query_type;
        std::cin >> query_type;
        segs[t] = segs[t - 1];
        if (query_type == 1) {
            int l, r, x;
            std::cin >> l >> r >> x;
            --l;
            segs[t] = segs[t].apply(l, r, x);
        } else if (query_type == 2) {
            int i, x;
            std::cin >> i >> x;
            --i;
            last[i] = { t - 1, x };
        } else {
            int i, j;
            std::cin >> i >> j;
            --i, --j;
            auto [tl, x] = last[i];
            std::cout << x + segs[t].get(j) - segs[tl].get(j) << '\n';
        }
    }
    return 0;
}
Back to top page