Cod sursa(job #2231648)

Utilizator lorena1999Marginean Lorena lorena1999 Data 15 august 2018 14:17:28
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.74 kb
#include <bits/stdc++.h>
#define MAX 400000
#define oo -10000000000

using namespace std;

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

int n, k, v[MAX], heap[MAX], posInTree[MAX];

void buildHeap(int st, int dr, int idx)
{
    if(st==dr)
    {
        posInTree[st] = idx;
        heap[idx]=v[st];
        return;
    }
    int mij=(st+dr)/2;
    int twice = (idx<<1);
    buildHeap(st, mij, twice);
    buildHeap(mij+1, dr, twice+1);
    heap[idx] = max(heap[twice], heap[twice+1]);
}

int getMax(int x, int y, int st, int dr, int idx)
{
    if(st<=dr)
    {
        int mij=(st+dr)/2;
        int twice=(idx<<1);
        if(x<=st && y>=dr)
            return heap[idx];
        else if(y<st || x>dr)
            return oo;
        else return max(getMax(x, y, st, mij, twice), getMax(x, y, mij+1, dr, twice+1));
    }

}

void updateTree(int pos, int val)
{
    int idx = posInTree[pos];
    heap[idx] = val;
    while(idx>1)
    {
        int newIdx = (idx>>1);
        if(idx&1)
            heap[newIdx] = max(heap[idx], heap[idx-1]);
        else heap[newIdx] = max(heap[idx], heap[idx+1]);
        idx=newIdx;
    }
}

int main()
{
    f>>n>>k;
    for(int i=1; i<=n; i++)
        f>>v[i];
    buildHeap(1, n, 1);
    /*for(int i=1; i<=30; i++)
        cout<<heap[i]<<" ";
    cout<<'\n';*/
    /*for(int i=1; i<=n; i++)
        cout<<posInTree[i]<<" ";
    cout<<'\n';*/
    for(int i=1; i<=k; i++)
    {
        int op, x, y;
        f>>op>>x>>y;
        if(op==0)
            g<<getMax(x, y, 1, n, 1)<<'\n';
        else
        {
            updateTree(x, y);
            /*for(int i=1; i<=30; i++)
                cout<<heap[i]<<" ";
            cout<<'\n';*/
        }
    }
}