Cod sursa(job #778607)

Utilizator TudorDDodoiu Tudor TudorD Data 15 august 2012 09:29:52
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.88 kb
#include<iostream>
#include<fstream>

using namespace std;

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

const int MAXN = 15010;

int AIB[MAXN], V[MAXN], M, N;

inline int lsb(int x)
{
	return ( x & (-x) );
}

void update(int poz, int val)
{
	int c = 0;
	
	while(poz <= N){
		AIB[poz] -= val;
		while( !(poz & (1 << c)) ) ++c;
		poz += (1 << c);
		++c;
	}
}

int query(int poz)
{
	int c = 0, s = 0;
	
	while(poz > 0){
		s += AIB[poz];
		while( !(poz & (1 << c)) ) ++c;
		poz -= (1 << c);
		++c;
	}
	
	return s;
}

int main()
{
	int i, x, y, z;
	
	in >> N >> M;
	
	for(i = 1; i <= N; ++i){
		in >> V[i];
		V[i] += V[i - 1];
	}
	
	for(i = 1; i <= N; ++i)
		AIB[i] = V[i] - V[i - lsb(i)];
	
	while(M--){
		in >> x >> y >> z;
		
		if(x)
			out << query(z) - query(y - 1) << "\n";
		else
			update(y, z);
	}
	
	return 0;
}