Cod sursa(job #1360398)

Utilizator valentin50517Vozian Valentin valentin50517 Data 25 februarie 2015 14:25:48
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <fstream>

#define tran(x) x-(x & (-x))+1 
#define trans(x) (x & (-x))

using namespace std;
ifstream fin("datorii.in");
ofstream fout("datorii.out");

int AIB[15100],X[15100],N,M,a,b;
bool u;

void achitare(int T, int V){
	while(T <= N){
		AIB[T] -= V;
		T += trans(T);
	}
}
int nr(int i){
	int rs = 0;
	while(i > 0){
		rs+=AIB[i];
		i-=trans(i);
	}
	return rs;
}
void cerere(int a,int b){
	fout << nr(b) - nr(a-1) << '\n';
}
int main(){
	fin >> N >> M;
	fin >> X[1];
	AIB[1] = X[1];
	for(int i = 2;i<=N;i++){
		fin >> X[i];
		if(tran(i) > 1) for(int j = i;j>=tran(i);j--) AIB[i] += X[j];
		else{
			AIB[i] += AIB[i/2];
			for(int j = i/2+1;j<=i;j++) AIB[i] += X[j];
		}
	}
	for(int i = 0;i<M;i++){
		fin >> u >> a >> b;
		if(u) cerere(a,b); else achitare(a,b);
	}
	return 0;
}