Cod sursa(job #2492851)

Utilizator SochuDarabaneanu Liviu Eugen Sochu Data 15 noiembrie 2019 13:39:50
Problema Arbori de intervale Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.46 kb
#include <bits/stdc++.h>
#define INTERVMAX 100005

using namespace std;

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

bool operation;
int n , A , B , x , m;
int a[2 * INTERVMAX];

void Update(int nod , int st , int dr , int pos , int val)
{
    if(st == dr)
    {
        a[nod] = val;
        return;
    }

    int mij = (dr - st) / 2 + st;

    if(pos <= mij)
        Update(2 * nod , st , mij , pos , val); /// 2 * nod = left son
    else Update(2 * nod + 1 , mij + 1 , dr , pos , val); /// 2 * nod + 1 = right son

    a[nod] = max(a[nod * 2] , a[nod * 2 + 1]);
}

void Query(int nod , int st , int dr , int &maxi , int start , int finish)
{
    if(start <= st && dr <= finish)
    {
        maxi = max(a[nod] , maxi);
        return;
    }

    int mij = (dr - st) / 2 + st;

    if(mij < finish)
        Query(2 * nod + 1 , mij + 1 , dr , maxi , start , finish);

    if(start <= mij)
        Query(2 * nod , st , mij , maxi , start , finish);
}

int main()
{
    int i , ans = 0;

    f >> n >> m;

    for(i = 1 ; i <= n ; i++)
    {
        f >> x;
        Update(1 , 1 , n , i , x);
    }



    for(i = 1 ; i <= m ; i++)
    {
        f >> operation >> A >> B;

        if(operation == 0)
        {
            ans = 0;
            Query(1 , 1 , n , ans , A , B);
            g << ans << '\n';
        }
        else if(operation == 1)
            Update(1 , 1 , n , A , B);
    }

    return 0;
}