Cod sursa(job #3134287)

Utilizator alexandramocanu181Mocanu Alexandra alexandramocanu181 Data 28 mai 2023 20:44:59
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

    int n, m;
    fin >> n >> m;

    vector<long long> aib(n + 1, 0);

    for (int i = 1; i <= n; i++) {
        int x;
        fin >> x;
        for (int j = i; j <= n; j += (j & -j)) {
            aib[j] += x;
        }
    }

    while (m--) {
        int t;
        fin >> t;

        if (t == 0) {
            int x, y;
            fin >> x >> y;
            for (int i = x; i <= n; i += (i & -i)) {
                aib[i] -= y;
            }
        } else {
            int st, dr;
            fin >> st >> dr;
            long long result = 0;

            for (int i = dr; i > 0; i -= (i & -i)) {
                result += aib[i];
            }

            for (int i = st - 1; i > 0; i -= (i & -i)) {
                result -= aib[i];
            }

            fout << result << '\n';
        }
    }

    fin.close();
    fout.close();

    return 0;
}