Pagini recente » Cod sursa (job #2921487) | Profil Ovidiu-Antonio | Cod sursa (job #1276195) | Cod sursa (job #1006471) | Cod sursa (job #2956016)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f ("aib.in");
ofstream g ("aib.out");
int n,m;
int arbore[100001];
int nr_de_adunat(int x)
{
return x&(-x);
}
void adaugare(int x,int indice)
{
while(indice<=n)
{
arbore[indice]+=x;
indice+=nr_de_adunat(indice);
}
}
int suma(int indice)
{
int sum= 0;
while(indice>=1)
{
sum+=arbore[indice];
indice-=nr_de_adunat(indice);
}
return sum;
}
int cb(int x)
{
int st = 1,dr = n,mij;
int rasp = -1;
while(st<=dr)
{
mij = (st+dr)/2;
if(suma(mij)==x)
{
rasp = mij;
dr = mij-1;
}
else if(suma(mij)<x)
{
st = mij+1;
}
else
dr =mij-1;
}
return rasp;
}
int main()
{
f >> n >> m;
for(int i = 1; i<=n; i++)
{
int x ;
f >> x;
adaugare(x,i);
}
for(int i = 1; i<=m; i++)
{
int c,x,y;
f >> c ;
if(c == 0)
{
f>> x >> y;
adaugare(y,x);
}
else if(c == 1)
{
f>> x >> y;
g << suma(y)-suma(x-1)<<endl;
}
else if(c==2)
{
f >> x;
g << cb(x)<< endl;
}
}
}