Cod sursa(job #2981976)

Utilizator DavidAA007Apostol David DavidAA007 Data 19 februarie 2023 12:44:10
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.29 kb
#include<iostream>
#include<fstream>
using namespace std;
ifstream fin("datorii.in");
ofstream fout("datorii.out");
int n,m,i,j,x,v[60005],c,a,b;
void update(int poz, int val, int st, int dr, int ind)
{
    int mij=(st+dr)/2;
    if(st==dr)
    {
        v[ind]=v[ind]+val;
    }
    else
    {
        if(poz<=mij)
            update(poz,val,st,mij,2*ind);
        else
            update(poz,val,mij+1,dr,2*ind+1);
        v[ind]=v[2*ind]+v[2*ind+1];
    }

}
int query(int ai, int bi, int st, int dr, int ind)
{
    int mij=(st+dr)/2;
    if(ai<=st && dr<=bi)
        return v[ind];
    if(ai<=mij && mij<bi)
        return query(ai,bi,st,mij,ind*2)+query(ai,bi,mij+1,dr,ind*2+1);
    if(mij<ai)
        return query(ai,bi,mij+1,dr,ind*2+1);
    return query(ai,bi,st,mij,ind*2);
}
int main()
{
    fin>>n>>m;
    for(i=1;i<=n;i++)
    {
        fin>>x;
        update(i,x,1,n,1);
    }
    for(i=1;i<=m;i++)
    {
        fin>>c>>a>>b;
        if(c==0)
        {
            //cout<<"INTRAM AICI 1\n";
            update(a,-b,1,n,1);
        }
        else
        {
            //cout<<"INTRAM AICI 2\n";
            fout<<query(a,b,1,n,1)<<"\n";
        }
        //cout<<"AM IESIT\n";
    }
    fin.close();
    fout.close();
    return 0;
}
/*
 6 6
 1 3 2 0 0 10
 1 3 6
 1 1 4
 0 3 1
 1 1 6
 0 6 2
 1 1 6
 */