Cod sursa(job #1464913)

Utilizator sfechisalin@yahoo.comSfechis Alin [email protected] Data 25 iulie 2015 23:23:06
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.1 kb
#include <fstream>
#include <vector>
using namespace std;
vector<int>G;
ofstream fout("datorii.out");
ifstream fin("datorii.in");
int n,m,a,semn,op,a1,b1,S,poz,val,l,r;
void update(int nod,int st,int dr)
{
    if (st==dr )
    {
        G[nod]+=semn*val;
        return;
    }
    int md =(st+dr)/2;
    if (poz<=md)
        update(2*nod,st,md);
    else
        update(2*nod+1,md+1,dr);
    G[nod]=G[2*nod]+G[2*nod+1];
}
void querry(int nod, int s, int d)
{
    if(l <= s && r >= d)
    {
        S += G[nod];
        return;
    }
    int m = (s+d)/2;
    if(l <= m) querry(nod*2, s, m);
    if(r > m) querry(nod*2+1, m+1, d);
}
int main()
{
    fin>>n>>m;
    G.resize(4*n);
    semn=1;
    for(int i=1;i<=n;i++)
        fin>>a,poz=i,val=a,update(1,1,n);
    semn=-1;
    while(m--)
    {
        fin>>op>>a1>>b1;
        if(op)
        {
            S=0;
            l=a1;
            r=b1;
            querry(1,1,n);
            fout<<S<<endl;
        }
        else
            poz=a1,val=b1,update(1,1,n);
    }
    fin.close();
    fout.close();
    return 0;
}