Skip to the content.

:heavy_check_mark: test/AOJ/DSL_2_E.test.cpp

Depends on

Code

#define PROBLEM                                                                \
    "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_E"
#include <bits/stdc++.h>

#include "library/algebra/lazy/AddMin.hpp"
#include "library/segtree/DualSegmentTree.hpp"

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

    int n, q;
    std::cin >> n >> q;
    DualSegmentTree<LazyAddMin<int>> seg(std::vector<int>(n, 0));
    while (q--) {
        int t;
        std::cin >> t;
        if (t) {
            int x;
            std::cin >> x;
            x--;
            std::cout << seg[x] << "\n";
        } else {
            int l, r, x;
            std::cin >> l >> r >> x;
            l--;
            seg.apply(l, r, x);
        }
    }
}
#line 1 "test/AOJ/DSL_2_E.test.cpp"
#define PROBLEM                                                                \
    "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_E"
#include <bits/stdc++.h>

#line 2 "library/algebra/group/Add.hpp"
template<typename X>
struct GroupAdd {
  using value_type = X;
  static constexpr X op(const X &x, const X &y) noexcept { return x + y; }
  static constexpr void Rchop(X&x, const X&y){ x+=y; }
  static constexpr void Lchop(const X&x, X&y){ y+=x; }
  static constexpr X inverse(const X &x) noexcept { return -x; }
  static constexpr X power(const X &x, long long n) noexcept { return X(n) * x; }
  static constexpr X unit() { return X(0); }
  static constexpr bool commute = true;
};
#line 1 "library/algebra/monoid/Min.hpp"
template <typename X> struct MonoidMin {
    using value_type = X;
    static constexpr X op(const X &x, const X &y) noexcept {
        return std::min(x, y);
    }
    static constexpr void Rchop(X &x, const X &y) {
        if (x > y)
            x = y;
    }
    static constexpr void Lchop(const X &x, X &y) {
        if (y > x)
            y = x;
    }
    static constexpr X unit() { return std::numeric_limits<X>::max() / 2; }
    static constexpr bool commute = true;
};
#line 4 "library/algebra/lazy/AddMin.hpp"
template <typename X> struct LazyAddMin {
    using MX = MonoidMin<X>;
    using MF = GroupAdd<X>;
    static constexpr X mapping(const X &f, const X &x) { return f + x; }
};
#line 1 "library/segtree/DualSegmentTree.hpp"
template <typename Lazy> class DualSegmentTree {
    using MX = typename Lazy::MX;
    using MF = typename Lazy::MF;
    using X = typename MX::value_type;
    using F = typename MF::value_type;
    int n, log, size;
    std::vector<X> dat;
    std::vector<F> laz;

    void point_apply(int k, const F &f) {
        if (k < size)
            MF::Lchop(f, laz[k]);
        else
            dat[k - size] = Lazy::mapping(f, dat[k - size]);
    }
    void push(int k) {
        point_apply(2 * k, laz[k]);
        point_apply(2 * k + 1, laz[k]);
        laz[k] = MF::unit();
    }
    void thrust(int k) {
        for (int i = log; i; i--)
            push(k >> i);
    }

  public:
    DualSegmentTree() : DualSegmentTree(0) {}
    DualSegmentTree(int n) : DualSegmentTree(std::vector<X>(n, MX::unit())) {}
    DualSegmentTree(const std::vector<X> &v) : n(v.size()), dat(v) {
        for (log = 1; (1 << log) < n; log++) {
        }
        size = 1 << log;
        laz.assign(size, MF::unit());
    }

    void set(int p, X x) {
        assert(0 <= p and p < n);
        thrust(p + size);
        dat[p] = x;
    }

    X operator[](int p) {
        assert(0 <= p and p < n);
        thrust(p + size);
        return dat[p];
    }

    void apply(int l, int r, F f) {
        assert(0 <= l && l <= r && r <= n);
        if (l == r)
            return;
        thrust(l += size);
        thrust(r += size - 1);
        for (int L = l, R = r + 1; L < R; L >>= 1, R >>= 1) {
            if (L & 1)
                point_apply(L++, f);
            if (R & 1)
                point_apply(--R, f);
        }
    }
};
#line 7 "test/AOJ/DSL_2_E.test.cpp"

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

    int n, q;
    std::cin >> n >> q;
    DualSegmentTree<LazyAddMin<int>> seg(std::vector<int>(n, 0));
    while (q--) {
        int t;
        std::cin >> t;
        if (t) {
            int x;
            std::cin >> x;
            x--;
            std::cout << seg[x] << "\n";
        } else {
            int l, r, x;
            std::cin >> l >> r >> x;
            l--;
            seg.apply(l, r, x);
        }
    }
}
Back to top page