Cod sursa(job #2669186)

Utilizator MohneaGosuMihnea Gusu MohneaGosu Data 6 noiembrie 2020 13:16:24
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.28 kb
#include <fstream>

using namespace std;
ifstream Gigi ("arbint.in");
ofstream Marcel ("arbint.out");
int a[262145];
int n,n1,x,y;
int v[100001];
int binary(int x)
{
    x--;
    x |= x >> 1;
    x |= x >> 2;
    x |= x >> 4;
    x |= x >> 8;
    x |= x >> 16;
    x++;
    return x;
}
void build()
{
    int i;
    for (i=0;i<n;i++)
    {
        a[n1+i]=v[i+1];
    }
    for (i=n1-1;i;i--){
        a[i]=max(a[i*2],a[i*2+1]);
    }
}
void update (int poz,int val)
{
    a[n1+poz-1]=val;
    int i=n1+poz-1;
    i/=2;
    while(i){
        a[i]=max(a[i*2],a[i*2+1]);
        i/=2;
    }
}
int maxim=-1;
void care(int stq,int drq,int nod,int st,int dr)
{
    if (st>=stq && dr<=drq) maxim=max(maxim,a[nod]);
    else{
        if (stq<=(st+dr)/2) care(stq,drq,nod*2,st,(st+dr)/2);
        if (drq>(st+dr)/2) care(stq,drq,nod*2+1,(dr+st)/2+1,dr);
    }
}
int main()
{
    int m,i;
    bool t;
    Gigi>>n>>m;
    n1=binary(n);
    for (i=1;i<=n;i++){
        Gigi>>v[i];
    }
    build();
    for (;m;m--){
        Gigi>>t;
        if (t){
            Gigi>>x>>y;
            update(x,y);
        }
        else{
            Gigi>>x>>y;
            maxim=0;care(x,y,1,1,n1);
            Marcel<<maxim<<"\n";
        }
    }
    return 0;
}