Cod sursa(job #1838715)

Utilizator BLz0rDospra Cristian BLz0r Data 1 ianuarie 2017 17:03:09
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.2 kb
#include <fstream>
using namespace std;

#define Nmax ( 1 << 14 ) + 1
#define LSB(x) ( x & ( -x ) )

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

int aib[Nmax], aibSize = 1;

// facute special 2 functii la care doar difera semnul pentru a evidentia rezolvarea problemei

// update pe pozitie cu +
inline void Adauga(int poz, int val) {

    for (int i = poz; i <= aibSize; i += LSB(i))
        aib[i] += val;
}

// update pe pozitie cu -
inline void Plateste(int poz, int val) {

    for (int i = poz; i <= aibSize; i += LSB(i))
        aib[i] -= val;
}

inline int Sum(int x) {

    int s = 0;

    while (x) {
        s += aib[x];
        x -= LSB(x);
    }

    return s;
}

inline int Query(int st, int dr) {

    return Sum(dr) - Sum(st - 1);
}

int main() {

    int N, M, x, a, b;

    // citire
    fin >> N >> M;

    while (aibSize < N)
        aibSize <<= 1;

    for (int i = 1; i <= N; ++i) {
        fin >> x;
        Adauga(i, x);
    }

    // rezolvare
    while (M--) {

        fin >> x >> a >> b;

        if (x == 0)
            Plateste(a, b);
        else
            fout << Query(a, b) << "\n";
    }

    return 0;
}