Cod sursa(job #2284034)

Utilizator cezar.plescaCezar Plesca cezar.plesca Data 16 noiembrie 2018 17:28:25
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.55 kb
#include<stdio.h>

#include<iostream>
#include<fstream>

using namespace std;

typedef struct node{
	int a, b;
	int maxim;
}node;

#define MAXN 100000

int M,N;
int V[MAXN];
node A[262141];

#define maxim(x,y) x>y?x:y

void buildtree(int root, int l,int r){
	if(l==r){
		A[root].a=l;
		A[root].b=l;
		A[root].maxim=V[l];
		return;
	}
	int mid=(l+r)/2;
	A[root].a=l;
	A[root].b=r;
	buildtree(2*root+1,l,mid);
	buildtree(2*root+2,mid+1,r);
	A[root].maxim=maxim(A[2*root+1].maxim,A[2*root+2].maxim);
}

int a,b;
int findmax(int root){
	if(a<=A[root].a && b>=A[root].b)
		return A[root].maxim;
	int mid=(A[root].a+A[root].b)/2;
	int max1=0,max2=0;
	if(a<=mid)
		max1=findmax(root*2+1);
	if(b>mid)
		max2=findmax(root*2+2);
	return maxim(max1,max2);
}

int index;
int value;

void replacenode(int root){
	int mid;
	if(A[root].a!=A[root].b){
		mid=(A[root].a+A[root].b)/2;
		if(index<=mid)
			replacenode(2*root+1);
		else
			replacenode(2*root+2);
		A[root].maxim=maxim(A[2*root+1].maxim,A[2*root+2].maxim);
		return;
	}
	A[root].maxim=value;
}

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

	scanf("%d %d", &N,&M);

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

	buildtree(0,0,N-1);

	int tip,x,y,max;
	for(int i=0;i<M;i++){
		scanf("%d %d %d",&tip,&x,&y);
		if(tip==0){
			a=x-1;
			b=y-1;
			max=findmax(0);
			printf("%d\n",max);
		}
		else{
			index=x-1;
			value=y;
			replacenode(0);
		}
	}
	
	return 0;
}