Pagini recente » Cod sursa (job #51363) | Cod sursa (job #2479960) | Cod sursa (job #2689261) | Cod sursa (job #2419874) | Cod sursa (job #3030913)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("aib.in");
ofstream fout("aib.out");
int n, m, i, j, a, b, c;
vector <int> aib;
void update (int pos, int val)
{
for (int i = pos; i <= n; i += i&(-i))
aib[i] += val;
}
int query (int pos)
{
int sum = 0;
for (int i = pos; i > 0; i -= i&(-i))
sum += aib[i];
return sum;
}
int where (int val)
{
int pos = 1, cp = 0;
while (pos < n)
pos *= 2;
for (; pos > 0; pos /= 2)
{
if (cp+pos <= n && val >= aib[cp+pos])
{
cp += pos;
val -= aib[cp];
if (val == 0) return cp;
}
}
return -1;
}
int main()
{
fin >> n >> m; aib.resize(n+1);
for (i = 1; i <= n; i++)
{
fin >> a;
update(i, a);
}
for (i = 1; i <= m; i++)
{
fin >> c >> a;
if (c == 0)
{
fin >> b;
update(a, b);
}
if (c == 1)
{
fin >> b;
fout << query(b)-query(a-1) << '\n';
}
if (c == 2)
{
fout << where(a) <<'\n';
}
}
return 0;
}