Cod sursa(job #1825341)

Utilizator d0rina2011Craciun Dorina d0rina2011 Data 8 decembrie 2016 23:42:50
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <iostream>
#include <fstream>
#include <limits.h>

using namespace std;

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

int n, M, v[15001], m[15001];

void arbore(int nod, int st, int dr){
    int mid = st + (dr - st) / 2;
    if(st == dr) m[nod] = v[st];
    else {
        arbore(2 * nod, st, mid);
        arbore(2 * nod + 1, mid + 1, dr);
        m[nod] = m[2 * nod] + m[2 * nod + 1];
    }
}

void update (int nod, int st, int dr, int a, int b)
{
    if(st == dr)
        m[nod] -= b;
    else{
        int mij = (st + dr) / 2;
        if(mij >= a)
            update(2 * nod, st, mij, a, b);
        else
            update(2*nod + 1, mij + 1, dr, a, b);
        m[nod] = m[2 * nod] + m[2 * nod + 1];
    }
}
int query (int nod, int st, int dr, int a, int b)
{
    if(a <= st && b>= dr)
        return m[nod];
    else
    {
        int v1 = 0, v2 = 0;
        int mij = (st + dr)/2;
        if(a <= mij)
            v1 = query(2 * nod, st, mij, a, b);
        if(b >= mij + 1)
            v2 = query(2 * nod + 1, mij + 1, dr, a, b);
        return v1 + v2;
    }
}

int main()
{
    int i, x, a, b;
    fin>>n>>M;
    for(i = 1; i <= n; ++i){
        fin>>v[i];
    }
    arbore(1, 1, n);
    for(i = 1; i <= M; ++i){
        fin>>x>>a>>b;
        if(x == 0){
            update(1, 1, n, a, b);
        }
        else {
            fout<<query(1, 1, n, a, b)<<'\n';;
        }
    }
    return 0;
}