Pagini recente » Cod sursa (job #2959229) | Cod sursa (job #711823) | Cod sursa (job #2708003) | Cod sursa (job #530292) | Cod sursa (job #2864296)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("aib.in");
ofstream fout("aib.out");
const int nmax = 100005;
int n, aib[nmax], m;
void Update(int x, int p)
{
while(p <= n)
{
aib[p] += x;
p += (p & ( -p));
}
}
int Query(int p)
{
int sum = 0;
while(p >= 1)
{
sum += aib[p];
p -= (p & (-p));
}
return sum;
}
int Cautbin(int x)
{
int st = 1, dr = n, mij, poz = -1;
while(st <= dr)
{
mij = (st + dr) / 2;
if(Query(mij) >= x)
{
dr = mij - 1;
poz = mij;
}
else if(aib[mij] < x)
st = mij + 1;
}
if(poz != -1 && Query(poz) != x)
return -1;
return poz;
}
void Solve()
{
int i, x;
fin >> n >> m;
for(i = 1;i <= n;i++)
{
fin >> x;
Update(x, i);
}
for(i = 1;i <= m;i++)
{
int a, b, op;
fin >> op;
if(op == 0)
{
fin >> a >> b;
Update(b, a);
}
else if(op == 1)
{
fin >> a >> b;
fout << Query(b) - Query(a - 1)<< "\n";
}
else
{
fin >> x;
fout << Cautbin(x) << "\n";
}
}
}
int main()
{
Solve();
return 0;
}