Cod sursa(job #2750656)

Utilizator un_fes_galbendaniel guba un_fes_galben Data 12 mai 2021 18:02:12
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.27 kb
#include <fstream>
using namespace std;
ifstream fin("datorii.in");
ofstream fout("datorii.out");
char buff[4096];
int pbuf=4095;
void readbuff()
{
    pbuf=0;
    fin.read(buff,4095);
}
int citire()
{
    int nr=0;
    if(pbuf==4095)
    {
        readbuff();
    }
    while(buff[pbuf]<'0'||buff[pbuf]>'9')
    {
        pbuf++;
        if(pbuf==4095)
        {
            readbuff();
        }
    }
    while(buff[pbuf]>='0'&&buff[pbuf]<='9')
    {
        nr=nr*10+buff[pbuf]-'0';
        pbuf++;
        if(pbuf==4095)
        {
            readbuff();
        }
    }
    return nr;
}
int aib[15005],cnt;
int n;
void update(int poz,int val)
{
    while(poz<=n)
    {
        aib[poz]+=val;
        poz+=poz&-poz;
    }
}
int query(int poz)
{
    cnt=0;
    while(poz>0)
    {
        cnt+=aib[poz];
        poz-=poz&-poz;
    }
    return cnt;
}
int main()
{
    int m,tip,a,b;
    n=citire();m=citire();
    for(int i=1;i<=n;i++)
    {
        a=citire();
        update(i,a);
    }
    for(int i=1;i<=m;i++)
    {
       tip=citire();a=citire();b=citire();
       if(tip==0)
       {
           update(a,-b);
       }
       else
       {
           fout<<query(b)-query(a-1)<<'\n';
       }
    }
    return 0;
}