Cod sursa(job #2919777)

Utilizator CrullTCrull Thomas CrullT Data 19 august 2022 17:16:52
Problema Datorii Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.5 kb
#include <fstream>

using namespace std;

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

int sir[100000],s[15001],n,m,t=0,v=0,suma;

void makesir(int st, int dr, int l) //creez nodurile
{
    int suma=0,i;
    for(i=st;i<=dr;i++)
        suma+=s[i];
    sir[l]=suma;
    if(st!=dr)
    {
        makesir(st, (st+dr)/2, 2*l);
        makesir((st+dr)/2+1, dr, 2*l+1);
    }
}

void update(int st, int dr, int l)  //upd nod cu nod
{
    sir[l]-=v;
    if(!(st==dr && st==t))
    {
        if((st+dr)/2>=t)
            update(st,(st+dr)/2, 2*l);
        else if((st+dr)/2<t)
            update((st+dr)/2+1, dr, 2*l+1);
    }
}

void finder(int st, int dr, int l, int cst, int cdr)
{
    if(st==cst && dr==cdr)
        suma+=sir[l];
    else if(cst<=(st+dr)/2 && cdr>=(st+dr)/2+1)
    {
        finder(st,(st+dr)/2 , 2*l, cst, (st+dr)/2);
        finder((st+dr)/2+1, dr, 2*l+1, (st+dr)/2+1, cdr);
    }
    else if(cst<=(st+dr)/2 && cdr<=(st+dr)/2+1)
        finder(st, (st+dr)/2, 2*l, cst, cdr);
    else
        finder((st+dr)/2+1, dr, 2*l+1, cst, cdr);

}

int main()
{
    int i,c,a,b;
    in>>n>>m;
    for(i=1;i<=n;i++)
        in>>s[i];
    makesir(1,n,1);
    for(i=1;i<=m;i++)
    {
        in>>c;
        if(c==0)
        {
            in>>t>>v;
            update(1,6,1);
        }
        else
        {
            in>>a>>b;
            suma=0;
            finder(1,6,1,a,b);
            out<<suma<<'\n';
        }
    }
    return 0;
}