Pagini recente » Cod sursa (job #2445291) | Cod sursa (job #960485) | Cod sursa (job #1480041) | Cod sursa (job #2081038) | Cod sursa (job #1363241)
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define fs first
#define sc second
#define pob pop_back
#define pub push_back
#define eps 1E-7
#define sz(a) a.size()
#define count_one __builtin_popcount;
#define count_onell __builtin_popcountll;
#define fastIO ios_base::sync_with_stdio(false)
#define PI (acos(-1.0))
#define linf (1LL<<62)//>4e18
#define inf (0x7f7f7f7f)//>2e9
#define DEBUG 1
#ifdef DEBUG
#define D(x) x
#else
#define D(x)
#endif
#define LIM 10001
FILE *in = fopen("aib.in", "r");
FILE *out = fopen("aib.out", "w");
int aib[LIM], n, m, x;
void update(int idx, int val) {
while(idx <= n) {
aib[idx] += val;
idx += idx & -idx;
}
}
int find(int idx) {
//cout << idx << ":"<<(idx & -idx)<<":"<< idx - (idx & -idx) << "\n";
int sum = 0;//aib[idx];
while(idx > 0) {
sum += aib[idx];
idx -= (idx & -idx);
}
return sum;
}
int search(int val) {
if(!val) return -1;
int pos, step;
for(pos = 0, step = (1 << 20); step; step >>= 1) {
if(pos + step <= n && aib[pos+step] <= val)
val -= aib[pos+step], pos += step;
}
if(val > 0) return -1;
return pos;
}
int main()
{
fscanf(in, "%d%d", &n, &m);
for(int i = 1; i <= n; ++i) {
fscanf(in, "%d", &x);
update(i, x);
}
int type, pos;
while(m--) {
fscanf(in, "%d", &type);
switch(type) {
case 0:
fscanf(in, "%d%d", &pos, &x);
update(pos, x);
break;
case 1:
fscanf(in, "%d%d", &pos, &x);
cout << find(x) - find(pos - 1) << "\n";
break;
case 2:
fscanf(in, "%d", &x);
cout << search(x) << "\n";
break;
}
}
/*for(int i = 1; i <= n; ++i) {
cout << aib[i] << " ";
}*/
return 0;
}