Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define watch(x) cerr<<(#x)<<": "<<x<<endl
- using namespace std;
- const int MAXN = 5000;
- const int MAXLOGN = 50;
- int N;
- int arr[MAXN + 1];
- int sparse_table[MAXLOGN][MAXN + 1];
- void llenar_tabla() {
- for (int i = 0; i < N; i++) {
- sparse_table[0][i] = arr[i];
- }
- for (int i = 1; i <= log2(N); i++) {
- for (int j = 0; j + (1 << (i - 1)) < N; j++) {
- cerr << "[" << i - 1 << ", " << j << "], [" << i - 1 << ", " << (j + (1 << (i - 1))) << "]" << endl;
- /// faltaron los paréntesis :/
- sparse_table[i][j] = min(sparse_table[i - 1][j], sparse_table[i - 1][j + (1 << (i - 1))]);
- }
- }
- }
- int rmq(int a, int b) {
- int mejor_potencia = log2(b - a + 1);
- watch(mejor_potencia);
- return min(sparse_table[mejor_potencia][a], sparse_table[mejor_potencia][b - (1<<mejor_potencia) + 1]);
- }
- int main() {
- cin >> N;
- for (int i = 0; i < N; i++) {
- cin >> arr[i];
- }
- cerr<<"Llenando tabla..."<<endl;
- llenar_tabla();
- cout << "Sparse table: " << endl;
- for (int i = 0; i <= log2(N); i++) {
- for (int j = 0; j < N; j++) {
- cout << sparse_table[i][j] << ' ';
- }
- cout << endl;
- }
- int a, b;
- while(cin >> a >> b) {
- cout << rmq(a, b) << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement