Cod sursa(job #729237)

Utilizator geniucosOncescu Costin geniucos Data 29 martie 2012 13:29:14
Problema Range minimum query Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include<cstdio>
using namespace std;
int xi,m,n,i,a,b,t[400002];
int min(int i,int j)
{
	if(i>j) return j;
	return i;
}
void update(int nod,int st,int dr,int poz,int val)
{
	if(st==dr)
	{
		t[nod]=val;
		return ;
	}
	int mij=(st+dr)/2;
	if(poz<=mij)
		update(nod*2,st,mij,poz,val);
	else
		update(nod*2+1,mij+1,dr,poz,val);
	t[nod]=min(t[nod*2],t[nod*2+1]);
}
int q(int nod,int st,int dr,int x,int y)
{
	if(x<=st&&dr<=y) return t[nod];
	int mij=(st+dr)/2;
	int sol=100002;
	if(x<=mij)
		sol=q(nod*2,st,mij,x,y);
	if(mij<y)
		sol=min(sol,q(nod*2+1,mij+1,dr,x,y));
	return sol;
}
int main()
{
freopen("rmq.in","r",stdin);
freopen("rmq.out","w",stdout);
scanf("%d",&n);
scanf("%d",&m);
for(i=1;i<=4*n;i++)
	t[i]=10002;
for(i=1;i<=n;i++)
{
	scanf("%d",&xi);
	update(1,1,n,i,xi);
}
for(i=1;i<=m;i++)
{
	scanf("%d",&a);
	scanf("%d",&b);
	printf("%d\n",q(1,1,n,a,b));
}
return 0;
}