Cod sursa(job #1043142)

Utilizator uagamagaMatei Rogoz uagamaga Data 28 noiembrie 2013 01:10:10
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.23 kb
#include <fstream>

using namespace std;

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

int tree[80000],m,n,d,aq,lowInt,highInt,s,k=1;
void update(int fp,int left,int right)
{

    if(left==right)
    {
        if(k) tree[fp] = aq;
        else tree[fp] -= aq;
        return;
    }

    int seg = (left+right)/2;

    if(d<=seg) update(2*fp,left,seg);
    else        update(2*fp+1,seg+1,right);

    tree[fp] = tree[2*fp]+tree[2*fp+1];

}

void query(int fp,int left,int right)
{

    if(lowInt<=left && highInt>=right)
    {
        s+=tree[fp];
        return;
    }

    int seg = (left+right)/2;

    if(lowInt<=seg) query(2*fp,left,seg);
    if(seg<highInt) query(2*fp+1,seg+1,right);

}

int main()
{
    int i,x,y,z;
    fin>>n>>m;
    k = 1;
    for(i=1;i<=n;i++)
    {
        fin>>y;
        d = i;
        aq = y;
        update(1,1,n);
    }

    k=0;
    while(m--)
    {
        fin>>x>>y>>z;
        if(x)
        {
            s=0;
            lowInt = y; highInt = z;
            query(1,1,n);
            fout<<s<<"\n";
        }
        else
        {
            d = y; aq = z;
            update(1,1,n);

        }

    }



    return 0;
}