Cod sursa(job #287158)

Utilizator Sorin_IonutBYSorynyos Sorin_Ionut Data 24 martie 2009 16:32:11
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.18 kb
#include <stdio.h>

#define IN "datorii.in"
#define OUT "datorii.out"
#define max 32000

int arb[max];
int n,m;
int elem=1;
int sol;

FILE *fin=fopen(IN,"r");
FILE *fout=fopen(OUT,"w");

void proceseaza(int,int,int,int);

int main()
{
 int i;
 int op,x,y;
 int curent,poz;

 fscanf(fin,"%d %d",&n,&m);

 while(elem<n)
  elem=(elem<<1);

 for(i=1;i<=n;i++)
 {
  fscanf(fin,"%d",&x);

  arb[elem+i-1]=x;
  curent=elem+i-1;

  while(curent!=1)
  {
   poz=curent/2;
   arb[poz]+=x;
   curent/=2;
  }
 }

 for(i=1;i<=m;i++)
 {
  fscanf(fin,"%d %d %d",&op,&x,&y);
  if(op==0)
  {
   arb[elem+x-1]-=y;
   curent=elem+x-1;

   while(curent!=1)
   {
    poz=curent/2;
    arb[poz]-=y;
    curent/=2;
   }
  }
  else
   if(op==1)
   {
    sol=0;
    proceseaza(1,x+elem-1,y+elem-1,elem);
    fprintf(fout,"%d\n",sol);
   }
 }

 fclose(fin);
 fclose(fout);

 return 0;
}

void proceseaza(int nodc,int x,int y,int el)
{
 if(x<=nodc*el && (nodc+1)*el-1<=y)
  sol+=arb[nodc];
 else
  if(x<=nodc*el || nodc*el<=y || x<=(nodc+1)*el-1 || (nodc+1)*el-1<=y)
   if(el)
   {
    proceseaza(nodc*2,x,y,el/2);
    proceseaza(nodc*2+1,x,y,el/2);
   }
}