Pagini recente » Cod sursa (job #1178349) | Cod sursa (job #2878521) | Cod sursa (job #2133357) | Cod sursa (job #2134832) | Cod sursa (job #1100822)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("aib.in");
ofstream fout("aib.out");
const int MAXN = 100005;
int N, M, tree[MAXN];
void Update(int index, int val) {
while (index <= N) {
tree[index] += val;
index += index & -index;
}
}
int Query(int index) {
int sum = 0;
while (index) {
sum += tree[index];
index -= index & -index;
}
return sum;
}
void Read() {
fin >> N >> M;
int nr;
for (int i = 1; i <= N; ++i) {
fin >> nr;
Update(i, nr);
}
}
int BinSearch(int K) {
int Left = 1; int Right = N; int Middle;
while (Right - Left > 1) {
Middle = (Left + Right) >> 1;
if (Query(Middle) >= K)
Right = Middle;
else
Left = Middle;
}
if (Query(Left) == K)
return Left;
if (Query(Right) == K)
return Right;
return -1;
}
int main() {
Read();
int op, a, b;
while (M--) {
fin >> op;
if (op == 2) {
fin >> a;
fout << BinSearch(a) << '\n';
}else {
fin >> a >> b;
if (op == 0)
Update(a, b);
else
fout << Query(b) - Query(a - 1) << '\n';
}
}
fin.close();
fout.close();
return 0;
}