Cod sursa(job #3263898)

Utilizator M132M132 M132 M132 Data 16 decembrie 2024 22:14:18
Problema Hotel Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("baruri.in");
ofstream g("baruri.out");

const int NMAX = 100000;
int T, n, m, aib[NMAX + 5];

void update(int poz,int val)
{
    for(int i = poz; i <= n; i += (-i&i))
        aib[i] += val;
}

int query(int poz)
{
    int sum = 0;
    for(int i = poz; i >= 1; i -= (-i&i))
        sum += aib[i];
    return sum;
}
int main()
{
    int i, j, x, c, a, b;
    f >> T;
    for(i = 1; i <= T; ++i)
    {
        f >> n;
        for(j = 1; j <= n; j++)
        {
            f >> x;
            update(i,x);
        }
        f >> m;
        for(j = 1; j <= m; ++j)
        {
            f >> c;
            if(c == 0)
            {
                f >> a >>b;
                g << query(a - 1) - query(max(a - b - 1, 0)) + query(min(a + b, n))-query(a) << "\n";
            }
            else
                if(c == 1)
                {
                    f >> a >> b;
                    update(b,a);
                }
                else
                    if(c == 2)
                    {
                        f >> a >> b;
                        update(b, -a);
                    }
                    else
                    {
                        f >> x >> a >> b;
                        update(a, -x);
                        update(b, x);
                    }
        }
        for(j = 0; j <= n; j++)
            aib[j] = 0;
    }
    return 0;
}