Cod sursa(job #2329901)

Utilizator rexlcdTenea Mihai rexlcd Data 27 ianuarie 2019 17:35:25
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <iostream>
#include <fstream>

using namespace std;

int v[15002];
int n, m;

inline void update1(int pos, int x)
{
    for(; pos <= n; pos += (pos & -pos))
        v[pos] += x;
}

inline void update2(int pos, int x)
{
    for(; pos <= n; pos += (pos & -pos))
        v[pos] -= x;
}

inline int query(int pos)
{
    int s = 0;
    for(; pos >= 1; pos -= (pos & -pos))
        s += v[pos];
    return s;
}

int main()
{
    ifstream f("datorii.in");
    ofstream g("datorii.out");
    f >> n >> m;
    for(int i = 1, x; i <= n; i++)
    {
        f >> x;
        update1(i, x);
    }

    for(int c, a, b; m; m--)
    {
        f >> c >> a >> b;
        if(c)
            g << query(b) - query(a - 1) << '\n';
        else
            update2(a, b);
    }

    f.close();
    g.close();
    return 0;
}