Pagini recente » Cod sursa (job #3233861) | Cod sursa (job #2349978) | Cod sursa (job #3236734) | Cod sursa (job #2763654) | Cod sursa (job #2788819)
#include <bits/stdc++.h>
using namespace std;
template <class T> class AIB
{
private:
T* tree, lung;
T sum_part(int poz)
{
T rez = 0;
while (poz)
{
rez += tree[poz];
poz -= poz & -poz;
}
return rez;
}
public:
AIB(int lungime)
{
lung = lungime;
tree = new T[lung + 1]();
}
~AIB()
{
delete[] tree;
}
void add(int poz, T value)
{
while (poz <= lung)
{
tree[poz] += value;
poz += poz & -poz;
}
}
T sum(int st, int dr)
{
return sum_part(dr) - sum_part(st - 1);
}
};
int n, q;
ifstream fin("aib.in");
ofstream fout("aib.out");
int main()
{
fin >> n >> q;
AIB <int> aib(n);
for (int i = 1, val; i <= n; i++)
{
fin >> val;
aib.add(i, val);
}
for (int t, a, b; q; q--)
{
fin >> t >> a;
if (t != 2)
fin >> b;
switch (t)
{
case 0: aib.add(a, b); break;
case 1: fout << aib.sum(a, b) << '\n'; break;
case 2:
int poz = 0;
for (int p = n; p; p /= 2)
while (poz + p <= n && aib.sum(1, poz + p) < a)
poz += p;
if (poz < n && aib.sum(1, poz + 1) == a)
fout << poz + 1 << '\n';
else
fout << "-1\n";
}
}
return 0;
}