Cod sursa(job #2595927)

Utilizator PopescuAndreiAlexandruPopescu Andrei Alexandru PopescuAndreiAlexandru Data 8 aprilie 2020 17:50:33
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int DIM = 15005;

int n,m,AIB[DIM],event,a,b,x;

void UpdateArray(int pos, int value, int ok)
{
    do
    {
        if(ok)
            AIB[pos]+=value;
        else
            AIB[pos]-=value;
        pos+=pos&-pos;
    }while(pos<=n);
}

int QueryArray(int pos)
{
    int ans=0;
    while(pos)
    {
        ans+=AIB[pos];
        pos&=pos-1;
    }
    return ans;
}

int main()
{
    fin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        fin>>x;
        UpdateArray(i,x,1);
    }
    for(int i=1;i<=m;i++)
    {
        fin>>event>>a>>b;
        switch(event)
        {
        case 0:
            {
                UpdateArray(a,b,0);
                break;
            }
        default:
            {
                fout<<(QueryArray(b)-QueryArray(a-1))<<'\n';
                break;
            }
        }
    }
}