Cod sursa(job #2551593)

Utilizator Anastasia11Susciuc Anastasia Anastasia11 Data 19 februarie 2020 23:02:47
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.8 kb
#include <iostream>
#include <fstream>
#define Nmax 15005
#define zeros(x) ((x^(x-1))&x)

using namespace std;

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

int n, m, cod, x, y;;
int aib[Nmax]; //  aib[i]- i are k zerouri in final;suma secventei [i-2^k+1; i]

void update(int poz, int val)
{
    for (; poz <= n; poz+=zeros(poz)) aib[poz]+=val;
}

int query(int poz)
{
    int sum=0;
    for (; poz; poz-=zeros(poz)) sum+=aib[poz];
    return sum;
}

int main()
{
    f >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        f >> x;
        update (i, x);
    }
    while (m--)
    {
        f >> cod >> x >> y;
        // cout << x << " " << y << '\n';
        if (cod == 0) update (x, (-1)*y);
        else g << query(y)-query(x-1) << '\n';
    }

    return 0;
}