Pagini recente » Cod sursa (job #717114) | Cod sursa (job #2443403) | Cod sursa (job #1265465) | Cod sursa (job #1895643) | Cod sursa (job #147093)
Cod sursa(job #147093)
#include <stdio.h>
#include <fstream>
#include <assert.h>
using namespace std;
#define in "aib.in"
#define out "aib.out"
#define dim 100001
int N, M, T;
int A[dim], Arb[dim];
int C, S;
void Update(int,int);
int Query(int);
int main()
{
memset(Arb,0,sizeof(Arb));
int K, X, Y;
freopen(in,"r",stdin);
freopen(out,"w",stdout);
scanf("%d%d", &N, &M);
for ( int i = 1; i <= N; i++ )
{
scanf("%d", &A[i]);
assert( 1 <= A[i] && A[i] <= 10000 );
Update(i,A[i]);
}
for ( ; M; M-- )
{
scanf("%d%d%d", &K, &X, &Y);
if ( K == 0 )
{
assert( 1 <= Y && Y <= N && X <= Y);
assert( 1 <= X && X <= N );
printf("%d\n", Query(Y)-Query(X-1));
}
else
{
assert( 1 <= X && X <= N );
assert( 1 <= Y && Y <= 10000 );
Update(X,Y);
}
}
}
void Update(int poz, int val)
{
C = 0;
while ( poz <= N )
{
Arb[poz] += val;
while ( !(poz & (1<<C)) ) C++;
poz += (1<<C);
C += 1;
}
}
int Query(int poz)
{
C = 0, S = 0;
while ( poz > 0 )
{
S += Arb[poz];
while ( !(poz & (1<<C)) ) C++;
poz -= (1<<C);
C += 1;
}
return S;
}