Cod sursa(job #3167734)

Utilizator maryyMaria Ciutea maryy Data 11 noiembrie 2023 01:13:35
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.47 kb
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
ifstream in("arbint.in");
ofstream out("arbint.out");
const int bks=512, nmax=1e5+3;
int v[nmax], b[200], d[200];
const int len=512;
void UpdateBucket(int x)
{
    int first=x*len, last=first+len-1;
    b[x]=0;
    for(int i=first; i<=last; i++)
    {
        b[x]=max(b[x], v[i]);
    }
}
int query(int x, int y)
{
    int rez=0;
    int xbucket=x/len+1, ybucket=y/len-1;
    int firstpos=len*xbucket, lastpos=(ybucket+1)*len-1;
    if(ybucket-xbucket>=2)
    {
        for(int i=x; i<=firstpos; i++)
        {
            rez=max(rez, v[i]);
        }
        for(int i=xbucket; i<=ybucket; i++)
        {
            rez=max(rez, b[i]);
        }
        for(int i=lastpos; i<=y; i++)
        {
            rez=max(rez, v[i]);
        }
    }
    else
    {
        for(int i=x; i<=y; i++)
        {
            rez=max(rez, v[i]);
        }
    }
    return rez;
}
int main()
{
    int n, m;
    in>>n>>m;
    for(int i=0; i<n; i++)
    {
        in>>v[i];
    }
    for(int i=0; i<n; i++)
    {
        b[i/len]=max(b[i/len], v[i]);
    }
    //DisplayB();
    int op, x, y, maxi=0;
    for(int i=0; i<m; i++)
    {
        in>>op>>x>>y; x--;
        maxi=0;
        if(op==1)
        {
            v[x]=y;
            UpdateBucket(x/len);
        }
        else
        {
            y--;
            out<<query(x, y)<<'\n';
        }
    }
}