Pagini recente » Clasament test_practic_pa_1 | Cod sursa (job #2504420) | Cod sursa (job #2120979) | Cod sursa (job #2127952) | Cod sursa (job #3192719)
#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 position;
}
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;
}