Cod sursa(job #3125896)

Utilizator Turcanu_DavidTurcanu David Turcanu_David Data 4 mai 2023 19:09:55
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.2 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("datorii.in");
ofstream fout("datorii.out");

int n, m;
int t[15000*4];

void update(int p, int low, int high, int pos, int val)
{
    if(p < low || high < p)
        return;
    if(low == p && p == high)
    {
        t[pos]+=val;
        return;
    }
    int mid=(low+high)/2;
    update(p, low, mid, pos*2+1, val);
    update(p, mid+1, high, pos*2+2, val);
    t[pos]=t[pos*2+1]+t[pos*2+2];
}

int query(int qleft, int qright, int low, int high, int pos)
{
    if(qleft <= low && high <= qright)
    {
        return t[pos];
    }
    if(qright < low || high < qleft)
    {
        return 0;
    }
    int mid=(low+high)/2;
    return query(qleft, qright, low, mid, pos*2+1)+query(qleft, qright, mid+1, high, pos*2+2);
}

int main()
{
    fin>>n>>m;
    for(int i=1; i<=n; i++)
    {
        int a;
        fin>>a;
        update(i, 1, n, 1, a);
    }
    for(int i=1; i<=m; i++)
    {
        int q, a, b;
        fin>>q>>a>>b;
        if(q == 0)
        {
            update(a, 1, n, 1, -b);
        }
        else
        {
            fout<<query(a, b, 1, n, 1)<<'\n';
        }
    }
    return 0;
}