Pagini recente » Cod sursa (job #64266) | Cod sursa (job #35493) | Cod sursa (job #1908576) | Cod sursa (job #2211442) | Cod sursa (job #1507382)
#include <fstream>
using namespace std;
ifstream fin ("aib.in");
ofstream fout ("aib.out");
int n, m, AIB[100010];
int zeros(int &x)
{
return (x ^ (x - 1) & x);
}
void Update(int poz, int value)
{
while (poz <= n)
{
AIB[poz] += value;
poz += zeros(poz);
}
}
int Query(int poz, int sum = 0)
{
while (poz)
{
sum += AIB[poz];
poz -= zeros(poz);
}
return sum;
}
int Binary_Search(int &sum)
{
int step, i;
for (step = 1; step <= n; step <<= 1);
step >>= 1;
for (i = 0; step; step >>= 1)
{
if (i + step <= n)
{
if (AIB[i + step] <= sum)
{
i += step;
sum -= AIB[i];
if (!sum) return i;
}
}
}
return -1;
}
int main()
{
fin >> n >> m;
for (int i = 1, x; i <= n; i++)
{
fin >> x;
Update(i, x);
}
for (int i = 1, a, b, tip; i <= m; i++)
{
fin >> tip;
switch (tip)
{
case 0 :
{
fin >> a >> b;
Update(a, b);
break;
}
case 1 :
{
fin >> a >> b;
fout << Query(b) - Query(a - 1) << '\n';
break;
}
case 2 :
{
fin >> a;
fout << Binary_Search(a) << '\n';
break;
}
}
}
fout.close();
return 0;
}