Pagini recente » Cod sursa (job #2634560) | Cod sursa (job #1118655) | Cod sursa (job #2402654) | Cod sursa (job #219930) | Cod sursa (job #2749692)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("rmq.in");
ofstream fout("rmq.out");
#define NMAX 400005
#define MAXNUM 100001
int n, segTree[NMAX], input[MAXNUM];
void construct(int low, int high, int pos)
{
if(low == high)
{
segTree[pos] = input[low];
return;
}
int mid = (low+high)>>1;
construct(low, mid, (1<<pos) +1);
construct(mid+1, high, (1<<pos) +2);
segTree[pos] = min(segTree[(1<<pos)+1], segTree[(1<<pos)+2]);
}
int RMQ(int qlow, int qhigh, int low, int high, int pos)
{
if(qlow <= low && qhigh>= high) //intervalul cerut include intervalul actual
return segTree[pos];
if(qlow > high || qhigh < low) //intervalele nu se suprapun
return MAXNUM;
int mid = (low +high)>>1;
return min(RMQ(qlow, qhigh, low, mid, (1<<pos)+1),
RMQ(qlow, qhigh, mid+1, high, (1<<pos)+2));
}
int main()
{
int m, a, b;
fin >> n >> m;
for(int i = 1;i <= n; ++i)
fin >> input[i];
construct(1,n,1);
for(int i = 0 ; i < m; ++i)
{
fin >> a >> b;
fout << RMQ(a, b, 1, n, 1)<<"\n";
}
return 0;
}