Pagini recente » Cod sursa (job #465990) | Cod sursa (job #1253894) | Cod sursa (job #798347) | Cod sursa (job #2409613) | Cod sursa (job #754193)
Cod sursa(job #754193)
#include <fstream>
#include <cstdlib>
using namespace std;
const int N_MAX=100011;
int v[N_MAX];
inline int log2(int x) {return 8*sizeof(int) - 1 - __builtin_clz(x);}
void Update(int N, int xIndex, int xValue)
{
for(; xIndex <= N; xIndex+=(xIndex & -xIndex))
v[xIndex]+=xValue;
}
int Sum(int xIndex)
{
int s=0;
for(; xIndex; xIndex-=(xIndex & -xIndex))
s+=v[xIndex];
return s;
}
int getIndex(int N, int s)
{
int start=1<<log2(N), now, think;
for(think=0; start; start>>=1)
{
now=start+think;
if(now <= N)
{
if(s == v[now])
return now;
if(s > v[now])
{
s-=v[now];
think=now;
}
}
}
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;
if(0 == op)
{
in>>a>>b;
Update(N, a, b);
}
else if(1 == op)
{
in>>a>>b;
out<<(Sum(b) - Sum(a-1))<<'\n';
}
else if(2 == op)
{
in>>b;
out<<getIndex(N, b)<<'\n';
}
}
return EXIT_SUCCESS;
}