Cod sursa(job #2902993)

Utilizator Tudose_StefanTudose Alexandru Stefan Tudose_Stefan Data 17 mai 2022 00:32:06
Problema Datorii Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.99 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

unsigned int nums[15000], nrElem, nrQuery, i, tipOp, nr1, nr2;
vector <unsigned int> arbore(70000, 0), rez;

unsigned int constructie(unsigned int st, unsigned int dr, unsigned int poz)
{
    if (st > dr) return 0;

    if (st == dr) {arbore[poz] = nums[dr]; return arbore[poz];}
    int stmax = constructie(st, st+(dr-st)/2, poz*2+1);
    int drmax = constructie(st+(dr-st)/2+1, dr, poz*2+2);
    arbore[poz] = stmax+drmax;
    return arbore[poz];
}

unsigned int query(unsigned int poz,unsigned int stLocal, unsigned int drLocal, unsigned int stQuer, unsigned int drQuer)
{
    if (drLocal < stQuer || stLocal > drQuer) return 0;
    if (drLocal <= drQuer && stLocal >= stQuer) return arbore[poz];
    return query(poz*2+1, stLocal, stLocal+(drLocal-stLocal)/2, stQuer, drQuer) + query(poz*2+2, stLocal+(drLocal-stLocal)/2+1, drLocal, stQuer, drQuer);
}

void modificare(unsigned int poz,unsigned int stLocal,unsigned int drLocal,unsigned int cineModif,unsigned int inCeModif)
{
    if (stLocal > cineModif || drLocal < cineModif) return;
    if (stLocal == drLocal) {arbore[poz] -= inCeModif; return;}
    modificare(poz*2+1, stLocal, stLocal+(drLocal-stLocal)/2, cineModif, inCeModif);
    modificare(poz*2+2, stLocal+(drLocal-stLocal)/2+1, drLocal, cineModif, inCeModif);
    arbore[poz] = arbore[2*poz+1] + arbore[2*poz+2];
}

int main()
{
    ios_base::sync_with_stdio(false);
    fin.tie(0);
    fin >> nrElem >> nrQuery;
    rez.reserve(nrQuery);
    for (i = 0; i < nrElem; ++i)
        fin >> nums[i];
    constructie(0, nrElem-1, 0);
    while (nrQuery--)
    {
        fin >> tipOp >> nr1 >> nr2;
        if (tipOp == 0)
            modificare(0, 0, nrElem-1, nr1-1, nr2);
        else
            rez.push_back(query(0, 0, nrElem-1, nr1-1, nr2-1));
    }
    for (auto i : rez)
        fout << i << '\n';
    return 0;
}