Pagini recente » Cod sursa (job #2944397) | Cod sursa (job #687814) | Cod sursa (job #347733) | Cod sursa (job #3248414) | Cod sursa (job #220939)
Cod sursa(job #220939)
#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 j, step;
for ( step = 1; step < n; step <<= 1 );
for( j = 0; step; step >>= 1 )
{
if( j + step <= n)
{
if( val >= c[j+step] )
{
j += step, val -= c[j];
if ( !val ) return j;
}
}
}
return -1;
}
int main()
{ ifstream in("c:\\aib.in");
ofstream out("aib.out");
in>>n;in>>m; int k,x,y,aux;
for(int i=1;i<=n;i++)
{in>>aux;update(i,aux);}
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;
}