Cod sursa(job #2271949)

Utilizator vescaDamian-Teodor BELES vesca Data 29 octombrie 2018 15:26:50
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.71 kb
#include <iostream>
#include <vector>
#include <map>
#include <fstream>

#define LSB(x) (x & (-x))
#define cout fout
#define cin fin

using namespace std;

ifstream fin("datorii.in");
ofstream fout("datorii.out");

int N, M;
std::vector<int> days;

struct FenwickTree {
    std::vector<int> vec;
    int vecSize;

    FenwickTree(int n) {
        vec.resize(n, 0);
        vecSize = n;
    }

    int rsq(int x) {
        int sum = 0;
        for (; x; x -= LSB(x)) sum += vec[x];
        return sum;
    }

    int rsq(int a, int b) {
        return rsq(b) - (a == 1 ? 0 : rsq(a - 1));
    }

    void adjust(int k, int v) {
        for (; k < vecSize; k += LSB(k)) vec[k] += v;
    }
};

enum OperationType { ADJUST, SUM };

struct Operation {
    OperationType type;
    std::pair<int, int> aux;
};
std::vector<Operation> operations;

void Read() {
    cin >> N >> M;

    days.resize(N);

    for (auto& it : days) {
        cin >> it;
    }

    operations.resize(M);
    bool oper;
    for (int i = 0; i < M; ++i) {
        cin >> oper;
        if (oper == true) operations[i].type = OperationType::SUM;
        else operations[i].type = OperationType::ADJUST;
        cin >> operations[i].aux.first >> operations[i].aux.second;
    }
}

void Solve() {
    FenwickTree ft(N + 1);

    for (int i = 0; i < N; ++i) {
        ft.adjust(i + 1, days[i]);
    }

    for (int i = 0; i < M; ++i) {
        if (operations[i].type == OperationType::SUM) cout << ft.rsq(operations[i].aux.first, operations[i].aux.second) << "\n";
        else ft.adjust(operations[i].aux.first, -operations[i].aux.second);
    }
}

int main()
{
    Read();
    Solve();
    return 0;
}