Cod sursa(job #2749306)

Utilizator andreiiorgulescuandrei iorgulescu andreiiorgulescu Data 6 mai 2021 12:12:26
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.48 kb
#include <bits/stdc++.h>

using namespace std;

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

int arbint[60005],n,m,sum,i,j;

void update(int poz,int a,int b,int p,int val)
{
    if (a == b)
    {
        arbint[poz] -= val;
        //if (i == 6)
          //  cout << arbint[poz] << " " << poz << '\n';
        return;
    }
    if (p <= (a + b) / 2)
    {
        update(2 * poz,a,(a + b) / 2,p,val);
        arbint[poz] = arbint[2 * poz] + arbint[2 * poz + 1];
    }
    else
    {
        update (2 * poz + 1,(a + b) / 2 + 1,b,p,val);
        //if (i == 6)
            //cout << a << " " << b << " " << poz << " " << arbint[2 * poz] << " " << arbint[2 * poz + 1] << '\n';
        arbint[poz] = arbint[2 * poz] + arbint[2 * poz + 1];
    }
}

void query(int poz,int a,int b,int st,int dr)
{
    if (a >= st and b <= dr)
    {
        sum += arbint[poz];
        return;
    }
    int mij = (a + b) / 2;
    if (st <= mij)
        query(2 * poz,a,mij,st,dr);
    if (dr > mij)
        query(2 * poz + 1,mij + 1,b,st,dr);
}

int main()
{
    in >> n >> m;
    for (i = 1; i <= n; i++)
    {
        int x;
        in >> x;
        update(1,1,n,i,-x);
    }
    for (j = 1; j <= m; j++)
    {
        int tip,x,y;
        in >> tip >> x >> y;
        if (tip == 0)
            update(1,1,n,x,y);
        else
        {
            sum = 0;
            query(1,1,n,x,y);
            out << sum << '\n';
        }
    }
    return 0;
}