Cod sursa(job #651566)

Utilizator repp4raduRadu-Andrei Szasz repp4radu Data 20 decembrie 2011 20:40:04
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <fstream>
#define function(x) ((x) & (-x))

using namespace std;

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

int s[15001];
int n ,m , command, firstValue, secondValue;

void addAndRemove(int position, int value)
{
    for(int i = position; i <= n; i+= function(i))
    {
        s[i] += value;
    }
}

void citire()
{
    in>>n>>m;
    int a;
    for(int i = 1; i <= n; i++)
    {
        in>>a;
        addAndRemove(i, a);
    }
}

int getSume(int a)
{
    int sume = 0;
    for(int i = a; i >= 1; i-= function(i))
    {
        sume += s[i];
    }
    return sume;
}

void resolve()
{
    for(int i = 1; i <= m; i++)
    {
        in>>command>>firstValue>>secondValue;
        if(command)
        {
            int a = getSume(secondValue);
            int b = getSume(firstValue - 1);
            out<<a - b<<'\n';
        }
        else
        {
            addAndRemove(firstValue, -secondValue);
        }
    }
}

int main()
{
    citire();

    resolve();
}