I am trying to solve this problem: https://cses.fi/problemset/task/1648/ using Binary Indexed Tree. My solution does not work, but I think it is correct.
Heres is my code:
include <bits/stdc++.h>
using namespace std;
const long MAXN = 2e5+5;
long n, q, arr[MAXN]; long long bit[MAXN];
void update(long x, long y) { for (; x <= n; x += x&-x) bit[x] += y; }
long long query(long x) { long long sum = 0; for (; x > 0; x -= x&-x) sum += bit[x]; return sum; }
long long sum(long l, long r) { return query(r) — query(l-1); }
int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> q; for (long i = 1; i <= n; i++) { cin >> arr[i]; update(i, arr[i]); } while (q--) { long i, a, b; cin >> i >> a >> b; if (i == 1) update(a, b-bit[a]); else cout << sum(a, b) << "\n"; } return 0; }
Thanks for your time!!!