Pagini recente » Cod sursa (job #2728704) | Cod sursa (job #2854587) | Cod sursa (job #3196306) | Cod sursa (job #957491) | Cod sursa (job #2481419)
#include <bits/stdc++.h>
#define NMAX 100005
using namespace std;
ifstream f ("rmq.in");
ofstream g ("rmq.out");
int n , m , c , d;
int a[NMAX] , b[NMAX][20];
void RMQ()
{
int k = log(n) , i , j;
for(i = 1 ; i <= n ; i++)
b[i][0] = i;
for(j = 1 ; j <= k ; j++)
for(i = 1 ; i + (1 << j) - 1 <= n ; i++)
if(a[ b[i][j - 1] ] < a[ b[i + (1 << (j - 1))][j - 1] ])
b[i][j] = b[i][j - 1];
else b[i][j] = b[i + (1 << (j - 1))][j - 1];
}
void Query(int c , int d)
{
int k = log(d - c + 1);
if(a[ b[c][k] ] <= a[ b[d - (1 << k) + 1][k] ])
g << a[ b[c][k] ] << '\n';
else g << a[ b[d - (1 << k) + 1][k] ] << '\n';
}
int main()
{
int i;
f >> n >> m;
for(i = 1 ; i <= n ; i++)
f >> a[i];
RMQ();
for(i = 1 ; i <= m ; i++)
{
f >> c >> d;
Query(c , d);
}
return 0;
}