Cod sursa(job #3133449)

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

using namespace std;

void calcDebt(int N, int M, vector<int>& debts, vector<vector<int>>& ops, ofstream& out) {
    vector<int> results;

    for (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];
            }

            results.push_back(totalDebt);
        }
    }

    for (int result : results) {
        out << result << endl;
    }
}

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

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

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

    vector<vector<int>> ops(M, 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);

    in.close();
    out.close();

    return 0;
}