Cod sursa(job #303498)

Utilizator blasterzMircea Dima blasterz Data 9 aprilie 2009 21:31:08
Problema Arbori de intervale Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.6 kb
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cmath> // pt sqrt
#include <algorithm> // pt max
#define N 100001
#define dim 8192
#define bucket(i) ((i) / ns)

using namespace std;

char ax[dim];
int pz;

inline void cit(int &x)
{
    x=0;
    while(ax[pz] < '0' || ax[pz] > '9')
	if(++pz == dim) fread(ax,1,dim,stdin),pz=0;

    while(ax[pz] >= '0' && ax[pz] <= '9')
    {
	x=x*10+ax[pz]-'0';
	if(++pz == dim) fread(ax,1,dim,stdin),pz=0;
    }
}


int a[N], x[2048];
// x[i]= maximul pt bucata de sqrt n elemente
int n, m;
int ns; // numarul de bucati (sqrt(n))

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

    ns=(int) sqrt((double) n) +1;


    int i;
    for(i=1; i <= n; ++i) cit(a[i]);
    
    for(i=1; i <= n ; ++i)//setez valorile x[] 
	x[bucket(i)] = max(x[bucket(i)], a[i]);

    int type, p, q,mx;

    while(m--)
    {
	cit(type); cit(p); cit(q);

	if(type == 1) // update
	{
	    a[p]=q;
	    mx=0;
	    for(i=bucket(p)*ns; i < bucket(p)*ns +n; ++i)
	        mx=max(mx, a[i]);

	    x[bucket(p)]=mx;
	    

	}
	
	if(type == 0) // query
	{

	    mx=0;
	    if(bucket(p) == bucket(q)) //tratez cazul cand p si q sunt in aceasi bucata
	    {
		for(i=p; i <= q; ++i) 
		    mx=max(mx, a[i]);
	    }
	    else
	    {

	    for(i=bucket(p)+1; i < bucket(q); ++i)//iau maximele dintre bucati
		mx=max(mx, x[i]);

	    for(i=p; bucket(p) == bucket(i); ++i)//tratez capatul stang
		mx=max(mx, a[i]);

	    for(i=q; bucket(q) == bucket(i); --i)//tratez capatul drept
		mx=max(mx, a[i]);
//
	    }

	    printf("%d\n", mx);
	}

    }

    return 0;
}