Pagini recente » Cod sursa (job #190294) | Cod sursa (job #1933185) | Cod sursa (job #2937279) | Cod sursa (job #741932) | Cod sursa (job #2454376)
#include <bits/stdc++.h>
#define N 100005
using namespace std;
ifstream fin ("aib.in");
ofstream fout ("aib.out");
int n, m, aib[N];
int lowestBit(int x)
{
return (x & -x);
}
void Update(int pos, int add)
{
for (int i = pos; i <= n; i += lowestBit(i))
aib[i] += add;
}
int Query(int pos)
{
int sum = 0;
for (int i = pos; i; i -= lowestBit(i))
sum += aib[i];
return sum;
}
int main()
{
fin >> n >> m;
for (int i = 1; i <= n; i++)
{
int x;
fin >> x;
Update(i, x);
}
for (int i = 1; i <= m; i++)
{
int op, a, b;
fin >> op;
if (op == 0)
{
fin >> a >> b;
Update(a, b);
}
else if (op == 1)
{
fin >> a >> b;
fout << Query(b) - Query(a - 1) << "\n";
}
else
{
fin >> a;
int st = 1, dr = n;
bool gasit = 0;
while (st <= dr && !gasit)
{
int mij = (st + dr) / 2;
int sum = Query(mij);
if (sum == a)
{
fout << mij << "\n";
gasit = 1;
}
else if (a < sum)
dr = mij - 1;
else
st = mij + 1;
}
if (!gasit)
fout << -1 << "\n";
}
}
return 0;
}