結果

問題 No.875 Range Mindex Query
ユーザー ngtkanangtkana
提出日時 2019-09-06 21:36:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 252 ms / 2,000 ms
コード長 4,893 bytes
コンパイル時間 2,342 ms
コンパイル使用メモリ 210,616 KB
実行使用メモリ 7,476 KB
最終ジャッジ日時 2023-09-06 22:59:12
合計ジャッジ時間 4,941 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 158 ms
7,024 KB
testcase_12 AC 131 ms
5,424 KB
testcase_13 AC 106 ms
7,352 KB
testcase_14 AC 106 ms
7,140 KB
testcase_15 AC 148 ms
7,476 KB
testcase_16 AC 234 ms
7,328 KB
testcase_17 AC 252 ms
7,324 KB
testcase_18 AC 240 ms
7,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define loop(n) for (int ngtkana_is_genius = 0; ngtkana_is_genius < int(n); ngtkana_is_genius++)
#define rep(i, begin, end) for(int i = int(begin); i < int(end); i++)
#define all(v) v.begin(), v.end()
#define lint long long
auto cmn = [](auto& a, auto b){if (a > b) {a = b; return true;} return false;};
auto cmx = [](auto& a, auto b){if (a < b) {a = b; return true;} return false;};
void debug_impl() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail){
  std::cerr << " " << head;
  debug_impl(tail...);
}
#define debug(...)\
  std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";\
  debug_impl(__VA_ARGS__);\
  std::cerr << std::noboolalpha;

template<typename Value, typename BinaryOp>
class segment_tree
{
    int                 n, N;
    BinaryOp            op;
    Value               id;
    std::vector<Value>  table;

    void cal (int u)
    {
      table.at(u) = op(table.at(2 * u), table.at(2 * u + 1));
    }

  public:
    segment_tree(int size, BinaryOp op, Value id, const std::vector<Value>& v):
      n     (std::pow(2, int(std::log2(2 * size - 1)))),
      N     (n * 2),
      op    (op),
      id    (id),
      table (N, id)
    {
      assert(n > 0);
      std::move(v.begin(), v.end(), table.begin() + n);
      for (int i = n - 1; i != 0; i--) cal(i);
    }

    segment_tree(int size, BinaryOp op, Value id):
      segment_tree(size, std::move(op), id, std::vector<Value>(size, id))
      {}

    auto at (int i) const -> Value
    {
      return table.at(n + i);
    }

    auto collect () const -> std::vector<Value>
    {
      auto ret = std::vector<Value>(n);
      for (auto i = 0; i < n; i++)
      {
        ret.at(i) = at(i);
      }
      return ret;
    }

    auto query (int l, int r) const -> Value
    {
      struct state {int top, left, right;};

      auto ret = id;
      std::stack<state> stk; stk.push({1, 0, n});
      while (!stk.empty())
      {
        auto now = stk.top(); stk.pop();
        if (l <= now.left && now.right <= r)
        {
          ret = op(ret, table.at(now.top));
          continue;
        }
        int mid = (now.left + now.right) / 2;
        if (l < mid) stk.push({2 * now.top,     now.left,  mid});
        if (mid < r) stk.push({2 * now.top + 1, mid, now.right});
      }
      return ret;
    }

    void update (int u, Value val)
    {
      table.at(u += n) = val;
      for (u /= 2; u != 0; u /= 2) cal(u);
    }

    void add (int u, Value val)
    {
      update(u, at(u) + val);
    }
};

template<typename Value, typename BinaryOp>
auto make_segment_tree(int size, BinaryOp op, Value vid)
{
  return segment_tree<Value, BinaryOp>(size, std::move(op), vid);
}

template<typename Value, typename BinaryOp>
auto make_segment_tree(int size, BinaryOp op, Value vid, std::vector<Value> v)
{
  return segment_tree<Value, BinaryOp>(size, std::move(op), vid, std::move(v));
}

template <typename T>
std::istream& operator>> (std::istream& is, std::vector<T>& v) {
  for (auto & x : v) is >> x;
  return is;
}

template <typename T>
std::ostream& operator<< (std::ostream& os, const std::vector<T>& v) {
  auto n = v.size();
  os << "{";
  for (size_t i = 0; i < n; i++)
    {os << (i > 0 ? "," : "") << v.at(i);}
  return os << "}";
}

template <typename T, typename U>
std::ostream& operator<< (std::ostream& os, const std::pair<T, U>& pair){
  return os << "(" << pair.first << "," << pair.second << ")";
}

template <typename T, typename U>
std::istream& operator>> (std::iostream& is, std::pair<T, U>& pair) {
  return is >> pair.first >> pair.second;
}

template <typename InputIterator, typename OutputIterator>
constexpr OutputIterator
  coenumerate(InputIterator  first,
              InputIterator  last,
              OutputIterator result)
{
  int cnt = 0;
  while (first != last)
  {
    *result = std::make_pair(*first, cnt);
    result++, first++, cnt++;
  }
  return result;
}

int main() {
  std::cin.tie(0); std::cin.sync_with_stdio(false);
  constexpr int inf = 1 << 30;
  int n, q; std::cin >> n >> q;
  std::vector<int> a(n); std::cin >> a;
  for (auto & x : a) x--;
  std::vector<std::pair<int, int>> pairs(n);
  coenumerate(all(a), pairs.begin());
  auto min_tree = make_segment_tree<std::pair<int, int>>(
    n,
    [](auto p, auto q) { return p.first < q.first ? p : q; },
    std::pair<int, int>{inf, -1},
    pairs
  );
  // debug(min_tree.collect());
  loop(q) {
    int c, l, r;
    std::cin >> c >> l >> r;
    if (c == 1) {
      l--, r--;
      auto L = min_tree.at(l).first;
      auto R = min_tree.at(r).first;
      min_tree.update(l, std::pair<int, int>{R, l});
      min_tree.update(r, std::pair<int, int>{L, r});
      // debug(min_tree.collect());
    } else {
      l--;
      auto ret = min_tree.query(l, r).second + 1;
      std::cout << ret << std::endl;
    }
  }
  return 0;
}
0