Cod sursa(job #2865293)

Utilizator Alle43221Moroz Alexandra-Ioana Alle43221 Data 8 martie 2022 18:10:04
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.75 kb
#include <iostream>
#include <fstream>
#include <climits>

#define max_n 15002

using namespace std;

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

int A[4*max_n], v[max_n], n, m, c, a, b;

void build(int l, int r, int n)
{
    if(r==l)
    {
        A[n]=v[l];
    }
    else
    {
        int m=(l+r)/2;
        build(l, m, 2*n);
        build(m+1, r, 2*n+1);
        A[n]=A[2*n]+ A[2*n+1];
    }
}

int maximul(int l, int r, int n, int a, int b)
{
    if((a<=l)&&(r<=b))
    {
        return A[n];
    }
    else
    {
        int m=(l+r)/2, s1=0, s2=0;
        if(m>=a)
        {
            s1=maximul(l, m, 2*n, a, b);
            //cout<<l<<" "<<m<<endl;
        }
        if(m+1<=b)
        {
            s2=maximul(m+1, r, 2*n+1, a, b);
            //cout<<l<<" "<<m<<endl;
        }
        A[n]=A[2*n]+A[2*n+1];
        return s1+s2;
    }
}

void update (int l, int r, int n, int a, int b)
{
    if(l==r)
    {
        A[n]-=b;
    }
    else
    {
        int m=(l+r)/2;
        if(a<=m)
        {
            update(l, m, n*2, a, b);
        }
        else
        {
            update(m+1, r, 2*n+1, a, b);
        }
        A[n]=A[2*n]+A[2*n+1];
    }
}

int main()
{
    fin>>n>>m;
    for(int i=1; i<=n; i++)
    {
        fin>>v[i];
    }
    build(1, n, 1);

    for(int i=0; i<m; i++)
    {
        fin>>c>>a>>b;
        if(c==1)
        {
            fout<<maximul(1, n, 1, a, b)<<'\n';
        }
        else
        {
            update(1, n, 1, a, b);
        }
        /*
        for(int i=1; i<=4*n; i++)
        {
            cout<<A[i]<<' ';
        }
        cout<<endl;
        */
    }


    fin.close();
    fout.close();
    return 0;
}