Cod sursa(job #2379056)

Utilizator RaresLiscanLiscan Rares RaresLiscan Data 12 martie 2019 20:51:59
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <bits/stdc++.h>
#define MAX 15005

using namespace std;

ifstream fin ("datorii.in");
ofstream fout ("datorii.out");

int n, m;
int a, b;

int aib[MAX];

void update (int pos, int val)
{
    while (pos <= n) {
        aib[pos] += val;
        pos += (pos & (-pos));
    }
}

int read(int pos)
{
    int sum = 0;
    while (pos) {
        sum += aib[pos];
        pos -= (pos & (-pos));
    }
    return sum;
}

int main()
{
    fin >> n >> m;
    for (int i = 1; i <= n; i ++) {
        fin >> a;
        update(i, a);
    }
    bool test;
    for (int i = 1; i <= m; i ++) {
        fin >> test >> a >> b;
        if (test) fout << read(b) - read(a - 1) << "\n";
        else update(a, -b);
    }
    return 0;
}