Pagini recente » Cod sursa (job #1885504) | Profil Naty.Ladaru | Cod sursa (job #2439867) | Cod sursa (job #2460947) | Cod sursa (job #3134066)
#include <fstream>
#include <cmath>
using namespace std;
ifstream fin("rmq.in");
ofstream fout("rmq.out");
int n, m, i, j, stanga, dreapta, aux;
int main()
{
fin >> n >> m;
int** st = new int*[n];
for (i = 0; i < n; i++)
{
st[i] = new int[(int)sqrt(n)];
fin >> st[i][0];
}
for (j = 1; (1 << j) <= n; j++)
{
for (i = 0; (i + (1 << j) - 1) < n; i++)
{
if (st[i][j - 1] < st[i + (1 << (j-1))][j - 1])
st[i][j] = st[i][j - 1];
else
st[i][j] = st[i + (1 << (j-1))][j - 1];
}
}
while (m)
{
fin >> stanga >> dreapta;
aux = (int)log2(dreapta - stanga + 1);
if (st[stanga-1][aux] < st[dreapta - (1 << aux)][aux])
fout << st[stanga-1][aux] << endl;
else
fout << st[dreapta - (1 << aux)][aux] << endl;
m--;
}
fin.close();
fout.close();
return 0;
}