Cod sursa(job #3133451)

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

void calcDebt(int N, int M, std::vector<int>& debts, std::vector<std::pair<int, std::pair<int, int>>>& ops, std::ofstream& out) {
    std::vector<long long> cumulativeSum(N + 1);
    for (int i = 1; i <= N; ++i) {
        cumulativeSum[i] = cumulativeSum[i - 1] + debts[i];
    }

    for (const auto& operation : ops) {
        int opType = operation.first;
        int startDay = operation.second.first;
        int endDay = operation.second.second;

        if (opType == 0) {
            int day = startDay;
            int amount = endDay;
            debts[day] -= amount;

            for (int i = day; i <= N; ++i) {
                cumulativeSum[i] -= amount;
            }
        }
        else if (opType == 1) {
            long long totalDebt = cumulativeSum[endDay] - cumulativeSum[startDay - 1];
            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::pair<int, std::pair<int, int>>> ops(M);
    for (int i = 0; i < M; ++i) {
        int opType;
        in >> opType;

        int arg1, arg2;
        in >> arg1 >> arg2;

        ops[i] = std::make_pair(opType, std::make_pair(arg1, arg2));
    }

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

    return 0;
}