Pagini recente » Cod sursa (job #1419589) | Cod sursa (job #1495312) | Cod sursa (job #581265) | Cod sursa (job #948768) | Cod sursa (job #1225989)
/*
* 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);
}