Pagini recente » Cod sursa (job #2969069) | Cod sursa (job #490268)
Cod sursa(job #490268)
// Arbori indexati binar
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("aib.in");
ofstream g("aib.out");
int arb[100100],m,n,x,a,b,k;
void update (int poz,int val)
{
k=0;
while(poz<=n)
{
arb[poz]+=val;
while( !(poz&1<<k) ) k++;
poz=poz+(1<<k);
k++;
}
}
int query (int poz)
{
int s=0;
k=0;
while(poz>0)
{
s=s+arb[poz];
while( !(poz&1<<k) ) k++;
poz=poz-(1<<k);
k++;
}
return s;
}
int main()
{
int i,j,suma;
f>>n>>m;
for(i=1;i<=n;i++)
{
f>>x;
update(i,x);
}
for(i=1;i<=m;i++)
{
f>>x;
if(x==0)
{
f>>a>>b;
update(a,b);
}
else
if(x==1)
{
f>>a>>b;
g<<query(b)-query(a-1)<<"\n";
}
else
if(x==2)
{
f>>a;
j=1;
do
{
suma=query(j);
if(suma==a)
g<<j<<"\n";
j++;
}
while(suma<a);
}
}
return 0;
}