Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Range Sum Queries II

Правка en3, от Aris12345, 2020-09-10 11:01:23

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!!!

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en3 Английский Aris12345 2020-09-10 11:01:23 24
en2 Английский Aris12345 2020-09-10 10:57:35 39
en1 Английский Aris12345 2020-09-10 10:56:31 1009 Initial revision (published)