Cod sursa(job #276008)

Utilizator me_andyAvramescu Andrei me_andy Data 10 martie 2009 19:49:39
Problema Arbori indexati binar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include<fstream.h>


  ifstream f("aib.in");
  ofstream g("aib.out");
   long a[100001],n,m,i,suma,j,x,y,z;


void ph(int x,int poz)
{
 int c=0;
 while(poz<=n)
 {
  a[poz]+=x;
  while(!(poz&(1<<c))) c++;
  poz+=(1<<c);
  c+=1;
 }
}


int func(int poz)
{
 int c=0,s=0;
 while(poz>=1)
 {
  s+=a[poz];
  while(!(poz&(1<<c))) c++;
  poz-=(1<<c);
  c+=1;
 }
 return s;
}

int min(int st,int dr)
{
 int x,y;
 if(st>dr) return -1;
 else
 {
  x=(st+dr)/2;
  y=func(x);
  if(y>suma) return min(st,x-1);
  else if(y<suma) return min(x+1,dr);
  else return x;
 }

}

int main()
{
 f>>n>>m;
 for(i=1;i<=n;i++)
 {
  f>>x;
  ph(x,i);
 }
 for(i=1;i<=m;i++)
 {
  f>>x;
  if(x==0)
  {
   f>>y>>z;
   ph(z,y);
  }
  if(x==1)
  {
   f>>y>>z;
   g<<(func(z)-func(y-1))<<"\n";
  }
  if(x==2)
  {
   f>>suma;
   g<<min(1,n)<<"\n";
  }
 }
 return 0;
}