Cod sursa(job #869520)

Utilizator TeOOOVoina Teodora TeOOO Data 1 februarie 2013 19:01:39
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.66 kb
#include <fstream>
#include <algorithm>
using namespace std;

//Definitii
#define leftSon 2*node
#define rightSon 2*node+1


//Constante
const int sz=(int)4e5+1;

//Functii
void update(int node, int left, int right);
void query(int node, int left, int right);

//Variabile
int num, operations;
int position, value;
int leftLimit, rightLimit;
int tree[sz];

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

    in >> num >> operations;

    for(position=1; position <= num; ++position)
    {
        in >> value;
        update(1, 1, num);
    }
    while(operations--)
    {
        int type;
        in >> type;
        if(type)
        {
            in >> position >> value;
            update(1, 1, num);
        }
        else
        {
            in >> leftLimit >> rightLimit;
            value = 0;
            query(1, 1, num);
            out << value << '\n';
        }
    }

    in.close();
    out.close();
    return 0;
}

void update(int node, int left, int right)
{
    if( left == right)
    {
        tree[node] = value;
        return;
    }
    int mid = (left + right) / 2;

    if(position <= mid)
        update(leftSon, left, mid);
    else
        update(rightSon, mid+1, right);

    tree[node] = max(tree[leftSon], tree[rightSon]);
}

void query(int node, int left, int right)
{
    if(leftLimit <= left && right <= rightLimit)
    {
        value = max(value, tree[node]);
        return;
    }

    int mid = (left + right) / 2;

    if(leftLimit <= mid)
        query(leftSon, left, mid);
    if(mid < rightLimit)
        query(rightSon, mid+1, right);
}