Cod sursa(job #1876608)

Utilizator ImGeluGelu Ungur ImGelu Data 12 februarie 2017 14:54:42
Problema Datorii Scor 0
Compilator cpp Status done
Runda becreative11 Marime 1.19 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int init[100005],t[400220],a,b,rez;

void construire(int st, int dr, int poz)
{
    int x=(st+dr)/2;
    if(st==dr){init[st]=poz; return ;}

    construire(st, x, 2*poz);
    construire(x+1, dr, 2*poz+1);
}

void actualizare(int poz, int val)
{
     int x=init[poz];
     t[x]=val;
     x>>=1;
     while(x) t[x]+=val, x>>=1;
}

void actualizare2(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 interogare(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;
        interogare(st, x, poz*2);
        interogare(x+1, dr, poz*2+1);
    }
}

int main()
{
    int x, n, m;

    fin>>n>>m;

    construire(1, n, 1);

    for(int i=1; i<=n; i++) fin>>x, actualizare(i, x);

    for(int i=1; i<=m; i++){
        fin>>x>>a>>b;

        if(x==1){
        rez=0;
        interogare(1, n, 1);
        fout<<rez<<"\n";
        }
        else if(x==0) actualizare2(a,b);
}

    return 0;
}