Cod sursa(job #1148431)

Utilizator vyrtusRadu Criuleni vyrtus Data 20 martie 2014 19:26:34
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <iostream>
#include <fstream>
#include <cmath>

#define nmax 15001
using namespace std;
ifstream f("datorii.in");
ofstream g("datorii.out");

int a[nmax],n,m;

void update(int poz,int val)
{
    while (poz <=n)
    {
        int p = 0;
        while (!(poz & (1<<p))) p++;
            a[poz] += val;
        poz += (1 << p);
    }
}

int query(int num)
{
    int sol = 0;
    while (num > 0)
    {
        int p = 0;
         while (!(num & (1<<p))) p++;
            sol += a[num];
            num -=(1 << p);
    }
    return sol;
}


int main()
{
    f >> n >> m;
    for (int i=1;i<=n;i++)
    {
        int x;
        f >> x;
         update(i,x);
    }

    while (m--)
    {
        int x;
        f >> x;
    if (x == 1)
        {
            int a,b;
            f >> a >> b;
            g << (query(b) - query(a-1)) << '\n';
        }
        else
            {
                int a,b;
                f >> a >> b;
                update(a,b*(-1));
            }
    }


    return 0;
}