Cod sursa(job #2615073)

Utilizator andreiomd1Onut Andrei andreiomd1 Data 13 mai 2020 17:03:45
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.41 kb
#include <fstream>

using namespace std;

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

const int NMAX = 15e3 + 5;

int N, Q, A[NMAX];

class FenwickTree
{
    static const int NMAX = 15e3 + 5;
    int A[NMAX];

private:
    static inline int UB (int X)
    {
        return (X & (-X));
    }

    inline int F (int pos)
    {
        int r = 0;

        for(int i = pos; i >= 1; i -= UB(i))
            r += A[i];

        return r;
    }

public:
    inline void Update (int pos, int val)
    {
        for(int i = pos; i <= N; i += UB(i))
            A[i] += val;

        return;
    }

    inline int Query (int Left, int Right)
    {
        if(Left > Right)
            return 0;

        return F(Right) - F(Left - 1);
    }
} AIB;

static inline void Read ()
{
    f.tie(nullptr);

    f >> N >> Q;

    for(int i = 1; i <= N; ++i)
        f >> A[i], AIB.Update(i, A[i]);

    return;
}

static inline void TestCase ()
{
    int Type = 0;
    f >> Type;

    if(Type == 0)
    {
        int T = 0, V = 0;
        f >> T >> V;

        AIB.Update(T, -V);

        return;
    }

    int Left = 0, Right = 0;
    f >> Left >> Right;

    g << AIB.Query(Left, Right) << '\n';

    return;
}

static inline void Solve ()
{
    while(Q--)
        TestCase();

    return;
}

int main()
{
    Read();

    Solve();

    return 0;
}