Pagini recente » Cod sursa (job #2436960) | Cod sursa (job #2069586) | Monitorul de evaluare | Cod sursa (job #1815828) | Cod sursa (job #1180345)
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <fstream>
using namespace std;
#define in "aib.in"
#define out "aib.out"
#define MAXN 100001
#define MAXM 150001
int n, m, tree[MAXN];
int min(int a, int b)
{
return a < b ? a : b;
}
int last_nonzero_position(int x)
{
return (x & -x);
}
void aib_update(int idx, int val)
{
while (idx <= n)
{
tree[idx] += val;
idx += (idx & -idx);
}
}
int aib_read(int idx)
{
int sum = 0;
while (idx > 0)
{
sum += tree[idx];
idx -= (idx & -idx);
}
return sum;
}
int aib_find_idx_by_val(int left, int right, int val)
{
if (left == right)
{
return (aib_read(left) == val) ? left : -1;
}
int mid = (left + right) / 2;
if (val <= aib_read(mid))
{
return aib_find_idx_by_val(left, mid, val);
}
else
{
return aib_find_idx_by_val(mid + 1, right, val);
}
}
void read_input()
{
ifstream fin(in);
fin >> n >> m;
int val;
for (int i = 1; i <= n; ++i)
{
fin >> val;
aib_update(i, val);
}
ofstream fout(out);
int op, a, b;
for (int i = 1; i <= m; i++)
{
fin >> op;
switch (op)
{
case 0:
fin >> a >> b;
aib_update(a, b);
break;
case 1:
fin >> a >> b;
fout << (aib_read(b) - aib_read(a - 1)) << '\n';
break;
default:
fin >> a;
fout << aib_find_idx_by_val(1, n, a) << '\n';
break;
}
}
fin.close();
fout.close();
}
void print_debug()
{
cout << '\n';
}
void print_solution()
{
}
int main()
{
read_input();
//resolve();
//print_debug();
//print_solution();
//char x;
//cin >> x;
return 0;
}