Cod sursa(job #2803660)

Utilizator maco1503Macovei Teodor-Andrei maco1503 Data 20 noiembrie 2021 12:21:27
Problema Datorii Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.61 kb
#include <bits/stdc++.h>
#define rpd ios_base :: sync_with_stdio(0); cin.tie(0);
#define ll long long
#define fs first
#define sc second
#define pb push_back
#define mod 666013
#define NMAX 100000 + 100
using namespace std;

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

int aint[4 * NMAX], v[NMAX], n, q, ans;

void build(int nod,int st,int dr)
{
    if(st==dr)
    {
        aint[nod]=v[st];
        return;
    }

    int mid=(st+dr)/2;

    build(nod<<1,st,mid);
    build(nod<<1|1,mid+1,dr);

    aint[nod]=aint[nod<<1] + aint[nod<<1|1];

    return;

}

void update(int nod,int st,int dr, int poz, int val)
{
    if(st==dr&&dr==poz)
    {
        aint[nod] -= val;
        return;
    }

    int mid=(st+dr)/2;

    if(poz<=mid)
        update(nod<<1,st,mid,poz,val);
    else
        update(nod<<1|1,mid+1,dr,poz,val);

    aint[nod] = aint[nod<<1] + aint[nod<<1|1];

    return;
}
int query(int nod,int st,int dr, int L, int R)
{
    if (R < st || dr < L)
        return 0;

    if(L<=st&&R>=dr)
    {
        return aint[nod];

    }
    int mid=(st+dr)/2;

    return query(nod<<1,st,mid,L,R)+query(nod<<1|1,mid+1,dr,L,R);
}
main()
{
    rpd;
    int n,q;
    cin>>n>>q;
    for(int i=1;i<=n;++i)
        cin>>v[i];
    build(1,1,n);
    while(q--)
    {
        int t;
        cin>>t;
        if(t==0)
        {   int poz,val;
            cin>>poz>>val;

            update(1,1,n,poz,val);


        }
        else
        {   int L,R;
            cin>>L>>R;

            cout<<query(1,1,n,L,R)<<'\n';



        }
    }

}