Cod sursa(job #2180254)

Utilizator MaxTeoTeo Oprescu MaxTeo Data 20 martie 2018 18:54:34
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.91 kb
#include <bits/stdc++.h>
using namespace std;

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

int tree[15001],n,m;

void update(int poz,int val)
{
    int pas=1;
    while(poz<=n)
    {
        tree[poz]+=val;
        while((poz&pas)==0)
            pas<<=1;
        poz+=pas;
        pas<<=1;
    }
}

int query(int poz)
{
    int pas=1,sum=0;
    while(poz)
    {
        sum+=tree[poz];
        while((poz&pas)==0)
            pas<<=1;
        poz-=pas;
        pas<<=1;
    }
    return sum;
}

int suma(int a,int b)
{
    return query(b)-query(a-1);
}

int main()
{
    int a,b,op,x;
    f>>n>>m;
    for(int i=1;i<=n;++i)
    {
        f>>x;
        update(i,x);
    }
    while(m--)
    {
        f>>op>>a>>b;
        switch(op)
        {
            case 0: update(a,-b); break;
            case 1: g<<suma(a,b)<<"\n"; break;
        }
    }
    return 0;
}