Cod sursa(job #2124339)

Utilizator IsacLucianIsac Lucian IsacLucian Data 7 februarie 2018 09:35:03
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <bits/stdc++.h>

using namespace std;

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

int n,q,aib[15005];

void Update_1(int p,int x)
{
    while(p<=n)
    {
        aib[p]+=x;
        p+=(p&(-p));
    }
}

void Update_2(int p,int x)
{
    while(p<=n)
    {
        aib[p]-=x;
        p+=(p&(-p));
    }
}

int Query(int p)
{
    int s=0;
    while(p)
    {
        s+=aib[p];
        p-=(p&(-p));
    }
    return s;
}

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

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

    while(q--)
    {
        fin>>op>>x>>y;

        if(!op)Update_2(x,y);
        else fout<<Query(y)-Query(x-1)<<"\n";
    }

    return 0;
}