Cod sursa(job #2982534)

Utilizator Alex578123Gusatu Alexandru Alex578123 Data 20 februarie 2023 13:44:59
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.66 kb
#include <fstream>
#include <queue>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <bitset>
#include <climits>
#include <set>
#include <cmath>
using namespace std;

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

#define dim 15001

int aint[4*dim+101], n, m, t, a, b;

inline void build(int node, int st, int dr)
{
    if(st == dr)
    {
        cin>>aint[node];
        return ;
    }
    int mid = (st+dr)>>1;
    build(node<<1, st, mid);
    build(node<<1|1, mid+1, dr);
    aint[node] = aint[node<<1] + aint[node<<1|1];
}

inline void update(int node, int st, int dr, int poz, int val)
{
    if(st == dr)
    {
        aint[node] -= val;
        return ;
    }
    int mid = (st+dr)>>1;
    if(poz <= mid)
        update(node<<1, st, mid, poz, val);
    if(poz > mid)
        update(node<<1|1, mid+1, dr, poz, val);
    aint[node] = aint[node<<1] + aint[node<<1|1];
}

inline int query(int node, int st, int dr, int a, int b)
{
    if(st >= a && dr <= b) ///overlapping
        return aint[node];
    else if(dr < a || b < st)
        return 0;
    else
    {
        int mid = (st+dr)>>1;
        if(a <= mid && b > mid)
            return (query(node<<1, st, mid, a, b) + query(node<<1|1, mid+1, dr, a, b));
        else if(a <= mid)
            return query(node<<1, st, mid, a, b);
        else
            return query(node<<1|1, mid+1, dr, a, b);
    }
}

int main()
{
    cin>>n>>m;
    build(1,1,n);
    for(int x = 1 ; x <= m ; x++)
    {
        cin>>t>>a>>b;
        if(t == 0)
            update(1,1,n,a,b);
        else
            cout<<query(1,1,n,a,b)<<'\n';
    }
}