Cod sursa(job #3133450)

Utilizator dariutTache Daria dariut Data 25 mai 2023 18:18:00
Problema Datorii Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.23 kb
#include <iostream>
#include <vector>
#include <fstream>

void calcDebt(int N, int M, std::vector<int>& debts, std::vector<std::vector<int>>& ops, std::ofstream& out) {
    for (const auto& operation : ops) {
        int opType = operation[0];

        if (opType == 0) {
            int day = operation[1];
            int amount = operation[2];
            debts[day] -= amount;
        }
        else if (opType == 1) {
            int startDay = operation[1];
            int endDay = operation[2];
            int totalDebt = 0;

            for (int day = startDay; day <= endDay; ++day) {
                totalDebt += debts[day];
            }

            out << totalDebt << std::endl;
        }
    }
}

int main() {
    std::ifstream in("datorii.in");
    std::ofstream out("datorii.out");

    int N, M;
    in >> N >> M;

    std::vector<int> debts(N + 1);
    for (int i = 1; i <= N; ++i) {
        in >> debts[i];
    }

    std::vector<std::vector<int>> ops(M, std::vector<int>(3));
    for (int i = 0; i < M; ++i) {
        int opType;
        in >> opType;

        ops[i][0] = opType;
        in >> ops[i][1] >> ops[i][2];
    }

    calcDebt(N, M, debts, ops, out);

    return 0;
}