Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define watch(x) cerr<<(#x)<<": "<<x<<endl
  3.  
  4. using namespace std;
  5.  
  6. const int MAXN = 5000;
  7. const int MAXLOGN = 50;
  8. int N;
  9. int arr[MAXN + 1];
  10. int sparse_table[MAXLOGN][MAXN + 1];
  11.  
  12. void llenar_tabla() {
  13. for (int i = 0; i < N; i++) {
  14. sparse_table[0][i] = arr[i];
  15. }
  16. for (int i = 1; i <= log2(N); i++) {
  17. for (int j = 0; j + (1 << (i - 1)) < N; j++) {
  18. cerr << "[" << i - 1 << ", " << j << "], [" << i - 1 << ", " << (j + (1 << (i - 1))) << "]" << endl;
  19. /// faltaron los paréntesis :/
  20. sparse_table[i][j] = min(sparse_table[i - 1][j], sparse_table[i - 1][j + (1 << (i - 1))]);
  21. }
  22. }
  23. }
  24.  
  25. int rmq(int a, int b) {
  26. int mejor_potencia = log2(b - a + 1);
  27. watch(mejor_potencia);
  28. return min(sparse_table[mejor_potencia][a], sparse_table[mejor_potencia][b - (1<<mejor_potencia) + 1]);
  29. }
  30.  
  31. int main() {
  32. cin >> N;
  33. for (int i = 0; i < N; i++) {
  34. cin >> arr[i];
  35. }
  36. cerr<<"Llenando tabla..."<<endl;
  37. llenar_tabla();
  38. cout << "Sparse table: " << endl;
  39. for (int i = 0; i <= log2(N); i++) {
  40. for (int j = 0; j < N; j++) {
  41. cout << sparse_table[i][j] << ' ';
  42. }
  43. cout << endl;
  44. }
  45. int a, b;
  46. while(cin >> a >> b) {
  47. cout << rmq(a, b) << endl;
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement