Cod sursa(job #2754904)

Utilizator MadalinaKopaczMadalina Kopacz MadalinaKopacz Data 26 mai 2021 17:50:52
Problema Order Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.55 kb
#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;
}