Pagini recente » Cod sursa (job #3262961) | Cod sursa (job #2386686) | Cod sursa (job #363142) | Cod sursa (job #3278688) | Cod sursa (job #751234)
Cod sursa(job #751234)
#include <fstream>
#include <cstdlib>
using namespace std;
const int N_MAX=100011;
int aib[N_MAX];
void Update(const int& N, int xIndex, const int& xValue)
{
for(; xIndex <= N; xIndex+=(xIndex & -xIndex))
aib[xIndex]+=xValue;
}
int Sum(int xIndex)
{
int s;
for(s=0; xIndex; xIndex-=(xIndex & -xIndex))
s+=aib[xIndex];
return s;
}
inline int log2(int x) {return 8*sizeof(x) - 1 - __builtin_clz(x);}
int Search(const int& N, int x)
{
int tidx, mIndex, idx=1<<log2(N);
for(; idx; idx>>=1)
{
tidx=idx+mIndex;
if(tidx <= N && aib[tidx] <= x)
{
x-=aib[tidx];
if(0 == x)
return tidx;
mIndex=tidx;
}
}
return -1;
}
int main()
{
int N, M, i, x, op, a, b;
ifstream in("aib.in");
ofstream out("aib.out");
in>>N>>M;
for(i=1; i <= N; ++i)
{
in>>x;
Update(N, i, x);
}
for(; M; --M)
{
in>>op;
switch(op)
{
case 0 : in>>a>>b; Update(N, a, b); break;
case 1 : in>>a>>b; out<<(Sum(b) - Sum(a-1))<<"\n"; break;
case 2 : in>>b; out<<Search(N, b)<<"\n";
}
}
return EXIT_SUCCESS;
}