Cod sursa(job #1447524)

Utilizator florinasAsavei florinas Data 4 iunie 2015 17:25:42
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <fstream>
using namespace std;
ifstream fin ("datorii.in");
ofstream fout ("datorii.out");
int N, M, tip, a, b, AIB[15010];

int zeros(int x)
{
    return ((x ^ (x - 1)) & x);
}

void Update(int poz, int val, int semn)
{
    while (poz <= N)
    {
        AIB[poz] += val * semn;
        poz += zeros(poz);
    }
}

int Query(int poz, int sum = 0)
{
    while (poz)
    {
        sum += AIB[poz];
        poz -= zeros(poz);
    }

    return sum;
}

int main()
{
    fin >> N >> M;

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

    for (int i = 1; i <= M; i++) {
        fin >> tip >> a >> b;

        if (!tip) { Update(a, b, -1); }
        else { fout << Query(b) - Query(a - 1) << '\n'; }
    }

    fout.close();
    return 0;
}