raybbian's CP Algos

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

View the Project on GitHub raybbian/comp-programming

:heavy_check_mark: verify/ds/fenwick.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/point_add_range_sum"
#include "algo/common.h"

/* #include */
#include "algo/ds/fenwick.h"

using namespace std;
using namespace algo;
using ds::fenwick;

void solve() {
    int n, q;
    cin >> n >> q;
    vector<int64_t> a(n);
    for (int i = 0; i < n; i++)
        cin >> a[i];
    fenwick<int64_t> t(a);
    for (int i = 0; i < q; i++) {
        int typ, x, y;
        cin >> typ >> x >> y;
        if (typ == 0) {
            t.add(x, y);
        } else {
            cout << t.sum(x, y - 1) << '\n';
        }
    }
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    // int t;
    // cin >> t;
    // while (t--)
    solve();
}
#line 1 "verify/ds/fenwick.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/point_add_range_sum"
#line 2 "algo/common.h"
#ifndef PREPROCESS
#include <bits/stdc++.h>
#endif

namespace algo {}

#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#line 3 "verify/ds/fenwick.test.cpp"

/* #include */
#line 3 "algo/ds/fenwick.h"

namespace algo::ds {

template <typename T>
struct fenwick {
    fenwick(int _n) : n(_n), bit(n, 0) {
    }
    fenwick(const std::vector<T> &a) : fenwick(sz(a)) {
        for (int i = 0; i < n; i++) {
            bit[i] += a[i];
            int r = i | (i + 1);
            if (r < n) bit[r] += bit[i];
        }
    }
    // Inclusive on [l, r]
    T sum(int l, int r) {
        return sum(r) - sum(l - 1);
    }
    T val(int pos) {
        return sum(pos, pos);
    }
    void add(int idx, int delta) {
        for (; idx < n; idx = idx | (idx + 1))
            bit[idx] += delta;
    }
    friend std::ostream &operator<<(std::ostream &os, fenwick f) {
        os << "[";
        bool first = true;
        for (int i = 0; i < f.n; i++) {
            if (!first) os << ", ";
            first = false;
            os << f.val(i);
        }
        os << "]";
        return os;
    }

private:
    int n;
    std::vector<T> bit;
    T sum(int r) {
        T ret(0);
        for (; r >= 0; r = (r & (r + 1)) - 1)
            ret += bit[r];
        return ret;
    }
};

} // namespace algo::ds
#line 6 "verify/ds/fenwick.test.cpp"

using namespace std;
using namespace algo;
using ds::fenwick;

void solve() {
    int n, q;
    cin >> n >> q;
    vector<int64_t> a(n);
    for (int i = 0; i < n; i++)
        cin >> a[i];
    fenwick<int64_t> t(a);
    for (int i = 0; i < q; i++) {
        int typ, x, y;
        cin >> typ >> x >> y;
        if (typ == 0) {
            t.add(x, y);
        } else {
            cout << t.sum(x, y - 1) << '\n';
        }
    }
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    // int t;
    // cin >> t;
    // while (t--)
    solve();
}

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 137 ms 11 MB
g++ max_random_01 :heavy_check_mark: AC 139 ms 11 MB
g++ max_random_02 :heavy_check_mark: AC 133 ms 11 MB
g++ max_random_03 :heavy_check_mark: AC 138 ms 11 MB
g++ max_random_04 :heavy_check_mark: AC 134 ms 11 MB
g++ random_00 :heavy_check_mark: AC 114 ms 9 MB
g++ random_01 :heavy_check_mark: AC 118 ms 10 MB
g++ random_02 :heavy_check_mark: AC 78 ms 4 MB
g++ random_03 :heavy_check_mark: AC 35 ms 10 MB
g++ random_04 :heavy_check_mark: AC 43 ms 7 MB
g++ small_00 :heavy_check_mark: AC 6 ms 3 MB
g++ small_01 :heavy_check_mark: AC 6 ms 3 MB
g++ small_02 :heavy_check_mark: AC 6 ms 3 MB
g++ small_03 :heavy_check_mark: AC 6 ms 3 MB
g++ small_04 :heavy_check_mark: AC 5 ms 3 MB
g++ small_05 :heavy_check_mark: AC 6 ms 3 MB
g++ small_06 :heavy_check_mark: AC 6 ms 3 MB
g++ small_07 :heavy_check_mark: AC 6 ms 3 MB
g++ small_08 :heavy_check_mark: AC 6 ms 3 MB
g++ small_09 :heavy_check_mark: AC 6 ms 3 MB
Back to top page