Pagini recente » Cod sursa (job #3165532) | Cod sursa (job #2448191) | Cod sursa (job #3124391) | Cod sursa (job #479515) | Cod sursa (job #1464063)
#include<cstdio>
#define LSB(x) (x & (-x))
using namespace std;
int N, M, x, a, b, t;
int aib[100002];
void Update(int, int);
inline int Query(int);
int Search(int);
int main()
{
int i;
freopen("aib.in","r",stdin);
freopen("aib.out","w",stdout);
scanf("%d%d", &N, &M);
for (i = 1; i <= N; i++)
{
scanf("%d", &x);
Update(i, x);
}
for (i = 1; i <= M; i++)
{
scanf("%d", &t);
if ( t < 2)
{
scanf("%d%d", &a, &b);
if (!t)
Update(a, b);
else
printf("%d\n", Query(b) - Query(a-1));
}
else
{
scanf("%d", &a);
printf("%d\n", Search(a));
}
}
}
void Update(int poz, int val)
{
for (int i = poz; i <= N; i += LSB(i))
aib[i] += val;
}
inline int Query(int poz)
{
int S = 0;
for (int i = poz; i; i -= LSB(i))
S += aib[i];
return S;
}
int Search(int val)
{
int i, step;
for (step = 1; step < N; step <<= 1);
for (i = 0; step; step >>= 1)
{
if (i + step <= N)
{
if (val >= aib[i + step])
{
i += step; val -= aib[i];
if (!val)
return i;
}
}
}
return -1;
}