Pagini recente » Cod sursa (job #1846219) | Istoria paginii utilizator/burcavlad | Cod sursa (job #2385071) | Cod sursa (job #1154612) | Cod sursa (job #528817)
Cod sursa(job #528817)
// http://infoarena.ro/problema/datorii
// rezolvata cu arbori indexati binar (http://infoarena.ro/aib)
#include <fstream>
using namespace std;
#define maxDays 15002
#define zeros(x) ( (x ^ (x - 1)) & x )
ifstream in("datorii.in");
ofstream out("datorii.out");
int days;
int AIB[maxDays];
void add(int where,int quantity);
int compute(int where); // returneaza suma elementelor subsecventei (0,where);
int main() {
int operations,value;
int first,second;
bool interogation;
in >> days >> operations;
for(int currentDay=1;currentDay<=days;currentDay++) {
in >> value;
add(currentDay,value);
}
for(int i=1;i<=operations;i++) {
in >> interogation >> first >> second;
if(interogation)
out << compute(second) - compute(first-1) << "\n";
else
add(first,-second);
}
return (0);
}
void add(int where,int quantity) {
for(int k=where;k<=days;k=k+zeros(k))
AIB[k] += quantity;
}
int compute(int where) {
int value = 0;
for(int k=where;k>0;k=k-zeros(k))
value = value + AIB[k];
return value;
}