Cod sursa(job #209124)

Utilizator pitbullpitbulll pitbull Data 20 septembrie 2008 20:43:42
Problema Datorii Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <stdio.h>
#include <assert.h>
#define IN_FILE "datorii.in"
#define OUT_FILE "datorii.out"
#define NMAX 15001


int A[NMAX];
int N,M,i,j,k;

void achita(int,int);
int query (int);
int nr_zero(int );

int main(){
	int type,res;
	FILE *f=fopen(IN_FILE,"rt");
	FILE *g=fopen(OUT_FILE,"wt");
	
	fscanf(f,"%d %d",&N,&M);
	
	int x;
	for (i=1;i<=N;i++){
		fscanf(f,"%d",&x);
		achita(i,x);
	}
	for (i=1;i<=M;i++){
		fscanf(f,"%d %d %d",&type,&j,&k);
		if(type==0)
			achita(j,-k);//se achta suma k in ziua j
		else{
			res=query(k)-query(j-1);
			fprintf(g,"%d\n",res);
		}
	}
	fclose(f);
	fclose(g);
	return 0;
}

int nr_zero(int a){
	if(a==0)
		return 0;
	int nr,comp;
	for (nr=0,comp=1;!(a&comp);nr++,comp<<=1);
	return nr;
}

void achita(int a,int b){
	int tmp=a;
	A[tmp]+=b;
	tmp=tmp+(1<<nr_zero(tmp));
	while(tmp<=N){
		A[tmp]+=b;
		tmp=tmp+(1<<nr_zero(tmp));
	}
}
int query (int z){
	int tmp=z,s=0;
	s+=A[z];
	tmp=tmp-(1 << nr_zero(tmp));
	while(tmp>=1){
		s+=A[tmp];
		tmp=tmp-(1<<nr_zero(tmp));
	}
	return s;
}