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

Range Update Queries

Правка en1, от Aris12345, 2020-09-11 14:57:48

Hello, I am trying to solve this problem: https://cses.fi/problemset/task/1651. I don't know where my code is wrong, can you help me.

here is my code:

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

const int MAXN = 1000005;

int n, q, arr[MAXN], bit[MAXN];

void update(int x, int y) {
    for (; x <= n; x += x&-x) bit[x] += y;
}

int query(int x) {
    int ans = 0;
    for (; x > 0; x -= x&-x) ans += bit[x];
    return ans;
}

void range(int x, int y, int z) {
    update(x, z);
    update(y+1, -z);
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    cin >> n >> q;
    for (int i = 1; i <= n; i++) {
        cin >> arr[i];
        update(i, arr[i]);
    }
    while (q--) {
        int x, y, z, w;
        cin >> x;
        if (x == 1) { cin >> y >> z >> w; range(y, z, w); }
        else { cin >> y; cout << query(y)-query(y-1) << "\n"; }
    }
    return 0;
}

Thanks for your time!!!

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en1 Английский Aris12345 2020-09-11 14:57:48 985 Initial revision (published)