Pagini recente » Cod sursa (job #462535) | Cod sursa (job #1563509) | Cod sursa (job #1125620) | Cod sursa (job #2191319) | Cod sursa (job #1704224)
#include <fstream>
using namespace std;
ifstream f("aib.in");
ofstream g("aib.out");
int n, m;
int arb[100005];
void adauga(int poz, int x){
while (poz <= n){
arb[poz] += x;
poz += (poz ^ (poz-1)) & poz;
}
}
int suma(int poz){
int s = 0;
while (poz > 0){
s += arb[poz];
poz -= (poz ^ (poz-1)) & poz;
}
return s;
}
int caut(int x){
int k = 1, i;
while (k < n)
k *= 2;
i = 0;
while (k > 0){
if (i + k <= n){
if (suma(i+k) == x)
return i+k;
else if (suma(i+k) < x)
i += k;
}
k /= 2;
}
return -1;
}
void citire(){
int x, i, a, b, tip;
f >> n >> m;
for (i = 1; i <= n; i++){
f >> x;
adauga(i, x);
}
while (m > 0){
f >> tip;
if (tip < 2){
f >> a >> b;
if (tip == 0)
adauga(a, b);
else
g << suma(b) - suma(a-1) << '\n';
}
else{
f >> a;
g << caut(a) << '\n';
}
m--;
}
}
int main(){
citire();
return 0;
}