Pagini recente » Arhiva de probleme | Profil StarGold2 | Cod sursa (job #1406750) | Infoarena Monthly 2014 - Runda 7 | Cod sursa (job #3277943)
#include <fstream>
#define case break; case
using namespace std;
ifstream in;
ofstream ou;
int n, m, aib[100005];
void update(int pos, int x)
{
while (pos <= n)
{
aib[pos] += x;
pos += pos & -pos;
}
}
int query(int pos)
{
int r = 0;
while (pos > 0)
{
r += aib[pos];
pos -= pos & -pos;
}
return r;
}
int main()
{
in.open("aib.in");
ou.open("aib.out");
in >> n >> m;
for (int i = 1; i <= n; i++)
{
int x;
in >> x;
update(i, x);
}
for (int i = 0; i < m; i++)
{
int t;
in >> t;
switch(t)
{
case 0:
{
int a, b;
in >> a >> b;
update(a, b);
}
case 1:
{
int a, b;
in >> a >> b;
ou << query(b) - query(a-1) << "\n";
}
case 2:
{
int x;
in >> x;
int st = 1, dr = n;
while (st < dr)
{
int mij = (st + dr) / 2;
if (query(mij) >= x)
{
dr = mij;
}
else
{
st = mij+1;
}
}
ou << st << "\n";
}
}
}
}