Pagini recente » Cod sursa (job #1262752) | Cod sursa (job #1073215) | Cod sursa (job #266882) | Cod sursa (job #2974318) | Cod sursa (job #2445208)
#include <fstream>
using namespace std;
ifstream fin("aib.in");
ofstream fout("aib.out");
const int NMAX = 1e5;
int N, M, p;
int aib[NMAX + 5];
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 s)
{
int cursor = 0, q = p;
while(q > 0)
{
if(s - aib[cursor + q] >= 0 && cursor + p <= N)
{
s -= aib[cursor + q];
cursor += q;
}
if(s == 0)
return cursor;
q >>= 1;
}
return -1;
}
int main()
{
fin >> N >> M;
p = 1;
while(p <= N)
p <<= 1;
p >>= 1;
for(int i = 1; i <= N; i++)
{
int x;
fin >> x;
Update(i, x);
}
for(int i = 1; i <= M; i++)
{
int t;
fin >> t;
if(t == 2)
{
int a;
fin >> a;
fout << Search(a) << '\n';
}
else if(t == 1)
{
int a, b;
fin >> a >> b;
fout << Query(b) - Query(a - 1) << '\n';
}
else
{
int a, b;
fin >> a >> b;
Update(a, b);
}
}
return 0;
}