Submission #45974331


Source Code Expand

#include <algorithm>
#include <vector>

// CUT begin
// 0-indexed BIT (binary indexed tree / Fenwick tree) (i : [0, len))
template <class T> struct BIT {
    int n;
    std::vector<T> data;
    BIT(int len = 0) : n(len), data(len) {}
    BIT(const std::vector<T> &v) : n(v.size()), data(v.size()) {
        T p = 0;
        for (int i = 0; i < n; i++) {
            data[i] = p += v[i];
        }
        for (int j = n - 1; j >= 0; j--) {
            const int i = j & (j + 1);
            if (i > 0) data[j] -= data[i - 1];
        }
    }
    void reset() { std::fill(data.begin(), data.end(), T(0)); }
    void add(int pos, T v) { // a[pos] += v
        pos++;
        while (pos > 0 and pos <= n) data[pos - 1] += v, pos += pos & -pos;
    }
    T sum(int k) const { // a[0] + ... + a[k - 1]
        T res = 0;
        while (k > 0) res += data[k - 1], k -= k & -k;
        return res;
    }

    T sum(int l, int r) const { return sum(r) - sum(l); } // a[l] + ... + a[r - 1]

    template <class OStream> friend OStream &operator<<(OStream &os, const BIT &bit) {
        T prv = 0;
        os << '[';
        for (int i = 1; i <= bit.n; i++) {
            T now = bit.sum(i);
            os << now - prv << ',', prv = now;
        }
        return os << ']';
    }
};

#include <bits/stdc++.h>
using namespace std;

static void __solve__();

int main() {
    int t = 1;
    // cin >> t;
    while (t--) __solve__();
    return 0;
}

/*

################################################################
#                                                              #
#              https://github.com/alantudyk/Stop               #
#                                                              #
################################################################

*/

static void __solve__() {
    int n, q;
    cin >> n >> q;
    vector<int64_t> v(n);
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        v[i] = x;
    }
    BIT<int64_t> b(v);
    while (q--) {
        int t;
        cin >> t;
        if (t == 0) {
            int p, x;
            cin >> p >> x;
            b.add(p, x);
        } else {
            int l, r;
            cin >> l >> r;
            cout << b.sum(l, r) << '\n';
        }
    }
}

/*

################################################################
#                                                              #
#              https://github.com/alantudyk/Stop               #
#                                                              #
################################################################

*/

Submission Info

Submission Time
Task B - Fenwick Tree
User Reznov
Language C++ 20 (gcc 12.2)
Score 100
Code Size 2679 Byte
Status AC
Exec Time 635 ms
Memory 12056 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 1
AC × 21
Set Name Test Cases
Sample example_00
All example_00, max_random_00, max_random_01, max_random_02, max_random_03, max_random_04, random_00, random_01, random_02, random_03, random_04, small_00, small_01, small_02, small_03, small_04, small_05, small_06, small_07, small_08, small_09
Case Name Status Exec Time Memory
example_00 AC 1 ms 3520 KiB
max_random_00 AC 630 ms 12040 KiB
max_random_01 AC 635 ms 12044 KiB
max_random_02 AC 627 ms 12056 KiB
max_random_03 AC 626 ms 12048 KiB
max_random_04 AC 630 ms 12044 KiB
random_00 AC 511 ms 9652 KiB
random_01 AC 527 ms 10856 KiB
random_02 AC 382 ms 4032 KiB
random_03 AC 132 ms 9720 KiB
random_04 AC 167 ms 7392 KiB
small_00 AC 2 ms 3468 KiB
small_01 AC 2 ms 3456 KiB
small_02 AC 2 ms 3508 KiB
small_03 AC 2 ms 3656 KiB
small_04 AC 2 ms 3656 KiB
small_05 AC 2 ms 3464 KiB
small_06 AC 2 ms 3572 KiB
small_07 AC 2 ms 3656 KiB
small_08 AC 2 ms 3504 KiB
small_09 AC 2 ms 3472 KiB