Cod sursa(job #2953937)

Utilizator Pop_EmilPal Tamas Pop_Emil Data 12 decembrie 2022 18:03:12
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <iostream>
using namespace std;

int N, M, BIT[15005];
FILE *in = fopen("datorii.in", "r"), *out = fopen("datorii.out", "w");

void update(int pos, int val){
    for(int i = pos; i <= N; i += i & (-i))
        BIT[i] += val;
}

int query(int pos){
    int sum = 0;
    for(int i = pos; i >= 1; i -= i & (-i))
        sum += BIT[i];
    return sum;
}

int main()
{
    fscanf(in, "%d %d", &N, &M);
    int num;
    for(int i = 1; i <= N; ++i){
        fscanf(in, "%d", &num);
        update(i, num);
    }

    int type, a, b;
    for(int j = 1; j <= M; ++j){
        fscanf(in, "%d %d %d", &type, &a, &b);
        if (type == 0){
            update(a, -b);
        }
        else if (type == 1){
            fprintf(out, "%d\n", query(b) - query(a - 1));
        }
    }

    return 0;
}