Cod sursa(job #1225989)

Utilizator XiewRiklXiewRikl XiewRikl Data 4 septembrie 2014 11:59:29
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.75 kb
/*
 * datorii.cpp
 *	http://www.infoarena.ro/problema/datorii
 *  Created on: 4 Sep 2014
 *      Author: XiewRikl
 */
#include<stdio.h>

const unsigned int N = 15001;
unsigned long int A[N];

void processQuerry(short unsigned int ident,unsigned int x,unsigned int y,unsigned int length,FILE *fout){
	if(ident == 0){			//Processing a modification operation
		unsigned int pos=0;
		unsigned long power=1;
		while(x<=length){
			A[x]-=y;
			while( (x&power) == 0){
				pos += 1;
				power *=2;
			}
			x += power;
			pos += 1;
		}
		return;
	}
	else if (ident == 1){ //Processing a querry
		unsigned long pow = 1,sum=0,temp=0;
		while(y>0){
			sum += A[y];
			while( (y&pow) == 0){
				pow *= 2;
			}
			y -= pow;
			pow *= 2;
		}
		x -= 1;
		pow = 1;
		while(x>0){
			temp += A[x];
			while( (x&pow) == 0){
				pow *= 2;
			}
			x -= pow;
			pow *=2;
		}
		fprintf(fout,"%lu\n",sum-temp);
		return;
	}
	else if (ident == 2){ //Processing an initialization
		unsigned int pos=0;
		unsigned long power=1;
		while(x<=length){
			A[x]+=y;
			while( (x&power) == 0){
				pos += 1;
				power *=2;
			}
			x += power;
			pos += 1;
		}
	}

}


int main(void){
	FILE *fin,*fout;
	fin = fopen("datorii.in","r");
	fout = fopen("datorii.out","w");
	unsigned int length;
	unsigned long nr_op,temp;
	int trash =0;
	trash += fscanf(fin,"%u %lu",&length,&nr_op);
	//printf("\nA[]= ");
	for(unsigned int i=1;i<=length;++i){
		trash += fscanf(fin,"%lu",&temp);
		processQuerry(2,i,temp,length,fout);
	}
	short unsigned int ident;
	unsigned int x,y;
	for(unsigned long i=1;i<=nr_op;++i){
		trash += fscanf(fin,"%hu %u %u",&ident,&x,&y);
		processQuerry(ident,x,y,length,fout);
	}
	fclose(fin);
	fclose(fout);
	return(0);
}