Pagini recente » Cod sursa (job #1976201) | Cod sursa (job #646322) | Cod sursa (job #1705187) | Cod sursa (job #1804954) | Cod sursa (job #2243861)
#include <fstream>
using namespace std;
ifstream fin("aib.in");
ofstream fout("aib.out");
int aib[132000];
int n, m, x, q, a, b;
void apdate(int pos, int val) {
while (pos <= n) {
aib[pos] += val;
pos += pos & (-pos);
}
}
int intSum(int pos) {
if (pos == 0) return 0;
return aib[pos] + intSum(pos - (pos & (-pos)));
}
int findPos(int pos, int sum) {
int s = intSum(pos);
int aux = (pos & (-pos)) >> 1;
if (s == sum) return pos;
if (aux == 0) return -1;
if (sum < s) return findPos(pos - aux, sum);
if (sum > s) return findPos(pos + aux, sum);
}
int main()
{
fin >> n >> m;
for (int i = 1; i <= n; i++) {
fin >> x;
apdate(i, x);
}
for (int i = 1; i <= m; i++) {
fin >> q;
switch (q) {
case 0: {
fin >> a >> b;
apdate(a, b);
} break;
case 1: {
fin >> a >> b;
fout << intSum(b) - intSum(a - 1) << '\n';
} break;
case 2: {
fin >> a;
fout << findPos(n, a) << '\n';
}
}
}
return 0;
}