Cod sursa(job #2622462)

Utilizator BourucLiviuBouruc Petru Liviu BourucLiviu Data 1 iunie 2020 12:47:17
Problema Datorii Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.39 kb
#include <fstream>

#define N 15001

using namespace std;

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

int heap[3*N];
int pos, x;

inline void actualizare(int nod, int st, int dr)
{
    int mij;
    if(st == dr)
        heap[nod] += x;
    else
    {
        mij = (st+dr) / 2;
        if(st <= pos && pos <= mij) actualizare(nod*2, st, mij);
        if(mij+1 <= pos && pos <= dr) actualizare(nod*2+1, mij+1, dr);
        heap[nod] = heap[nod*2] + heap[nod*2+1];
    }
}

inline int DEI(int nod, int st, int dr, int a, int b)
{
    int mij, sum;
    if(a <= st && dr <= b) return heap[nod];
    else
    {
        mij = (st+dr) / 2;
        sum = 0;
        if(a <= mij) sum += DEI(nod*2, st, mij, a, b);
        if(mij+1 <= b) sum += DEI(nod*2+1, mij+1, dr, a, b);
        return sum;
    }
}

int main()
{
    int n, m;
    fin >> n >> m;
    for(int i = 1; i <= n; ++i)
    {
        fin >> x;
        pos = i;
        //x *= -1;
        actualizare(1, 1, n);
    }
    bool var;
    while(m--)
    {
        fin >> var;
        if(var == 0)
        {
            fin >> pos >> x;
            x *= -1;
            actualizare(1, 1, n);
        }
        else
        {
            int p, q;
            fin >> p >> q;
            fout << DEI(1, 1, n, p, q) << '\n';
        }
    }
    fin.close(); fout.close();
    return 0;
}