Cod sursa(job #2781929)
| Utilizator | Data | 10 octombrie 2021 23:12:50 | |
|---|---|---|---|
| Problema | Range minimum query | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.59 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream fin("rmq.in");
ofstream fout("rmq.out");
vector < int > dp[18];
int main()
{
int n, q, i, j, x, a, b;
fin >> n >> q;
for(i = 0; i < n; i++) fin >> x, dp[0].push_back(x);
x = log2(n);
for(i = 1; i <= x; i++)
for(j = 0; j < n - ((1<<i)-1); j++)
dp[i].push_back(min(dp[i-1][j], dp[i-1][j+(1<<(i-1))]));
while(q--)
{
fin >> a >> b;
a--, b--;
x = log2(b - a + 1);
fout << min(dp[x][a], dp[x][b-((1<<x)-1)]) << '\n';
}
return 0;
}
