Cod sursa(job #2158601)

Utilizator mrhammerCiocan Cosmin mrhammer Data 10 martie 2018 14:20:53
Problema Arbori de intervale Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.4 kb
#include<iostream>
#include<fstream>
#include<vector>
#define MIN_VAL -100
using namespace std;
ifstream fin("arbint.in");
ofstream fout("arbint.out");
int n,m;
std::vector<int> arr;
std::vector<int> vec;
void construct_segment_tree(int x,int y,int pos)
{
    if(x == y) arr[pos] = vec[x];
    else
    {
        construct_segment_tree(x,(x+y)/2,2*pos+1);
        construct_segment_tree(((x+y)/2)+1,y,2*pos+2);
        arr[pos] = max(arr[2*pos+1],arr[2*pos+2]);
    }
}
int range_query(int x,int y,int pos,int x1,int y1)
{
    if(x >= x1 && y <= y1) return arr[pos];
    if(x > y1 || y < x1) return MIN_VAL;
    return max(range_query(x,(x+y)/2,2*pos+1,x1,y1),range_query(((x+y)/2)+1,y,2*pos+2,x1,y1));
}
int main()
{
    fin>>n>>m;
    arr.assign(2*n,0);
    int k1;
    for(int i=0;i<n;i++)
    {
        fin>>k1;
        vec.push_back(k1);
    }
    construct_segment_tree(0,vec.size()-1,0);
    bool reconstruct = false;
    for(int i=0;i<m;i++)
    {
        int k2,k3;
        fin>>k1>>k2>>k3;
        if(k1 == 0)
        {
            if(reconstruct == true)
            {
                construct_segment_tree(0,vec.size()-1,0);
                reconstruct = false;
            }
            fout<<range_query(0,vec.size()-1,0,k2-1,k3-1)<<"\n";
        }
        else if(k1 == 1)
        {
            vec[k2-1] = k3;
            reconstruct = true;
        }
    }
}