Cod sursa(job #2623963)

Utilizator miramaria27Stroie Mira miramaria27 Data 4 iunie 2020 11:44:07
Problema Range minimum query Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.23 kb

// Sample code to perform I/O:

#include <iostream>
#include <fstream>
#include <algorithm>
#include <climits>
using namespace std;

void build(int a[], int tree[], int start, int stop, int node)
{
  if(start==stop)
  {
	  tree[node]=a[start];
  }
  else
  {
	  int m=(start+stop)/2;
	  build(a,tree,start,m,2*node);
	  build(a,tree,m+1,stop,2*node+1);
	  tree[node]=min(tree[2*node],tree[2*node+1]);

  }

}
int query(int tree[], int node, int start, int stop, int l, int r)
{
    if(stop<l || r<start)
	   return INT_MAX;
	if(l<=start && stop<=r)
	    return tree[node];

	int m=(start+stop)/2;
    int m1=query(tree,2*node,start,m,l,r);
    int m2=query(tree,2*node+1,m+1,stop,l,r);
	return min(m1,m2);

}
int main() {

    ifstream in("rmq.in");
    ofstream out("rmq.out");
	int n, nr, *a,*tree;
	a=new int[100003];
	tree=new int[200020];

	in>>n>>nr;
	for(int i=1;i<=n;i++)
	  in>>a[i];

	build(a,tree,1,n,1);

	int x, y;
	for(int i=1;i<=nr;i++)
	{
        in>>x>>y;
        out<<query(tree,1,1,n,x,y)<<"\n";


	}
	delete []a;
	delete []tree;
	return 0;

}

// Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail


// Write your code here