Pagini recente » Cod sursa (job #1959715) | Cod sursa (job #1580177) | Cod sursa (job #601432) | Cod sursa (job #50696) | Cod sursa (job #2982772)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("aib.in");
ofstream fout("aib.out");
int a[100001], n;
int query(int pi) {
int sum = 0;
for ( int i = pi; i > 0; i -= (i&-i) ) {
sum += a[i];
}
return sum;
}
void update(int p, int x) {
for ( int i = p; i <= n; i += (i&-i) ) {
a[i] += x;
}
}
int Search(int x) {
int i;
for ( i = 1; i < n; i <<= 1 ) ;
for ( int j = 0; i; i >>= 1 ) {
if ( i + j <= n ) {
if ( x >= a[i+j] ) {
j += i;
x -= a[j];
}
if ( !x ) return j;
}
}
return -1;
}
int main() {
int m, c, i, x, y;
fin >> n >> m;
for ( i = 1; i <= n; ++i ) {
fin >> x;
update(i, x);
}
//for ( i = 1; i <= n; ++i ) cout << a[i] << " ";
//cout << '\n';
for ( i = 1; i <= m; ++i ) {
fin >> c;
if ( c == 2 ) {
fin >> x;
fout << Search(x) << '\n';
} else {
fin >> x >> y;
if ( c == 0 ) update(x, y);
else fout << query(y) - query(x-1) << '\n';
}
}
return 0;
}