Cod sursa(job #1876687)

Utilizator marcdariaDaria Marc marcdaria Data 12 februarie 2017 15:51:31
Problema Datorii Scor 0
Compilator cpp Status done
Runda becreative11 Marime 1.45 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int lg=100005;
int init[lg],t[4*lg+200],a,b,rez;
int n,m,x;

void Build(int st, int dr, int poz);
void Update(int poz, int val);
void Update2(int poz, int val);
void Solve(int st, int dr, int poz);

int main()
{
    fin>>n>>m;

    Build(1,n,1);

    for(int i=1;i<=n;++i)
    {
        fin>>x;
        Update(i,x);
    }

    while(m--)
    {
        fin>>x>>a>>b;

        if(x)
        {
            rez=0;
            Solve(1,n,1);
            fout<<rez<<'\n';
        }
        else if(!x) Update2(a,b);
    }

    fin.close();
    fout.close();

    return 0;
}

void Build(int st, int dr, int poz)
{
    if(st==dr)
    {
        init[st]=poz;
        return;
    }

    int x=(st+dr)/2;
    Build(st,x,2*poz);
    Build(x+1,dr,2*poz+1);
}
void Update(int poz, int val)
{
    int x=init[poz];
    t[x]=val;
    x>>=1;
    while(x)
    {
        t[x]=t[x]+val;
        x>>=1;
    }
}
void Update2(int poz, int val)
{
    int x=init[poz];
    t[x]=t[x]-val;
    x>>=1;
    while(x)
    {
        t[x]=t[x]-val;
        x>>=1;
    }
}
void Solve(int st, int dr, int poz)
{
    if(st>b || dr<a) return;
    if(a<=st && dr<=b)
        rez=rez+t[poz];
    else if(st<dr)
    {
        int x=(st+dr)/2;
        Solve(st,x,poz*2);
        Solve(x+1,dr,poz*2+1);
    }
}