Cod sursa(job #716187)

Utilizator algotrollNume Fals algotroll Data 18 martie 2012 13:56:48
Problema Arbori de intervale Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.73 kb
#include<cstdio>
#include<cmath>
#define _NMAX 100010
#define _TMAX 500
int vA[_NMAX];
int sA;

int table[_TMAX];
int _sBlock;

void add_table(int);
void reev_table(int);
int query_max(int,int);
int main()
{
    freopen("arbint.in", "r", stdin);
    freopen("arbint.out", "w", stdout);
    int nOperatii;
    scanf("%d %d", &sA, &nOperatii);
    _sBlock=sqrt(sA);
    for (int i=0;i<sA;i++)
    {
        scanf("%d", &vA[i]);
        add_table(i);
    }
    for (int i=1;i<=nOperatii;i++)
    {
        int assign;//bool
        scanf("%d", &assign);
        if (assign)
        {
            int poz, val;
            scanf("%d %d", &poz, &val); poz--;
            vA[poz]=val;
            reev_table(poz);
        }
        else
        {
            int a, b;
            scanf("%d %d", &a, &b); a--; b--;
            printf("%d\n", query_max(a,b));
        }
    }
    return 0;
}

void add_table(int Apoz)
{
    int Tpoz=Apoz/_sBlock;
    if (vA[Apoz]>table[Tpoz])
        table[Tpoz]=vA[Apoz];
}

void reev_table(int Apoz)
{
    int Tpoz=Apoz/_sBlock;
    int max=0;
    for (int i=Tpoz*_sBlock;i<(Tpoz+1)*_sBlock;i++)
        if (vA[i]>max) max=vA[i];
    table[Tpoz]=max;
}

int query_max(int Apoz1, int Apoz2)
{
    int max=0;
    if (Apoz1-Apoz2<=2*_sBlock)
    {
        for (int i=Apoz1;i<=Apoz2;i++)
            if (vA[i]>max) max=vA[i];
        return max;
    }//else
    int Tpoz1=Apoz1/_sBlock +1;
    int Tpoz2=Apoz2/_sBlock;
    for (int i=Apoz1;i<Tpoz1*_sBlock;i++)
        if (vA[i]>max) max=vA[i];
    for (int i=Tpoz1;i<Tpoz2;i++)
        if (table[i]>max) max=table[i];
    for (int i=Tpoz2*_sBlock;i<=Apoz2;i++)
        if (vA[i]>max) max=vA[i];
    return max;
}