Cod sursa(job #1935957)

Utilizator dr55Dan Rusu dr55 Data 22 martie 2017 19:17:53
Problema Arbori de intervale Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.33 kb
#include <fstream>

const int kNMAX = 100010;

int n; // number of elements in the vector
int m; // number of operations applied to the vector
int A[kNMAX]; // the vector

int query(int left, int right)
{
    int result = -1;
    for ( left += n, right += n; left < right; left /= 2, right /= 2)
    {
        if (left % 2)
        {
            result = std::max(result, A[left++]);
        }
        if (right % 2)
        {
            result = std::max(result, A[--right]);
        }
    }
    return result;
}

void update(int position, int value)
{
    A[position += n]  = value;
    for (position /= 2; position; position /= 2)
    {
        A[position] = std::max(A[2*position], A[2*position + 1]);
    }
}

int main()
{
    std::ifstream fin("arbint.in");
    std::ofstream fout("arbint.out");
    int i, x, y;
    int operationCode;
    fin >> n >> m;
    for ( i = n+1; i <= 2*n; i++ )
    {
        fin >> A[i];
    }
    for ( i = n; i > 0; i--)
    {
        A[i] = std::max(A[2*i], A[2*i+1]);
    }

    for (i = 1; i <= m; i++)
    {
        fin >> operationCode >> x >> y;
        if (operationCode == 0)
        {
            fout << query(x, y+1) << '\n';
        }
        else
        {
            update(x, y);
        }
    }
    fin.close();
    fout.close();
    return 0;
}