Cod sursa(job #2348488)

Utilizator alexge50alexX AleX alexge50 Data 19 februarie 2019 19:16:36
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.14 kb
/*
 * Software sellers want to divide the users and conquer them, making each user agree not to share with others.
 *      Richard M. Stallman
 */

#include <fstream>
#include <iostream>

const int MAX_N = 100000;
const int MAX_LOG_N = 17;

struct {
    int data[MAX_N + 1];
    int n;
}aib;

int computeSum(int i)
{
    int sum = 0;

    while(i != 0)
    {
        sum += aib.data[i];
        i -= i & (-i);
    }

    return sum;
}

void update(int i, int v)
{
    while(i <= aib.n)
    {
        aib.data[i] += v;
        i += i & (-i);
    }
}

int main()
{
    std::ifstream fin("datorii.in");
    std::ofstream fout("datorii.out");
    int n, k;

    fin >> n >> k;
    aib.n = n;
    for(int i = 1; i <= n; i++)
    {
        int x;
        fin >> x;

        update(i, x);
    }

    void (*branches[2])(std::ofstream&, int, int) = {
            [](std::ofstream&, int a, int b) { update(a, -b); },
            [](std::ofstream& fout, int a, int b) { fout << computeSum(b) - (a == 1 ? 0 : computeSum(a - 1)) << '\n'; },
    };

    for(; k > 0; k--)
    {
        int x, a, b;
        fin >> x;
        fin >> a >> b;

        branches[x](fout, a, b);
    }
}