Pagini recente » Cod sursa (job #1972388) | Cod sursa (job #470761) | Cod sursa (job #1378311) | Cod sursa (job #940267) | Cod sursa (job #2668808)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("aib.in");
ofstream out("aib.out");
int aib[100001], n, q, p2n=1;
int aux, cer, a, b;
void update(int poz, int val)
{
int p2=0;
while(poz<=n)
{
aib[poz]+=val;
while(!(poz & (1<<p2))) ++p2;
poz+=(1<<p2);
++p2;
}
}
int query(int poz)
{
int p2=0, sum=0;
while(poz)
{
sum+=aib[poz];
while(!(poz & (1<<p2))) ++p2;
poz-=(1<<p2);
++p2;
}
return sum;
}
int getK(int val)
{
int step=p2n, i, sum=0;
for(i=0; step; step>>=1)
if(step+i<=n)
{
if(aib[step+i]<=val) val-=aib[step+i], i+=step;
if(!val) return i;
}
return -1;
}
void readit()
{
in>>n>>q;
for(int i=1; i<=n; i++) in>>aux, update(i, aux);
while(p2n<n) p2n<<=1;
}
int main()
{
readit();
while(q--)
{
in>>cer>>a;
if(cer<2) in>>b;
if(cer==0) update(a, b);
else if(cer==1) out<<query(b)-query(a-1)<<'\n';
else out<<getK(a)<<'\n';
}
return 0;
}