Pagini recente » Cod sursa (job #2726235) | Cod sursa (job #1375646) | Cod sursa (job #298467) | Cod sursa (job #1507986) | Cod sursa (job #1943655)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("aib.in");
ofstream fout("aib.out");
int n, m, i, x, y, op;
int AIB[ 250005 ];
void update( int pos, int val ) {
while ( pos <= n ) {
AIB[ pos ] += val;
pos += pos & (-pos);
}
}
int interval( int pos ) {
int sum = 0;
while ( pos > 0 ) {
sum += AIB[ pos ];
pos -= pos & (-pos);
}
return sum;
}
int query( int k ) {
int pos = 1, sum = 0;
while ( sum < k ) {
if ( sum + AIB[ pos ] < k ) {
pos += pos & (-pos);
} else if ( sum + AIB[ pos ] > k ) {
sum += AIB[ pos ];
pos++;
} else {
return pos;
}
}
return -1;
}
int main()
{
fin >> n >> m;
for ( i = 1 ; i <= n ; i++ ) {
fin >> x;
update( i , x );
}
for ( i = 1 ; i <= m ; i++ ) {
fin >> op >> x;
if ( op == 0 ) {
fin >> y;
update( x , y );
} else if ( op == 1 ) {
fin >> y;
fout << interval( y ) - interval( x - 1 ) << '\n';
} else {
fout << query( x ) << '\n';
}
}
return 0;
}