Cod sursa(job #2378332)

Utilizator dan.ghitaDan Ghita dan.ghita Data 12 martie 2019 05:21:34
Problema Arbori indexati binar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.05 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("aib.in");
ofstream g("aib.out");

vector<int> aint;

int update(int index, int nodeLeft, int nodeRight, int updateIndex, int val)
{
    if (updateIndex < nodeLeft || nodeRight < updateIndex)
        return aint[index];

    if (nodeLeft == nodeRight)
    {
        aint[index] += val;
        return aint[index];
    }

    int mid = nodeLeft + (nodeRight - nodeLeft) / 2;
    aint[index] = update(index << 1, nodeLeft, mid, updateIndex, val) + 
        update((index << 1) + 1, mid + 1, nodeRight, updateIndex, val);

    return aint[index];
}

int query(int index, int nodeLeft, int nodeRight, int queryLeft, int queryRight)
{
    if (queryLeft <= nodeLeft && nodeRight <= queryRight)
        return aint[index];

    if (queryRight < nodeLeft || nodeRight < queryLeft)
        return 0;

    int mid = nodeLeft + (nodeRight - nodeLeft) / 2;
    return query(index << 1, nodeLeft, mid, queryLeft, queryRight) +
        query((index << 1) + 1, mid + 1, nodeRight, queryLeft, queryRight);
}

int findKSum(int index, int nodeLeft, int nodeRight, int S)
{
    // current node has exact sum
    if (S == aint[index])
        return nodeRight;

    // leaf & sum != node value
    if (nodeLeft == nodeRight)
        return -1;

    int mid = nodeLeft + (nodeRight - nodeLeft) / 2;
    if (S <= aint[index << 1])
        return findKSum(index << 1, nodeLeft, mid, S);
    else
        return findKSum((index << 1) + 1, mid + 1, nodeRight, S - aint[index << 1]);
}

int main()
{
    int n, m, x, t, a, b;

    f >> n >> m;

    aint.resize(4 * n + 5, 0);

    for (int i = 1; i <= n; ++i)
        f >> x,
        update(1, 1, n, i, x);

    while (m--)
    {
        f >> t;
        if (t == 0)
            f >> a >> b,
            update(1, 1, n, a, b);
        else if (t == 1)
            f >> a >> b,
            g << query(1, 1, n, a, b) << '\n';
        else
            f >> a,
            g << findKSum(1, 1, n, a) << '\n';
    }

    return 0;
}