Cod sursa(job #3233241)

Utilizator MirceaDonciuLicentaLicenta Mircea Donciu MirceaDonciuLicenta Data 2 iunie 2024 20:38:19
Problema Arbori indexati binar Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.95 kb
#include <iostream>
#include <vector>
#include <fstream>
#include <climits>
#include <algorithm>

using namespace std;

class FenwickTree {
    vector<int> BIT;
public:
    FenwickTree(int n) : BIT(n + 1, 0) {}

    void update(int index, int val) {
        while (index < BIT.size()) {
            BIT[index] += val;
            index += index & -index;
        }
    }

    int query(int index) {
        int sum = 0;
        while (index > 0) {
            sum += BIT[index];
            index -= index & -index;
        }
        return sum;
    }

    int queryRange(int l, int r) {
        return query(r) - query(l - 1);
    }
};

class SegmentTree {
    vector<int> st;
    int n;
public:
    SegmentTree(int size) : n(size) {
        st.resize(4 * n, 0);
    }

    void update(int index, int start, int end, int pos, int val) {
        if (start == end) {
            st[index] = val;
        } else {
            int mid = (start + end) / 2;
            if (pos <= mid) {
                update(2 * index + 1, start, mid, pos, val);
            } else {
                update(2 * index + 2, mid + 1, end, pos, val);
            }
            st[index] = max(st[2 * index + 1], st[2 * index + 2]);
        }
    }

    int rangeMax(int index, int start, int end, int l, int r) {
        if (r < start || l > end) return INT_MIN;
        if (l <= start && r >= end) return st[index];
        int mid = (start + end) / 2;
        return max(rangeMax(2 * index + 1, start, mid, l, r), rangeMax(2 * index + 2, mid + 1, end, l, r));
    }

    int rangeMax(int l, int r) {
        return rangeMax(0, 0, n - 1, l, r);
    }
};

int main() {
    ifstream infile("aib.in");
    ofstream outfile("aib.out");

    int N, M;
    infile >> N >> M;

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

    FenwickTree fenwickTree(N);
    SegmentTree segmentTree(N);

    for (int i = 1; i <= N; ++i) {
        fenwickTree.update(i, A[i]);
        segmentTree.update(0, 0, N - 1, i - 1, A[i]);
    }

    for (int i = 0; i < M; ++i) {
        int type;
        infile >> type;

        if (type == 0) {
            int a, b;
            infile >> a >> b;
            fenwickTree.update(a, b);
            segmentTree.update(0, 0, N - 1, a - 1, A[a] + b);
            A[a] += b;
        } else if (type == 1) {
            int a, b;
            infile >> a >> b;
            outfile << fenwickTree.queryRange(a, b) << "\n";
        } else if (type == 2) {
            int k;
            infile >> k;
            int currentSum = 0, pos = -1;
            for (int j = 1; j <= N; ++j) {
                currentSum += A[j];
                if (currentSum >= k) {
                    pos = j;
                    break;
                }
            }
            outfile << pos << "\n";
        }
    }

    infile.close();
    outfile.close();

    return 0;
}