ku-library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub kogetsu0728/ku-library

:heavy_check_mark: test/data_structure/fenwick_tree.test.cpp

Depends on

Required by

Code

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

#include "../../data_structure/fenwick_tree.hpp"
#include "../../template/template.hpp"

int main() {
    int N, Q;
    cin >> N >> Q;

    vector<ll> A(N);
    rep (i, 0, N) {
        cin >> A[i];
    }

    FenwickTree<ll> fw1(N);
    FenwickTree<ll> fw2(N);
    FenwickTree<ll> fw3(A);
    rep (i, 0, N) {
        fw1.add(i, A[i]);
        fw2.set(i, A[i]);
    }

    while (Q--) {
        int l, r;
        cin >> l >> r;

        ll s1 = fw1.sum(l, r);
        ll s2 = fw2.sum(l, r);
        ll s3 = fw3.sum(l, r);

        assert(s1 == s2);
        assert(s2 == s3);

        cout << s1 << LF;
    }

    return 0;
}
#line 1 "test/data_structure/fenwick_tree.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/static_range_sum"

#line 2 "data_structure/fenwick_tree.hpp"

#line 2 "template/template.hpp"

/**
 * @author ku_senjan
 * @title 提出用テンプレート
 * @see https://github.com/kogetsu0728/ku-library
 */

#line 2 "template/constant.hpp"

#line 2 "template/include.hpp"

#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>
using namespace std;
#line 2 "template/type_alias.hpp"

#line 4 "template/type_alias.hpp"

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;

using ld = long double;

template <class T, bool REVERSE = false>
using heap = priority_queue<T, vector<T>, conditional_t<REVERSE, greater<T>, less<T>>>;
#line 5 "template/constant.hpp"

template <class T>
inline constexpr T INF = numeric_limits<T>::max() / 2;

inline constexpr array<int, 4> DY4 = {0, -1, 0, 1};
inline constexpr array<int, 4> DX4 = {1, 0, -1, 0};
inline constexpr array<int, 8> DY8 = {0, -1, -1, -1, 0, 1, 1, 1};
inline constexpr array<int, 8> DX8 = {1, 1, 0, -1, -1, -1, 0, 1};

inline constexpr char LF = '\n';
#line 2 "template/macro.hpp"

#line 5 "template/macro.hpp"

/**
 * @see https://trap.jp/post/1224/
 */

#ifdef LOCAL
inline constexpr bool IS_LOCAL = true;
#else
inline constexpr bool IS_LOCAL = false;
#endif

#define IF_LOCAL if constexpr (IS_LOCAL)

#define all(a) begin(a), end(a)
#define rall(a) rbegin(a), rend(a)

#define overload4(a, b, c, d, e, ...) e

#define rep1(i, a) for (ll i = 0; (i) < (ll)(a); ++(i))
#define rep2(i, a, b) for (ll i = (ll)(a); (i) < (ll)(b); ++(i))
#define rep3(i, a, b, c) for (ll i = (ll)(a); (i) < (ll)(b); (i) += (ll)(c))
#define rep(...) overload4(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)

#define rrep1(i, a) for (ll i = (ll)(a); (i) >= 0; --(i))
#define rrep2(i, a, b) for (ll i = (ll)(a); (i) >= (ll)(b); --(i))
#define rrep3(i, a, b, c) for (ll i = (ll)(a); (i) >= (ll)(b); (i) -= (ll)(c))
#define rrep(...) overload4(__VA_ARGS__, rrep3, rrep2, rrep1)(__VA_ARGS__)
#line 13 "template/template.hpp"

#line 2 "utility/choose_min_max.hpp"

/**
 * @title Choose Minimum / Maximum
 */

template <class T>
bool chmin(T& a, const T& b) {
    if (a > b) {
        a = b;
        return true;
    }

    return false;
}

template <class T>
bool chmax(T& a, const T& b) {
    if (a < b) {
        a = b;
        return true;
    }

    return false;
}
#line 2 "utility/set_io.hpp"

#line 4 "utility/set_io.hpp"

void set_io(int d = 16) {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(d);

    return;
}
#line 4 "data_structure/fenwick_tree.hpp"

/**
 * @brief Fenwick Tree
 */
template <class T>
class FenwickTree {
  private:
    int n;
    vector<T> v;

    T sum(int r) const {
        assert(0 <= r && r <= n);

        T res = T(0);
        while (0 < r) {
            res += v[r - 1];
            r -= 1 << __builtin_ctz(r);
        }

        return res;
    }

  public:
    FenwickTree() : FenwickTree(0) {}
    FenwickTree(int _n) : n(_n), v(_n, T(0)) { assert(0 <= n); }
    FenwickTree(const vector<T>& _v) : FenwickTree(_v.size()) {
        rep (i, 0, _v.size()) {
            set(i, _v[i]);
        }
    }

    T sum(int l, int r) const {
        assert(0 <= l && l <= r && r <= n);

        return sum(r) - sum(l);
    }

    T get(int i) const {
        assert(0 <= i && i < n);

        return sum(i, i + 1);
    }

    void add(int i, const T& x) {
        assert(0 <= i && i < n);

        i++;
        while (i <= n) {
            v[i - 1] += x;
            i += 1 << __builtin_ctz(i);
        }

        return;
    }

    void set(int i, const T& x) {
        assert(0 <= i && i < n);

        add(i, x - get(i));

        return;
    }
};
#line 5 "test/data_structure/fenwick_tree.test.cpp"

int main() {
    int N, Q;
    cin >> N >> Q;

    vector<ll> A(N);
    rep (i, 0, N) {
        cin >> A[i];
    }

    FenwickTree<ll> fw1(N);
    FenwickTree<ll> fw2(N);
    FenwickTree<ll> fw3(A);
    rep (i, 0, N) {
        fw1.add(i, A[i]);
        fw2.set(i, A[i]);
    }

    while (Q--) {
        int l, r;
        cin >> l >> r;

        ll s1 = fw1.sum(l, r);
        ll s2 = fw2.sum(l, r);
        ll s3 = fw3.sum(l, r);

        assert(s1 == s2);
        assert(s2 == s3);

        cout << s1 << LF;
    }

    return 0;
}

Test cases

Env Name Status Elapsed Memory
g++ example_00 :heavy_check_mark: AC 6 ms 3 MB
g++ max_random_00 :heavy_check_mark: AC 1050 ms 19 MB
g++ max_random_01 :heavy_check_mark: AC 1001 ms 19 MB
g++ max_random_02 :heavy_check_mark: AC 1078 ms 19 MB
g++ max_random_03 :heavy_check_mark: AC 1012 ms 19 MB
g++ max_random_04 :heavy_check_mark: AC 998 ms 19 MB
g++ random_00 :heavy_check_mark: AC 826 ms 15 MB
g++ random_01 :heavy_check_mark: AC 859 ms 18 MB
g++ random_02 :heavy_check_mark: AC 529 ms 5 MB
g++ random_03 :heavy_check_mark: AC 227 ms 17 MB
g++ random_04 :heavy_check_mark: AC 298 ms 12 MB
Back to top page