Pagini recente » Cod sursa (job #12998) | Cod sursa (job #402304) | Cod sursa (job #935442) | Cod sursa (job #1440295) | Cod sursa (job #1834104)
#include <stdio.h>
#include <vector>
using namespace std;
#define NMAX 100002
#define INF 0x3f3f3f3f
long int A[NMAX];
long int M[NMAX*3];
long int n,m;
void initialize(int node, int b, int e)
{
if (b == e)
M[node] = b;
else
{
//compute the values in the left and right subtrees
initialize(2 * node, b, (b + e) / 2);
initialize(2 * node + 1, (b + e) / 2 + 1, e);
//search for the minimum value in the first and
//second half of the interval
if (A[M[2 * node]] <= A[M[2 * node + 1]])
M[node] = M[2 * node];
else
M[node] = M[2 * node + 1];
}
}
int query(int node, int b, int e, int i, int j)
{
int p1, p2;
//if the current interval doesn't intersect
//the query interval return -1
if (i > e || j < b)
return -1;
//if the current interval is included in
//the query interval return M[node]
if (b >= i && e <= j)
return M[node];
//compute the minimum position in the
//left and right part of the interval
p1 = query(2 * node, b, (b + e) / 2, i, j);
p2 = query(2 * node + 1, (b + e) / 2 + 1, e, i, j);
//return the position where the overall
//minimum is
if (p1 == -1)
return M[node] = p2;
if (p2 == -1)
return M[node] = p1;
if (A[p1] <= A[p2])
return M[node] = p1;
return M[node] = p2;
}
int main()
{
freopen("rmq.in","r",stdin);
freopen("rmq.out","w",stdout);
long int i,x,y,k;
scanf("%ld %ld",&n,&m);
for (i=0;i<n;i++)
{
scanf("%ld",&x);
A[i]=x;
}
for (i=1;i<=m;i++)
{
scanf("%ld %ld",&x,&y);
mx=INF;
mx=A[query(1,0,N-1,x-1,y-1)];
printf("%ld\n",mx);
}
return 0;
}