Pagini recente » Cod sursa (job #618548) | Cod sursa (job #671663) | Cod sursa (job #1526448) | Cod sursa (job #30648) | Cod sursa (job #220933)
Cod sursa(job #220933)
#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;
}