Pagini recente » Cod sursa (job #2806395) | Cod sursa (job #878630) | Cod sursa (job #1275118) | Cod sursa (job #2313234) | Cod sursa (job #2456344)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("aib.in");
ofstream out("aib.out");
const int dim = 100005;
int n,m,aib[dim];
void update(int poz,int val)
{
while (poz <= n)
{
aib[poz] += val;
poz += poz & (-poz);
}
}
int query(int dr)
{
int s = 0;
while (dr != 0)
{
s += aib[dr];
dr -= dr & (-dr);
}
return s;
}
int main()
{
in >> n >> m;
int a;
int log = 1;
while (log <= n)
{
log *= 2;
}
log /= 2;
for (int i=1; i<=n; i++)
{
in >> a;
update(i,a);
}
int op,b;
for (int i=1; i<=m; i++)
{
in >> op >> a;
if (op == 0)
{
in >> b;
update(a,b);
}
if (op == 1)
{
in >> b;
out << query(b) - query(a-1) << "\n";
}
if (op == 2)
{
int baza = 0;
int off = log,s = 0,ok = 0;
while (off >= 1 && ok == 0)
{
while (baza + off > n)
{
off /= 2;
}
s += aib[baza + off];
if (s < a)
{
baza += off;
off /= 2;
}
else if (s > a)
{
s -= aib[baza + off];
off /= 2;
}
else
{
out << baza + off << "\n";
ok = 1;
}
}
if (ok == 0)
{
out << "-1\n";
}
}
}
return 0;
}