Cod sursa(job #2479050)

Utilizator Cezar211Popoveniuc Cezar Cezar211 Data 23 octombrie 2019 10:08:35
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <bits/stdc++.h>
#define NM 15010
using namespace std;
ifstream fin ("datorii.in");
ofstream fout ("datorii.out");
void read();
int n, cod, Q;
int v[NM], t[NM];
int lg(int x)
{
    return x&-x;
}
void update(int ind, int val)
{
    while(ind<=n)
    {
        t[ind]-=val;
        ind+=lg(ind);
    }
}
int query(int x)
{
    int s = 0;
    while(x>=1)
    {
        s+=t[x];
        x-=lg(x);
    }
    return s;
}
void upd_bld(int ind, int val)
{
    while(ind<=n)
    {
        t[ind]+=val;
        ind+=lg(ind);
    }
}
void build()
{
    for(int i=1; i<=n; i++)
        upd_bld(i, v[i]);
}
int main()
{
    read();
    build();
    while(Q--)
    {
        int x, y;
        fin >> cod >> x >> y;
        if(cod == 0)
            update(x, y);
        else fout << query(y) - query(x-1) << '\n';
    }
    return 0;
}
void read()
{
    fin >> n >> Q;
    for(int i=1; i<=n; i++)
        fin >> v[i];
}