Pagini recente » Cod sursa (job #1499782) | Cod sursa (job #2139579) | Cod sursa (job #2514403) | Cod sursa (job #3135360) | Cod sursa (job #2754904)
#include <fstream>
#define andBit(x) ( x & (-x) )
using namespace std;
const int NMAX = 1e5 + 5;
int BITree[NMAX], n;
ifstream fin("baruri.in");
ofstream fout("baruri.out");
//documentatie BIT: https://www.youtube.com/watch?v=4SNzC4uNmTA&t=277s
void update(int position, int val)
{
for (int i = position; i <= n; i += andBit(i)) BITree[i] += val;
}
int query(int position)
{
int sum = 0;
for (int i = position; i > 0; i -= andBit(i)) sum += BITree[i];
return sum;
}
int main()
{
int t;
fin >> t;
while (t--) {
fin >> n;
for (int i = 1; i <= n; i++) BITree[i] = 0;//initializare
for (int i = 1; i <= n; i++)
{
int x; fin >> x;
update(i, x);
}
int q;
fin >> q;
while (q--) {
int op;
fin >> op;
if (op == 0) {
int b, d;
fin >> b >> d;
fout << query(min(b + d, n)) - query(max(b - d - 1, 0)) - (query(b) - query(b - 1)) << '\n';
}
if (op == 1) {
int x, b;
fin >> x >> b;
update(b, x);
}
if (op == 2) {
int x, b;
fin >> x >> b;
update(b, -x);
}
if (op == 3) {
int x, b1, b2;
fin >> x >> b1 >> b2;
update(b1, -x);
update(b2, x);
}
}
}
return 0;
}