Pagini recente » Cod sursa (job #1729329) | Cod sursa (job #2424512) | Istoria paginii runda/colinde1 | Statistici Butiri Cristian (butiricristian) | Cod sursa (job #969117)
Cod sursa(job #969117)
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
#include <algorithm>
#include <ctime>
#include <utility>
using namespace std;
ifstream cin("aib.in");
ofstream cout("aib.out");
const int MAXN = 100005;
int n, m;
class aib
{
private:
int a[MAXN];
public :
inline int lsb(int x)
{
return (x^(x-1))&x;
}
void update(int where, int value)
{
while(where <= n)
{
a[where] += value;
where += lsb(where);
}
}
int query(int where)
{
int sum = 0;
while(where > 0)
{
sum += a[where];
where -= lsb(where);
}
return sum;
}
int Search(int val)
{
int li = 1, ls = n, x, gasit = -1;
while (li <= ls)
{
x = (li+ls) / 2;
int aa = query(x);
if (aa >= val)
{
if (aa == val)
gasit = x;
ls = x-1;
}
else
li = x+1;
}
return gasit;
}
};
int main()
{
aib Arbore;
cin >> n >> m;
for(int i = 1 ; i <= n ; ++ i)
{
int x;
cin >> x;
Arbore.update(i, x);
}
for(int i = 1 ; i <= m ; ++ i)
{
int op;
cin >> op;
if(op == 0)
{
int pozi, valo;
cin >> pozi >> valo;
Arbore.update(pozi, valo);
}
else if(op == 1)
{
int x, y;
cin >> x >> y;
cout << Arbore.query(y) - Arbore.query(x-1) << "\n";
}
else if(op == 2)
{
int x;
cin >> x;
cout << Arbore.Search(x) << "\n";
}
}
return 0;
}