Pagini recente » Cod sursa (job #1386460) | Cod sursa (job #2027801) | Cod sursa (job #1469954) | Cod sursa (job #15787) | Cod sursa (job #1379660)
#include <fstream>
const int MAX_N(100001);
const int LOG2(16);
int Bit [MAX_N];
int n;
inline int Lsb (int x)
{
return x & -x;
}
inline void Update (int index, int val)
{
while (index <= n)
{
Bit[index] += val;
index += Lsb(index);
}
}
inline int Query (int index)
{
int result(0);
while (index)
{
result += Bit[index];
index -= Lsb(index);
}
return result;
}
int main (void)
{
std::ifstream input("aib.in");
int m, code, x, y;
input >> n >> m;
for (int i(1) ; i <= n ; ++i)
{
input >> x;
Update(i,x);
}
std::ofstream output("aib.out");
while (m)
{
input >> code;
if (code == 0 || code == 1)
input >> x >> y;
else
input >> x;
if (code == 0)
Update(x,y);
else if (code == 1)
output << Query(y) - Query(x - 1) << '\n';
else
{
int value(0), index(0);
for (int i(LOG2) ; i >= 0 ; --i)
if (index + (1 << i) <= n && value + Bit[index + (1 << i)] <= x)
{
if (value + Bit[index + (1 << i)] == x && Query(index + (1 << i)) == Query(index + (1 << i) - 1))
continue;
index += (1 << i);
value += Bit[index];
}
output << (value == x ? index : -1) << '\n';
}
--m;
}
return 0;
}