Pagini recente » Cod sursa (job #3208700) | Cod sursa (job #3192730)
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
ifstream fin("aib.in");
ofstream fout("aib.out");
#define cin fin
#define cout fout
const int MAXN = 1e5 + 2;
int tree[MAXN];
int N, M, a, b, t;
void add(int poz, int val)
{
for(int i = poz; i <= N; i += (i & (-i)))
tree[i] += val;
}
int sum(int n)
{
int rez = 0;
for(int i = n; i > 0; i -= (i & (-i)))
rez += tree[i];
return rez;
}
int minpos(int val) {
int mask = 1;
while ( mask <= N) {
mask <<=1;
}
mask >>= 1;
int position = 0;
for (; mask > 0; mask >>= 1) {
if ( position + mask <= N && tree[position + mask] <= val ) {
val -= tree[position + mask];
position += mask;
if ( val == 0 ) return position;
}
}
return -1;
}
int main()
{
ios_base::sync_with_stdio(false);
cin >> N >> M;
for (int i = 1; i <= N; ++i)
{
cin >> t;
for (int j = i; j <= N; j += (j & (-j)))
tree[j] += t;
}
while(M--)
{
cin >> t >> a;
if(t == 2)
{
cout << minpos(a) << endl;
continue;
}
cin >> b;
if(t == 0) {
add(a, b);
continue;
}
cout << sum(b) - sum(a - 1) << endl;
}
return 0;
}