Cod sursa(job #2418143)

Utilizator AlexNeaguAlexandru AlexNeagu Data 3 mai 2019 20:13:19
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <fstream>

using namespace std;

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

int aib [20000], n, m, p, v, y, st, dr;

void update (int position, int value)
{
    while (position <= n)
    {
        aib [position] += value;
        position += (position & (- position));
    }
}
int query (int x)
{
    int s = 0;
    while (x)
    {
        s += aib [x];
        x -= (x & (-x) );
    }
    return s;
}
int main()
{
    ios_base::sync_with_stdio(false);

    fin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        int x;
        fin >> x;
        update(i , x);
    }
    for (int i = 1; i <= m; i++)
    {
       fin >> y;
       if (y == 0)
       {
           fin >> p >> v;
           update (p, -v);
       }
       if (y == 1)
       {
           fin >> st >> dr;
           int s1 = query (st - 1);
           int s2 = query (dr);
           fout << (s2 - s1) << "\n";
       }
    }
    return 0;
}