Cod sursa(job #220934)

Utilizator Bogdan_CCebere Bogdan Bogdan_C Data 13 noiembrie 2008 19:44:58
Problema Arbori indexati binar Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.22 kb
#include <fstream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int c[100001],n,m,poz;

void update(int locat,int val)
      {int loc=locat;
      poz=0;
       while(loc<=n)
        {c[loc]+=val;
          while(!(loc&(1<<poz))) poz++;
          loc+=1<<poz;
          poz++;     }}

int interogare(int st)
{int x=st;  poz=0;
 int sum1=0;
  while(x>0)
    {sum1+=c[x];
     while(!(x&(1<<poz))) poz++;
     x-=1<<poz;
     poz++;
    }
return sum1;
}
 int search(int val)
 {
     int i, step;

     for ( step = 1; step < n; step <<= 1 );

     for( i = 0; step; step >>= 1 )
     {
          if( i + step <= n)
          {
             if( val >= c[i+step] )
             {
                 i += step, val -= c[i];
                 if ( !val ) return i;
             }
          }
     }

     return -1;
 }

int main()
{    ifstream in("aib.in");
ofstream out("aib.out");
      in>>n;in>>m; int k,x,y;
      for(int i=1;i<=n;i++)
       in>>c[i];
      for(;m;m--)
      {in>>k;
       if(k<2)
        {in>>x>>y;
         if(k==0) update(x,y);
         else out<<(interogare(y)-interogare(x-1))<<'\n';}
       else {in>>x;out<<search(x)<<'\n';}
      }

      return 0;
}