Cod sursa(job #3239977)

Utilizator nistor_dora_valentinaNistor Dora Valentina nistor_dora_valentina Data 10 august 2024 15:09:31
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.3 kb
#include <fstream>

using namespace std;
ifstream fin("datorii.in");
ofstream fout("datorii.out");
int n, m, i, j, v[15005], a[60005], t, x, y, sol;
void build(int nod, int st, int dr)
{
    if(st==dr)
        a[nod]=v[st];
    else
    {
       int mij=(st+dr)/2;
       build(2*nod, st, mij);
       build(2*nod+1, mij+1, dr);
       a[nod]=a[2*nod]+a[2*nod+1];
    }
}
void update(int nod, int st, int dr, int p, int x)
{
    if(st==dr)
        a[nod]-=x;
    else
    {
        int mij=(st+dr)/2;
        if(p<=mij)
            update(2*nod, st, mij, p, x);
        if(p>mij)
            update(2*nod+1, mij+1, dr, p, x);
        a[nod]=a[2*nod]+a[2*nod+1];
    }
}
void query(int nod, int st, int dr, int x, int y)
{
    if(x<=st && dr<=y)
        sol+=a[nod];
    else
    {
        int mij=(st+dr)/2;
        if(x<=mij)
            query(2*nod, st, mij, x, y);
        if(y>mij)
            query(2*nod+1, mij+1, dr, x, y);
    }
}
int main()
{
    fin>>n>>m;
    for(i=1; i<=n; i++)
        fin>>v[i];
        build(1, 1, n);
    while(m--)
    {
        fin>>t>>x>>y;
        if(t==0)
            update(1, 1, n, x, y);
        else
        {
            sol=0;
            query(1, 1, n, x, y);
            fout<<sol<<'\n';
        }
    }
    return 0;
}