CP-Algorithms Library

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

View the Project on GitHub cp-algorithms/cp-algorithms-aux

:heavy_check_mark: Point Add Range Sum (verify/structures/fenwick/point_add_range_sum.test.cpp)

Depends on

Code

// @brief Point Add Range Sum
#define PROBLEM "https://judge.yosupo.jp/problem/point_add_range_sum"
#pragma GCC optimize("Ofast,unroll-loops")
#include "cp-algo/structures/fenwick.hpp"
#include <bits/stdc++.h>

using namespace std;

void solve() {
    int n, q;
    cin >> n >> q;
    vector<int64_t> a(n + 1);
    for(auto &it: a | views::drop(1)) {cin >> it;}
    cp_algo::structures::fenwick<int64_t> me(move(a));
    for(int i = 0; i < q; i++) {
        int t, x, y;
        cin >> t >> x >> y;
        if(t == 0) {
            me.add(x, y);
        } else {
            cout << me.range_sum(x, y) << '\n';
        }
    }
}

signed main() {
    //freopen("input.txt", "r", stdin);
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t; 
    t = 1;// cin >> t;
    while(t--) {
        solve();
    }
}
#line 1 "verify/structures/fenwick/point_add_range_sum.test.cpp"
// @brief Point Add Range Sum
#define PROBLEM "https://judge.yosupo.jp/problem/point_add_range_sum"
#pragma GCC optimize("Ofast,unroll-loops")
#line 1 "cp-algo/structures/fenwick.hpp"


#include <cassert>
#include <vector>
namespace cp_algo::structures {
    template<typename T, typename Container = std::vector<T>>
    struct fenwick {
        size_t n;
        Container data;

        fenwick(auto &&range) {
            assign(move(range));
        }
        void to_prefix_sums() {
            for(size_t i = 1; i < n; i++) {
                if(i + (i & -i) <= n) {
                    data[i + (i & -i)] += data[i];
                }
            }
        }
        void assign(auto &&range) {
            n = size(range) - 1;
            data = move(range);
            to_prefix_sums();
        }
        void add(size_t x, T const& v) {
            for(++x; x <= n; x += x & -x) {
                data[x] += v;
            }
        }
        // sum of [0, r)
        T prefix_sum(size_t r) const {
            assert(r <= n);
            T res = 0;
            for(; r; r -= r & -r) {
                res += data[r];
            }
            return res;
        }
        // sum of [l, r)
        T range_sum(size_t l, size_t r) const {
            return prefix_sum(r) - prefix_sum(l);
        }
        // Last x s.t. k = prefix_sum(x) + r for r > 0
        // Assumes data[x] >= 0 for all x, returns [x, r]
        auto prefix_lower_bound(T k) const {
            int x = 0;
            for(size_t i = std::bit_floor(n); i; i /= 2) {
                if(x + i <= n && data[x + i] < k) {
                    k -= data[x + i];
                    x += i;
                }
            }
            return std::pair{x, k};
        }
    };
}

#line 5 "verify/structures/fenwick/point_add_range_sum.test.cpp"
#include <bits/stdc++.h>

using namespace std;

void solve() {
    int n, q;
    cin >> n >> q;
    vector<int64_t> a(n + 1);
    for(auto &it: a | views::drop(1)) {cin >> it;}
    cp_algo::structures::fenwick<int64_t> me(move(a));
    for(int i = 0; i < q; i++) {
        int t, x, y;
        cin >> t >> x >> y;
        if(t == 0) {
            me.add(x, y);
        } else {
            cout << me.range_sum(x, y) << '\n';
        }
    }
}

signed main() {
    //freopen("input.txt", "r", stdin);
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t; 
    t = 1;// cin >> t;
    while(t--) {
        solve();
    }
}

Test cases

Env Name Status Elapsed Memory
g++ example_00 :heavy_check_mark: AC 5 ms 4 MB
g++ max_random_00 :heavy_check_mark: AC 139 ms 7 MB
g++ max_random_01 :heavy_check_mark: AC 141 ms 7 MB
g++ max_random_02 :heavy_check_mark: AC 146 ms 7 MB
g++ max_random_03 :heavy_check_mark: AC 146 ms 7 MB
g++ max_random_04 :heavy_check_mark: AC 142 ms 7 MB
g++ random_00 :heavy_check_mark: AC 115 ms 6 MB
g++ random_01 :heavy_check_mark: AC 124 ms 7 MB
g++ random_02 :heavy_check_mark: AC 88 ms 4 MB
g++ random_03 :heavy_check_mark: AC 36 ms 7 MB
g++ random_04 :heavy_check_mark: AC 42 ms 5 MB
g++ small_00 :heavy_check_mark: AC 6 ms 4 MB
g++ small_01 :heavy_check_mark: AC 5 ms 4 MB
g++ small_02 :heavy_check_mark: AC 5 ms 4 MB
g++ small_03 :heavy_check_mark: AC 5 ms 4 MB
g++ small_04 :heavy_check_mark: AC 5 ms 4 MB
g++ small_05 :heavy_check_mark: AC 5 ms 4 MB
g++ small_06 :heavy_check_mark: AC 5 ms 4 MB
g++ small_07 :heavy_check_mark: AC 5 ms 4 MB
g++ small_08 :heavy_check_mark: AC 5 ms 4 MB
g++ small_09 :heavy_check_mark: AC 5 ms 4 MB
Back to top page