Pagini recente » Cod sursa (job #2404703) | Cod sursa (job #1950465) | Cod sursa (job #2915525) | Cod sursa (job #1533956) | Cod sursa (job #1943690)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("datorii.in");
ofstream fout("datorii.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, prev = 1;
while ( sum < k ) {
if ( sum + AIB[ pos ] < k && pos <= n ) {
prev = pos;
pos += pos & (-pos);
} else if ( sum + AIB[ pos ] == k ) {
return pos;
} else {
pos = prev;
sum += AIB[ pos ];
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;
}