Advertisement
rudolf222222

Untitled

Nov 17th, 2022 (edited)
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.80 KB | None | 0 0
  1. #include <math.h>
  2.  
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <vector>
  6. bool SortByVal(const std::pair<int, int>& a, const std::pair<int, int>& b) {
  7.   return a.first < b.first;
  8. }
  9. class SparceTable {
  10.  public:
  11.   SparceTable(std::vector<int>& arr) {
  12.     arr_ = arr;
  13.     const size_t kSize = arr.size();
  14.     size_t kHeight =
  15.         static_cast<size_t>(std::log2(static_cast<double>(arr.size()))) + 1;
  16.     for (size_t i = 0; i <= kHeight; ++i) {
  17.       ST_.push_back({});
  18.       ST_[i].assign(kSize + 1, {0, 0});
  19.     }
  20.     for (size_t i = 0; i < kSize; ++i) {
  21.       ST_[0][i].first = i;
  22.       ST_[0][i].second = INT32_MAX;
  23.     }
  24.     for (size_t i = 1; i <= kHeight; i++) {
  25.       for (size_t j = 0; j + G(i) <= kSize; j++) {
  26.         std::pair<int, int> new_min;
  27.         new_min = Minimums(ST_[i - 1][j], ST_[i - 1][j + G(i - 1)]);
  28.         ST_[i][j] = new_min;
  29.       }
  30.     }
  31.     logs_.assign(kSize + 1, 0);
  32.     logs_[1] = 0;
  33.     logs_[0] = -1;
  34.     for (size_t i = 2; i <= kSize; ++i) {
  35.       logs_[i] = logs_[i / 2] + 1;
  36.     }
  37.   }
  38.   std::pair<int, int> Minimums(std::pair<int, int>& fp,
  39.                                std::pair<int, int>& sp) {
  40.     std::vector<int> indexes;
  41.     indexes.push_back(fp.first);
  42.     indexes.push_back(sp.second);
  43.     indexes.push_back(fp.second);
  44.     indexes.push_back(sp.first);
  45.     std::sort(indexes.begin(), indexes.end());
  46.     indexes.erase(std::unique(indexes.begin(), indexes.end()), indexes.end());
  47.     std::pair<int, int> res;
  48.     std::vector<std::pair<int, int>> temp;
  49.     for (size_t i = 0; i < indexes.size(); ++i) {
  50.       if (indexes[i] == INT32_MAX) {
  51.         temp.push_back({INT32_MAX, INT32_MAX});
  52.       } else {
  53.         temp.push_back({arr_[indexes[i]], indexes[i]});
  54.       }
  55.     }
  56.     std::sort(temp.begin(), temp.end());
  57.     res.first = temp[0].second;
  58.     res.second = temp[1].second;
  59.     return res;
  60.   }
  61.   int Query(size_t left, int right) {
  62.     size_t i = static_cast<int>(logs_[right - left + 1]);
  63.     std::pair<int, int> minimum =
  64.         Minimums(ST_[i][left], ST_[i][right - G(i) + 1]);
  65.     return minimum.second == INT32_MAX ? arr_[minimum.first]
  66.                                        : arr_[minimum.second];
  67.     // return arr_[minimum.second];
  68.   }
  69.  
  70.  private:
  71.   static size_t G(size_t x) { return 1 << x; }
  72.   std::vector<std::vector<std::pair<int, int>>> ST_;
  73.   std::vector<int> logs_;
  74.   std::vector<int> arr_;
  75. };
  76.  
  77. void Requests() {
  78.   size_t n = 0;
  79.   size_t m = 0;
  80.   std::cin >> n >> m;
  81.   int a = 0;
  82.   std::vector<int> arr;
  83.   for (size_t i = 0; i < n; ++i) {
  84.     std::cin >> a;
  85.     arr.push_back(a);
  86.   }
  87.   SparceTable table(arr);
  88.   int l = 0;
  89.   int r = 0;
  90.   for (size_t i = 0; i < m; ++i) {
  91.     std::cin >> l >> r;
  92.     std::cout << table.Query(l - 1, r - 1) << "\n";
  93.   }
  94. }
  95.  
  96. int main() {
  97.   Requests();
  98.   return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement