Cod sursa(job #3278355)

Utilizator Victor5539Tanase Victor Victor5539 Data 19 februarie 2025 15:38:56
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <iostream>
#include <fstream>
#define int long long
using namespace std;
ifstream fin("datorii.in");
ofstream fout("datorii.out");

const int MAX=15000;
int v[MAX+5],A[4*MAX+5],i,q,n,t,x,y,ans;


void build(int nod, int st ,int dr)
{
    if (st==dr)
        A[nod]=v[st];
    else
    {
        int mij=(st+dr)>>1;
        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)>>1;

        if (p<=mij)
            update(2*nod,st,mij,p,x);
        else
            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 a ,int b)
{
    if (a<=st && dr<=b)
        ans+=A[nod];
    else
    {
        int mij=(st+dr)>>1;

        if (a<=mij)
            query(2*nod,st,mij,a,b);

        if (b>=mij+1)
            query(2*nod+1,mij+1,dr,a,b);
    }
}


signed main()
{
    fin>>n>>q;

    for (i=1; i<=n; i++)
        fin>>v[i];

    build(1,1,n);

    while (q)
    {
        fin>>t>>x>>y;

        if (t==0)
            update(1,1,n,x,y);
        else
        {
            ans=0;
            query(1,1,n,x,y);

            fout<<ans<<"\n";
        }

        q--;
    }
    return 0;
}