Cod sursa(job #3244852)

Utilizator Sasha_12454Eric Paturan Sasha_12454 Data 26 septembrie 2024 18:02:48
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <bits/stdc++.h>

const std :: string FILENAME = "datorii";

std :: ifstream f (FILENAME + ".in");

std :: ofstream g (FILENAME + ".out");

const int NMAX = 15e3 + 5;

int n;

int q;

int cer;

int x;

int y;

int v[NMAX];

int arb[NMAX];

void update(int x, int y)
{
    while(x <= n)
    {
        arb[x] += y;

        x += (x & (-x));
    }
}

int query(int x)
{
    int s = 0;

    while(x)
    {
        s += arb[x];

        x -= (x & (-x));
    }

    return s;
}

int main()
{

    f >> n >> q;

    for(int i = 1; i <= n; i ++)
    {
        f >> v[i];

        update(i, v[i]);
    }

    while(q --)
    {
        f >> cer >> x >> y;

        if(cer == 0)
        {
            update(x, -y);
        }
        else
        {
            g << query(y) - query(x - 1) << '\n';
        }
    }

    return 0;
}