proconlib

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

View the Project on GitHub KodamaD/proconlib

:heavy_check_mark: test/fenwick_tree.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/point_add_range_sum"
#include <iostream>
#include "../container/fenwick_tree.cpp"
#include "../traits/sum_group.cpp"
#include "../utility/int_alias.cpp"
#include "../utility/rep.cpp"

int main() {
    int N, Q;
    std::cin >> N >> Q;
    FenwickTree<SumGroup<u64>> fen(N);
    for (const int i : rep(0, N)) {
        u64 x;
        std::cin >> x;
        fen.add(i, x);
    }
    while (Q--) {
        int t;
        std::cin >> t;
        if (t == 0) {
            int p;
            u64 x;
            std::cin >> p >> x;
            fen.add(p, x);
        } else {
            int l, r;
            std::cin >> l >> r;
            std::cout << fen.fold(l, r) << '\n';
        }
    }
    return 0;
}
#line 1 "test/fenwick_tree.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/point_add_range_sum"
#include <iostream>
#line 2 "container/fenwick_tree.cpp"
#include <cassert>
#include <vector>
#line 2 "internal/enable_avx2.cpp"

#ifdef ENABLE_AVX2
#define TARGET_AVX2 __attribute__((target("avx2")))
#else
#define TARGET_AVX2
#endif
#line 2 "utility/int_alias.cpp"
#include <cstdint>

using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
#line 4 "utility/countl_zero.cpp"

TARGET_AVX2 constexpr int countl_zero(u64 x) {
#ifdef __GNUC__
    return x == 0 ? 64 : __builtin_clzll(x);
#else
    x |= x >> 1;
    x |= x >> 2;
    x |= x >> 4;
    x |= x >> 8;
    x |= x >> 16;
    x |= x >> 32;
    return 64 - countr_zero(~x);
#endif
}
#line 4 "utility/bit_width.cpp"

TARGET_AVX2 constexpr int bit_width(const u64 x) { return 64 - countl_zero(x); }
#line 5 "utility/ceil_log2.cpp"

TARGET_AVX2 constexpr int ceil_log2(const u64 x) {
#ifdef __GNUC__
    return x == 0 ? 0 : bit_width(x - 1);
#else
    int e = 0;
    while (((u64)1 << e) < x) ++e;
    return e;
#endif
}
#line 5 "container/fenwick_tree.cpp"

template <class G> class FenwickTree {
    using T = typename G::Type;

    int logn;
    std::vector<T> data;

  public:
    explicit FenwickTree(const int size = 0) {
        logn = ceil_log2(size + 1) - 1;
        data = std::vector<T>(size + 1, G::identity());
    }

    int size() const { return data.size() - 1; }

    void add(int i, const T& x) {
        assert(0 <= i and i < size());
        i += 1;
        while (i <= size()) {
            data[i] = G::operation(data[i], x);
            i += i & -i;
        }
    }

    T fold() const { return fold(0, size()); }
    T fold(int l, int r) const {
        assert(0 <= l and l <= r and r <= size());
        T ret = G::identity();
        while (l < r) {
            ret = G::operation(ret, data[r]);
            r -= r & -r;
        }
        while (r < l) {
            ret = G::operation(ret, G::inverse(data[l]));
            l -= l & -l;
        }
        return ret;
    }

    template <class F> int max_right(const F& f) const {
        assert(f(G::identity()));
        int i = 0;
        T sum = G::identity();
        for (int k = (1 << logn); k > 0; k >>= 1) {
            if (i + k <= size() && f(G::operation(sum, data[i + k]))) {
                sum = G::operation(sum, data[i += k]);
            }
        }
        return i;
    }
};
#line 2 "traits/sum_group.cpp"

template <class T> struct SumGroup {
    using Type = T;
    static constexpr T identity() { return T(0); }
    static constexpr T operation(const T& l, const T& r) { return l + r; }
    static constexpr T inverse(const T& x) { return -x; }
};
#line 2 "utility/rep.cpp"
#include <algorithm>

class Range {
    struct Iter {
        int itr;
        constexpr Iter(const int pos) noexcept : itr(pos) {}
        constexpr void operator++() noexcept { ++itr; }
        constexpr bool operator!=(const Iter& other) const noexcept { return itr != other.itr; }
        constexpr int operator*() const noexcept { return itr; }
    };
    const Iter first, last;

  public:
    explicit constexpr Range(const int first, const int last) noexcept : first(first), last(std::max(first, last)) {}
    constexpr Iter begin() const noexcept { return first; }
    constexpr Iter end() const noexcept { return last; }
};

constexpr Range rep(const int l, const int r) noexcept { return Range(l, r); }
constexpr Range rep(const int n) noexcept { return Range(0, n); }
#line 7 "test/fenwick_tree.test.cpp"

int main() {
    int N, Q;
    std::cin >> N >> Q;
    FenwickTree<SumGroup<u64>> fen(N);
    for (const int i : rep(0, N)) {
        u64 x;
        std::cin >> x;
        fen.add(i, x);
    }
    while (Q--) {
        int t;
        std::cin >> t;
        if (t == 0) {
            int p;
            u64 x;
            std::cin >> p >> x;
            fen.add(p, x);
        } else {
            int l, r;
            std::cin >> l >> r;
            std::cout << fen.fold(l, r) << '\n';
        }
    }
    return 0;
}
Back to top page