# Cod sursa(job #2592346)

Utilizator Data 1 aprilie 2020 16:33:10 Datorii 0 cpp-64 done Arhiva de probleme 1.32 kb
``````#include <iostream>
#include <fstream>

using namespace std;

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

const int N = 1 << 14, INF = 1;
int n, q, a, b, poz, val, t[N];

void actualizare(int p, int st, int dr)
{
if(st == dr){
if(t[p] != 0){
t[p] -= val;
}
else{
t[p] = val;
}
return;
}
int m = (st + dr) / 2;
if(poz <= m){
actualizare(2 * p, st, m);
}
else{
actualizare(2 * p + 1, m + 1, dr);
}
t[p] = t[2 * p] + t[2 * p + 1];
}

int interogare(int p, int st, int dr)
{
if(a <= st && dr <= b){
return t[p];
}
int m = (st + dr) / 2, mst = 0, mdr = 0;
if(a <= m){
mst = interogare(2 * p, st, m);
}
if(b > m){
mdr = interogare(2 * p + 1, m + 1, dr);
}
return mst + mdr;
}

int main()
{
in >> n >> q;
for(int i = 1; i <= n; i++){
in >> val;
poz = i;
actualizare(1, 1, n);
}
while(q--){
int code;
in >> code;
if(code == 0){
in >> poz >> val;
actualizare(1, 1, n);
}
else{
in >> a >> b;
int m = interogare(1, 1, n);
out << m << "\n";
}
}

return 0;
}
``````