Pagini recente » Cod sursa (job #1165344) | Istoria paginii runda/simulare_oji2020_31_10_2019 | Cod sursa (job #1702422) | Cod sursa (job #1624322) | Cod sursa (job #2367326)
#include <fstream>
using namespace std;
ifstream fin("aib.in");
ofstream fout("aib.out");
const int NMAX = 100000 + 5;
int N, M, p = 1;
int aib[NMAX];
inline int LastBit(int x)
{
return x & (-x);
}
void Update(int pos, int val)
{
for(int i = pos; i <= N; i += LastBit(i))
aib[i] += val;
}
int Query(int pos)
{
int ans = 0;
for(int i = pos; i > 0; i -= LastBit(i))
ans += aib[i];
return ans;
}
int Search(int sum)
{
if(sum == 0)
return -1;
int step = p;
int cursor = 0;
for(; step > 0; step >>= 1)
if(cursor + step <= N && aib[cursor + step] <= sum)
{
sum -= aib[cursor + step];
cursor += step;
if(sum == 0)
return cursor;
}
return -1;
}
int main()
{
int type, x, y;
fin >> N >> M;
for(; p <= N; p <<= 1);
for(int i = 1; i <= N; i++)
{
fin >> x;
Update(i, x);
}
for(int i = 1; i <= M; i++)
{
fin >> type;
if(type == 0)
{
fin >> x >> y;
Update(x, y);
}
else if(type == 1)
{
fin >> x >> y;
fout << Query(y) - Query(x - 1) << '\n';
}
else
{
fin >> x;
fout << Search(x) << '\n';
}
}
return 0;
}