Kuro-orzz's Library

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

View on GitHub

:heavy_check_mark: DataStructure/Static_range_sum.test.cpp

Depends on

Code

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

#include "../template.h"
#include "Fenwick/Fenwick.h"

void solve() {
    int n, q; cin >> n >> q;
    Fenwick<ll> BIT(n);
    for (int i = 1; i <= n; i++) {
        int x; cin >> x;
        BIT.update(i, x);
    }
    while (q--) {
        int l, r; cin >> l >> r;
        cout << BIT.get(l+1, r) << '\n';
    }
}
#line 1 "DataStructure/Static_range_sum.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/static_range_sum"

#line 2 "template.h"

#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
#define MOD (ll)(1e9+7)
#define all(x) (x).begin(),(x).end()
#define unique(x) x.erase(unique(all(x)), x.end())
#define INF32 ((1ull<<31)-1)
#define INF64 ((1ull<<63)-1)
#define inf (ll)1e18

#define vi vector<int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second

const int mod = 998244353;

void solve();

int main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    // cin.exceptions(cin.failbit);
    // int t; cin >> t;
    // while(t--)
        solve();
    cerr << "\nTime run: " << 1000 * clock() / CLOCKS_PER_SEC << "ms" << '\n';
    return 0;
}
#line 2 "DataStructure/Fenwick/Fenwick.h"

template <typename T>
struct Fenwick{
    int n;
    vector<T> fen;

    Fenwick() {}
    Fenwick(int _n): n(_n), fen(_n+1) {}

    void update(int pos, T val) {
        for (; pos <= n; pos += pos & -pos) {
            fen[pos] += val;
        }
    }

    T get(int pos) {
        T ans = 0;
        for (; pos > 0; pos -= pos & -pos) {
            ans += fen[pos];
        }
        return ans;
    }

    T get(int l, int r) {
        return get(r) - get(l - 1);
    }

    void update_range(int l, int r, T val){
        update(l, val);
        update(r+1, -val);
    }

    void reset() {
        fill(all(fen), T(0));
    }
};
#line 5 "DataStructure/Static_range_sum.test.cpp"

void solve() {
    int n, q; cin >> n >> q;
    Fenwick<ll> BIT(n);
    for (int i = 1; i <= n; i++) {
        int x; cin >> x;
        BIT.update(i, x);
    }
    while (q--) {
        int l, r; cin >> l >> r;
        cout << BIT.get(l+1, r) << '\n';
    }
}
Back to top page