Cod sursa(job #1838061)

Utilizator mdiannnaMarusic Diana mdiannna Data 30 decembrie 2016 22:28:58
Problema Arbori de intervale Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.78 kb
#include <iostream>
#include <cmath>
#include <climits>


using namespace std;
int A[100005];
int B[100005];
int N, M;

 int lung_bloc, nr_bloc;

void calcMax(){
    int j = 0;
    int maxim = 0;

    for(int i=0; i<nr_bloc; i++){
        maxim = A[j];
        for(int k=0; k<lung_bloc; k++){
            if(A[j] > maxim)
                maxim = A[j];
            j++;
        }

        B[i] = maxim;
    }


}


int query(int st, int dr){
    int st_bloc = st/lung_bloc;
    int dr_bloc = dr/lung_bloc;

    int maxim = A[st];
    while((st % lung_bloc)!=0 && (st <= dr)){
        if(A[st] > maxim)
            maxim = A[st];
        st++;
    }
    while((dr%lung_bloc!=0) && (dr > st)){
        if(A[dr] > maxim)
            maxim = A[dr];
        dr--;
    }

    if((dr - st) >= lung_bloc )
        for(int i=st_bloc; i<=dr_bloc; i++){
            if(B[i] > maxim)
                maxim = B[i];
        }
    return maxim;
}


void update_bloc(int pos, int val){
        A[pos] = val;
        int maxim = 0;

         if(pos <= nr_bloc*lung_bloc){

            int bloc = pos/lung_bloc;
            int j = bloc * lung_bloc;

            maxim = A[j];
            for(int k=0; k<lung_bloc; k++){
                if(A[j] > maxim)
                    maxim = A[j];
                j++;
            }
            B[bloc] = maxim;
        }

}


int main()
{
    freopen("arbint.in","r", stdin);
    freopen("arbint.out","w", stdout);

    scanf("%d %d", &N, &M);
    for(int i=0; i<N; i++)
        scanf("%d", &A[i]);

    lung_bloc = sqrt(N);
    nr_bloc = N/lung_bloc;

    calcMax();

    int op, a, b;

    for(int i=0; i<M; i++){
        scanf("%d %d %d", &op, &a, &b);
        if(op == 0)
            printf("%d\n", query(a-1, b-1));
        else
            update_bloc(a-1, b);
       }




    return 0;
}