Cod sursa(job #2904423)

Utilizator AntoniaPopoviciAntonia-Adelina Popovici AntoniaPopovici Data 17 mai 2022 23:36:18
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <fstream>
#include <iostream>
#define MAX 100005

using namespace std;

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

int n, m, v[10 * MAX];
int x, y, z, q, sum_rest;
bool opt;


void update(int poz, int val)
{
    while (poz <= n)
    {
        v[poz] += val;
        poz += (poz & (-poz));
    }
}

int query(int poz)
{
    int sum = 0;
    while (poz)
    {
        sum += v[poz];
        poz -= (poz & (-poz));
    }
    return sum;
}

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

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