icpc_library

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

View the Project on GitHub ebi-fly13/icpc_library

:heavy_check_mark: dual segtree
(data_structure/dualsegtree.hpp)

説明

モノイドの列$(a_0,a_1,\dots,a_{n-1})$に対して区間作用、 $1$ 点更新。

コンストラクタ

dualsegtree<S, op, e>(int n) dualsegtree<S, op, e>(const std::vector<S> &v)

に対する双対セグ木を構築する。要素数を渡す場合、 std::vector<S>(n, e()) に対して双対セグ木を構築する。Range Add Point Getの場合次のようになる。計算量 $O(n)$

int op(int a, int b) { 
    return a+b; 
}

int e() { 
    return 0; 
}

dualsegtree<int, op, e> seg(n);

get(int p)

v[p] を返す。

apply(int l, int r, S s)

区間 $[l, r)$ に s を作用させる。

Verified with

Code

#pragma once

#include <cassert>
#include <vector>

namespace lib {

template <class S, S (*op)(S, S), S (*e)()>
struct dualsegtree {
  public:
    dualsegtree(int n) : n(n) {
        sz = 1;
        while (sz < n) sz <<= 1;
        data.assign(2 * sz, e());
    }

    dualsegtree(const std::vector<S> &vec) : n(vec.size()) {
        sz = 1;
        while (sz < n) sz <<= 1;
        data.assign(2 * sz, e());
        std::copy(vec.begin(), vec.end(), data.begin() + sz);
    }

    S get(int p) const {
        assert(0 <= p && p < n);
        p += sz;
        S val = e();
        while (p > 0) {
            val = op(val, data[p]);
            p >>= 1;
        }
        return val;
    }

    void apply(int l, int r, S x) {
        assert(0 <= l && l <= r && r <= n);
        l += sz;
        r += sz;
        while (l < r) {
            if (l & 1) {
                data[l] = op(data[l], x);
                l++;
            }
            if (r & 1) {
                r--;
                data[r] = op(data[r], x);
            }
            l >>= 1;
            r >>= 1;
        }
        return;
    }

  private:
    std::vector<S> data;
    int n;
    int sz;
};

}  // namespace ebi
#line 2 "data_structure/dualsegtree.hpp"

#include <cassert>
#include <vector>

namespace lib {

template <class S, S (*op)(S, S), S (*e)()>
struct dualsegtree {
  public:
    dualsegtree(int n) : n(n) {
        sz = 1;
        while (sz < n) sz <<= 1;
        data.assign(2 * sz, e());
    }

    dualsegtree(const std::vector<S> &vec) : n(vec.size()) {
        sz = 1;
        while (sz < n) sz <<= 1;
        data.assign(2 * sz, e());
        std::copy(vec.begin(), vec.end(), data.begin() + sz);
    }

    S get(int p) const {
        assert(0 <= p && p < n);
        p += sz;
        S val = e();
        while (p > 0) {
            val = op(val, data[p]);
            p >>= 1;
        }
        return val;
    }

    void apply(int l, int r, S x) {
        assert(0 <= l && l <= r && r <= n);
        l += sz;
        r += sz;
        while (l < r) {
            if (l & 1) {
                data[l] = op(data[l], x);
                l++;
            }
            if (r & 1) {
                r--;
                data[r] = op(data[r], x);
            }
            l >>= 1;
            r >>= 1;
        }
        return;
    }

  private:
    std::vector<S> data;
    int n;
    int sz;
};

}  // namespace ebi
Back to top page