Pagini recente » Cod sursa (job #282032) | Cod sursa (job #3258786) | Cod sursa (job #985301) | Cod sursa (job #996726) | Cod sursa (job #2949327)
#include <fstream>
#include <vector>
using namespace std;
const int nmax = 1e5;
struct fenwick {
vector < int > aib;
int N;
void init ( int n ) {
N = n;
aib.resize ( n + 1 );
}
void update ( int poz, int val ) {
for ( ; poz <= N; poz += poz & -poz )
aib[poz] += val;
}
int query ( int poz ) {
int s = 0;
for ( ; poz > 0; poz -= poz & -poz )
s += aib[poz];
return s;
}
int search ( int sum ) {
int poz = 0;
for ( int pas = ( 1 << 16 ); pas >= 1; pas >>= 1 )
if ( poz + pas <= N && aib[poz + pas] <= sum )
sum -= aib[poz + pas], poz += pas;
return ( sum == 0 ? poz : -1 );
}
}Aib;
ifstream fin ( "aib.in" );
ofstream fout ( "aib.out" );
int main() {
int n, q, x, tip, a, b;
fin >> n >> q;
Aib.init ( n );
for ( int i = 1; i <= n; i++ ) {
fin >> x;
Aib.aib[i] += x;
if ( i + ( i & -i ) <= n )
Aib.aib[i + ( i & -i )] += Aib.aib[i];
}
while ( q-- ) {
fin >> tip >> a;
if ( tip == 0 ) {
fin >> b;
Aib.update ( a, b );
} else if ( tip == 1 ) {
fin >> b;
fout << Aib.query ( b ) - Aib.query ( a - 1 ) << '\n';
} else
fout << ( a == 0 ? -1 : Aib.search ( a ) ) << '\n';
}
return 0;
}