Cod sursa(job #3206858)

Utilizator Raul_AArdelean Raul Raul_A Data 24 februarie 2024 12:16:09
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.63 kb
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
using namespace std;

const string fn("datorii");

ifstream in(fn + ".in");
ofstream out(fn + ".out");

#define cin in
#define cout out

int n,q;

struct SegmentTree{
    int n;
    vector<ll> tree;

    void init(int _n)
    {
        n=_n;
        tree.resize(4*n+5,0);
    }

    void build(int node,int l,int r)
    {
        if(l==r)
            cin>>tree[node];
        else
        {
            int m=(l+r)/2;
            build(2*node,l,m);
            build(2*node+1,m+1,r);

            tree[node]=tree[2*node]+tree[2*node+1];
        }
    }

    void upd(int node,int l,int r,int target,int val)
    {
        if(l==r)
            tree[node]+=val;
        else
        {
            int m=(l+r)/2;
            if(target<=m)
                upd(2*node,l,m,target,val);
            else upd(2*node+1,m+1,r,target,val);

            tree[node]=tree[2*node]+tree[2*node+1];
        }
    }

    ll query(int node,int l,int r,int a,int b)
    {
        if(a<=l and r<=b)
            return tree[node];
        int m=(l+r)/2;

        ll lft=0,rgt=0;
        if(a<=m) lft=query(2*node,l,m,a,b);
        if(b>m) rgt=query(2*node+1,m+1,r,a,b);

        return lft+rgt;
    }
}aint;

int main()
{
    int n,q;
    cin>>n>>q;
    aint.init(n);
    aint.build(1,1,n);
    
    while(q--)
    {
        int t,l,r;
        cin>>t>>l>>r;

        if(t==0)
        {
            aint.upd(1,1,n,l,-r);
        }
        else
        {
            cout<<aint.query(1,1,n,l,r)<<'\n';
        }
    }
    return 0;
}