Pagini recente » Cod sursa (job #3189707) | Cod sursa (job #2949183) | Cod sursa (job #2167366) | Cod sursa (job #265379) | Cod sursa (job #3337991)
#include <fstream>
using namespace std;
ifstream fin("datorii.in");
ofstream fout("datorii.out");
int n, m, arbore[100005];
void update(int pozitie, int valoare, int st = 1, int dr = n, int nod = 1)
{
if(st == dr)
{
arbore[nod] -= valoare;
return;
}
int mijloc = (st+ dr) / 2;
if(mijloc >= pozitie)
{
update(pozitie, valoare, st , mijloc, nod*2);
}
else
{
update(pozitie, valoare, mijloc + 1, dr, nod*2 +1);
}
arbore[nod] = arbore[nod * 2] + arbore[nod * 2 + 1];
}
int show_sum(int st_initial, int dr_initial, int st = 1, int dr = n, int nod = 1)
{
if(st_initial <= st && dr <= dr_initial)
{
return arbore[nod];
}
int mijloc = (st + dr) / 2;
int suma = 0;
if(mijloc >= st_initial)
{
suma += show_sum(st_initial, dr_initial, st, mijloc, nod*2);
}
if(mijloc + 1 <= dr_initial)
{
suma += show_sum(st_initial, dr_initial, mijloc + 1, dr, nod * 2 + 1);
}
return suma;
}
int main()
{
fin>>n>>m;
int x;
for(int i = 1; i<= n; i++)
{
fin>>x;
update(i, -x);
}
int tip, a, b;
for(int i = 1; i <= m ; i++)
{
fin >> tip >> a >> b;
if(tip == 0)
{
update(a, b);
}
else
{
fout<<show_sum(a, b)<<'\n';
}
}
return 0;
}