Cod sursa(job #1231619)

Utilizator LycrsTrifan Tamara Lycrs Data 21 septembrie 2014 08:21:10
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.1 kb
#include <fstream>
#include<algorithm>
using namespace std;


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

int tb[100000*4+100], x, n, m, a, b, i, r, u, j;

void update(int nod, int st, int dr, int poz, int val)
{
    if (st==dr) tb[nod]+=val;
    else
    {
        int mij=(st+dr)/2;
        if (mij>=poz) update(nod*2, st, mij, poz, val);
                 else update(nod*2+1, mij+1, dr, poz, val);
        tb[nod]=tb[nod*2] + tb[nod*2+1];
    }

}

int query(int nod, int st, int dr, int a, int b)
{
    if (a==st && b==dr)
        return tb[nod];
    int m=(st+dr)/2;
    if (b<=m)
        return query(2*nod,st,m,a,b);
    if (a>=m+1)
        return query(2*nod+1,m+1,dr,a,b);
    return
        query(2*nod,st,m,a,m)+query(2*nod+1,m+1,dr,m+1,b);
}


int main()
{
    f>>n>>m;
    for (i=1; i<=n; ++i) {f>>x; update(1, 1, n, i,x);}
    for (i=1; i<=m; ++i)
    {
        f>>u>>a>>b;
        if (u==1)
            {
                r=0;
                g<<query(1, 1, n, a, b)<<'\n';
            }
        else update(1,1,n,a,-1*b);
    }



    return 0;
}