Pagini recente » Cod sursa (job #1073731) | Statistici Andrei George (Arcadego999) | Cod sursa (job #1187254) | Cod sursa (job #2891997) | Cod sursa (job #2173085)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("rmq.in");
ofstream fout("rmq.out");
int p[100005], rmq[22][100005], n, q;
void Citire()
{
fin >> n >> q;
for(int i = 1; i <= n; i++)
fin >> rmq[0][i];
}
void Build()
{
p[1] = 0;
for(int i = 2; i <= n; i++)
p[i] = p[i / 2] + 1;
int L = p[n];
for(int i = 1; i <= L; i++)
for(int j = 1; j <= n - (1 << i) + 1; j++)
rmq[i][j] = min(rmq[i - 1][j],
rmq[i -1][j + (1 << (i - 1))]);
}
void Rez()
{
int x, y;
while(q--)
{
fin >> x >> y;
if(x > y)
swap(x, y);
int L = p[y - x + 1];
int t = rmq[L][x];
int t1 = rmq[L][y - (1 << L) + 1];
fout << min(t, t1) << "\n";
}
}
int main()
{
Citire();
Rez();
return 0;
}