Cod sursa(job #2754374)

Utilizator icnsrNicula Ionut icnsr Data 25 mai 2021 18:24:56
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.15 kb
#include <fstream>
std::ifstream fin("datorii.in");
std::ofstream fout("datorii.out");

#include <array>
#include <climits>

constexpr unsigned NMAX = 100000;
constexpr unsigned DIM = 6 * NMAX;

std::array<int, DIM> data = {};

void update_leaf(const unsigned index, const unsigned begin, const unsigned end,
                 const unsigned pos, const int value)
{
        if(begin == end)
        {
                data[index] += value;
                return;
        }

        const unsigned mid = begin + (end - begin) / 2;
        if(pos <= mid)
        {
                update_leaf(2 * index, begin, mid, pos, value);
        }
        else
        {
                update_leaf(2 * index + 1, mid + 1, end, pos, value);
        }

        data[index] = data[2 * index] + data[2 * index + 1];
}

void query(const unsigned index, const unsigned begin, const unsigned end,
           const unsigned qleft, const unsigned qright, int& result)
{
        if(qleft <= begin && end <= qright)
        {
                result += data[index];
                return;
        }

        const unsigned mid = begin + (end - begin) / 2;

        if(qleft <= mid)
        {
                query(2 * index, begin, mid, qleft, qright, result);
        }
        if(mid < qright)
        {
                query(2 * index + 1, mid + 1, end, qleft, qright, result);
        }
}

template<typename T>
T mread()
{
        T temp;
        fin >> temp;
        return temp;
}

int main()
{
        const auto N = mread<unsigned>();
        const auto M = mread<unsigned>();

        for(unsigned i = 0; i < N; ++i)
        {
                update_leaf(1, 1, N, i + 1, mread<int>());
        }

        for(unsigned i = 0; i < M; ++i)
        {
                const int op = mread<int>();
                const int x = mread<int>();
                const int y = mread<int>();

                if(op == 1)
                {
                        int result = 0;
                        query(1, 1, N, x, y, result);

                        fout << result << '\n';
                }
                else
                {
                        update_leaf(1, 1, N, x, -y);
                }
        }
}