pankovamg

SparseTable(С++)

Sep 20th, 2021 (edited)
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5. #include <stack>
  6. #include <cmath>
  7. #include <set>
  8. #include <iomanip>
  9. #include <map>
  10. #include <queue>
  11. #include <cstdio>
  12. #include <deque>
  13.  
  14. using namespace std;
  15.  
  16. ifstream cin("input.txt");
  17. ofstream cout("output.txt");
  18.  
  19. typedef long long ll;
  20. typedef unsigned long long ull;
  21. typedef long double ld;
  22.  
  23. #define all(a) a.begin(), a.end()
  24. #define sqr(a) a * a
  25.  
  26. int INF = 2 * 1e9;
  27. vector <int> arr, lg;
  28. vector <vector <int> > sparce;
  29.  
  30. void build(int n) {
  31. arr.resize(n);
  32. lg.resize(n + 1);
  33. for (int i = 0; i < n; i++) {
  34. cin >> arr[i];
  35. }
  36.  
  37. for (int i = 2; i <= n; i++) {
  38. lg[i] = lg[i / 2] + 1;
  39. }
  40.  
  41. sparce.resize(lg[n] + 1, vector <int>(n));
  42. for (int i = 0; i < n; i++) {
  43. sparce[0][i] = arr[i];
  44. }
  45. for (int i = 1; i <= lg[n]; i++) {
  46. for (int j = 0; j + (1 << i) - 1 < n; j++) {
  47. sparce[i][j] = min(sparce[i - 1][j], sparce[i - 1][j + (1 << (i - 1))]);
  48. }
  49. }
  50. }
  51.  
  52. int find_min(int l, int r) {
  53. int len = lg[r - l + 1];
  54. return min(sparce[len][l], sparce[len][r - (1 << len) + 1]);
  55. }
  56.  
  57. int main() {
  58. int n, k;
  59. cin >> n >> k;
  60. build(n);
  61. }
Add Comment
Please, Sign In to add comment