Pagini recente » Cod sursa (job #1398583) | Cod sursa (job #2366348) | Statistici Veleat Valentina-Georgiana (valentina_veleat) | Cod sursa (job #3219878) | Cod sursa (job #1654124)
#include<iostream>
#include<fstream>
using namespace std;
ifstream f("aib.in");
ofstream g("aib.out");
int N, M, copac[100001];
void update(int pos, int x) {
for (int i = pos; i <= N; i += (-i&i)){//(i&~(i-1))
copac[i] += x;
}
}
int query(int pos) {//sum 1...pos
int ans = 0;
for (int i = pos; i > 0; i -= (-i&i))
{
ans += copac[i];
}
return ans;
}
void citire()
{
int val;
f >> N >> M;
for (int i = 1; i <= N; i++)
{
f >> val;
update(i, val);
}
}
int main()
{
citire();
int op,a,b;
for (int i = 0; i < M; i++)
{
f >> op;
if (op == 0)
{
f>> a >> b;
update(a, b);
}
if (op == 1) {
int ans;
f >> a >> b;
ans = query(b);
ans -= query(a - 1);
g << ans << '\n';
}
if (op == 2) {
f >> a;
int mij,st=1,dr=N,ras=-1,x;
while (st <= dr) {
mij = (st + dr) / 2;
x = query(mij);
if (x < a)
{
st = mij + 1;
}
else
{
if (x == a) {
ras = mij;
}
dr = mij - 1;
}
}
//if (ras != -1)
g << ras << '\n';
}
}
return 0;
}