Cod sursa(job #1907985)

Utilizator valentinoMoldovan Rares valentino Data 6 martie 2017 22:04:25
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <fstream>
#include <iostream>
#define zero(x) ((x ^ (x - 1)) & x)
using namespace std;

int n, m, arb[15005 * 4], s;
ifstream f("datorii.in");
ofstream g("datorii.out");

void Add(int nod, int st, int dr, int val, int poz)
{
    if(st == dr)
    {
        arb[nod] += val;
    }
    else
    {
        int m = (st + dr) >> 1;
        if(poz <= m) Add(2 * nod, st, m, val, poz);
        else Add(2 * nod + 1, m + 1, dr, val, poz);
        arb[nod] = arb[2 * nod ] + arb[2 * nod + 1];
    }
}

void Query(int nod, int st, int dr, int start, int finish)
{
    if(start <= st && dr <= finish)
    {
         s += arb[nod];
         return;
    }
    else
    {
        int m = (st + dr) >> 1;
        if(start <= m) Query(2 * nod, st, m, start, finish);
        if(m < finish) Query(2 * nod + 1, m + 1, dr, start, finish);
    }
}

int main()
{
    int x, y, q;
    f >> n >> m;
    for(int i = 1; i <= n; ++i)
    {
        f >> x;
        Add(1, 1, n, x, i);
    }
    for(int i = 1; i <= m; ++i)
    {
        f >> q >> x >> y;
        if(!q) Add(1, 1, n, -y, x);
        else {s = 0; Query(1, 1, n, x, y); g << s << '\n';}
    }
}