Cod sursa(job #1838073)

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


using namespace std;
int A[100010];
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 maxim = A[st];
    while((st % lung_bloc)!=0 && (st <= dr)){
        if(A[st] > maxim)
            maxim = A[st];
        st++;
    }
    while(((dr+1)%lung_bloc!=0) && (dr > st)){
        if(A[dr] > maxim)
            maxim = A[dr];
        dr--;
    }

    int st_bloc = st/lung_bloc;
    int dr_bloc = dr/lung_bloc;


    if((st % lung_bloc==0) && (dr - st+1) >= 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;

        int bloc = pos/lung_bloc;

        if(bloc < nr_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;
        }
}

void afis(){

    for(int i=0; i<N; i++)
        cout << A[i] << " ";

}



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

    cin >> N >> M;
    for(int i=0; i<N; i++)
        cin >> A[i];

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

    calcMax();

    int op, a, b;

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




    return 0;
}