Cod sursa(job #1197408)

Utilizator ariel_roAriel Chelsau ariel_ro Data 11 iunie 2014 21:15:28
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.1 kb
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h> 

#define NX 100005
#define NXARB 262144

int t[NXARB], A, B;

inline int MaximTwoElems(int a, int b) {
       if (a > b) 
           return a;
       return b;
}

int createArbInt(int nod, int st, int dr, int val)
{
	if (st == dr)
	{
		t[nod] = val;
		// printf("nod: %d\n ", nod);
		return t[nod];
	}
	else 
	{
		int mij = (st + dr) >> 1;
		int val1 = t[nod << 1], val2 = t[(nod << 1) + 1];
		if (A <= mij)
		{
			val1 = createArbInt(nod << 1, st, mij, val);
		} 
		else 
		{
			val2 = createArbInt((nod << 1) + 1, mij + 1, dr, val);
		}
		t[nod] = MaximTwoElems(val1, val2);
		// printf("nod: %d\n ", nod);
		return t[nod];
	}
}

int query(int nod, int st, int dr)
{
	if (A <= st && dr <= B)
	{
		//printf("nod: %d\n ", nod);
		return t[nod];
	}
	else 
	{
		int mij = (st + dr) >> 1;
		int val1 = 0, val2 = 0;
		if (A <= mij)
		{
			val1 = query(nod << 1, st, mij);
		}
		
		//printf("nod: %d\n ", nod);
		if (B > mij)
		{
			val2 = query((nod << 1) + 1, mij + 1, dr);
		}

		//printf("nod: %d\n ", nod);
		return MaximTwoElems(val1, val2);
	}
}

int main()
{
    int N, M, op, an, bn, val;
    freopen("arbint.in", "r", stdin);
    freopen("arbint.out", "w", stdout);
 
	scanf("%d %d", &N, &M);
  
	//clock_t t = clock();

	for (int i = 1; i <= N; i++)
	{
		scanf("%d", &val);
		A = i; B = i;
		// printf("insert %d\n", val);
		createArbInt(1, 1, N, val);
	}

	//t = clock() - t;
    //printf ("It took %d clicks (%f seconds).\n",t,((float)t)/CLOCKS_PER_SEC);

	//t = clock();
    for (int i = 1; i <= M; i++)
    {
        scanf("%d %d %d\n", &op, &an, &bn);
  
        switch (op)
        {
        case 0:
			A = an;
			B = bn;
			//printf("query int: %d, %d\n ", an, bn);
            printf("%d\n", query(1, 1, N));
            break;
        case 1:
			A = an;
			B = an;
			createArbInt(1, 1, N, bn);
             
            break;
        }
    }
	//t = clock() - t;
    //printf ("It took %d clicks (%f seconds).\n",t,((float)t)/CLOCKS_PER_SEC);

}