Pagini recente » Cod sursa (job #2215870) | Cod sursa (job #427339) | Cod sursa (job #2647899) | Cod sursa (job #1335901) | Cod sursa (job #743821)
Cod sursa(job #743821)
#include<iostream>
#include<fstream>
using namespace std;
ifstream in("aib.in");
ofstream out("aib.out");
inline int minim(int a, int b)
{
return (a < b ? a : b);
}
int m, n, t;
int aib[100001];
int c, s;
int search(int val)
{
int i, pas;
for(pas = 1; pas < n; pas <<= 1);
for(i = 0; pas; pas >>= 1)
if(i + pas <= n)
if(val >= aib[i + pas]){
i += pas;
val -= aib[i];
if(!val) return i;
}
return -1;
}
void update(int poz, int val)
{
c = 0;
while(poz <= n){
aib[poz] += val;
while(!(poz & (1 << c))) c++;
poz += (1 << c);
c++;
}
}
int query(int poz)
{
c = 0; s = 0;
while(poz > 0){
s += aib[poz];
while(!(poz & (1 << c))) c++;
poz -= (1 << c);
c++;
}
return s;
}
int main()
{
int k, x, y, i;
in >> n >> m;
for(i = 1; i <= n; ++i){
in >> t;
update(i, t);
}
while(m--){
in >> k;
if(k < 2){
in >> x >> y;
if(!k) update(x, y);
else out << (query(y) - query(x-1)) << "\n";
}
else{
in >> x;
out << search(x) << "\n";
}
}
return 0;
}