Pagini recente » Cod sursa (job #2954221) | Cod sursa (job #371600) | Cod sursa (job #2353190) | Cod sursa (job #2560336) | Cod sursa (job #1789471)
#include <iostream>
#include<fstream>
#define inf 100000000000
using namespace std;
ifstream f("rmq.in");
ofstream g("rmq.out");
int v[100001],n,m,i,x,y,tree[400004];
void constructTree(int low,int high,int poz)
{
if(low==high)
{
tree[poz]=v[low];
return;
}
int mid=(low+high)/2;
constructTree(low,mid,2*poz);
constructTree(mid+1,high,2*poz+1);
tree[poz]=min(tree[2*poz],tree[2*poz+1]);
}
int RMQ(int x,int y,int low,int high,int poz)
{
if(x<=low && y>=high)
return tree[poz];
if(x>high || y<low)
return inf;
int mid=(low+high)/2;
return min(RMQ(x,y,low,mid,2*poz),RMQ(x,y,mid+1,high,2*poz+1));
}
int main()
{
f>>n>>m;
for(i=1; i<=n; i++)
f>>v[i];
constructTree(1,n,1);
for(i=1; i<=m; i++)
{
f>>x>>y;
g<<RMQ(x,y,1,n,1)<<'\n' ;
}
}