結果

問題 No.875 Range Mindex Query
ユーザー Imperi_NightImperi_Night
提出日時 2019-09-06 21:43:10
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 637 ms / 2,000 ms
コード長 2,463 bytes
コンパイル時間 1,051 ms
コンパイル使用メモリ 105,200 KB
実行使用メモリ 5,312 KB
最終ジャッジ日時 2023-09-06 23:18:38
合計ジャッジ時間 5,596 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 3 ms
4,376 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 344 ms
5,312 KB
testcase_12 AC 272 ms
4,380 KB
testcase_13 AC 229 ms
5,312 KB
testcase_14 AC 227 ms
5,220 KB
testcase_15 AC 318 ms
5,204 KB
testcase_16 AC 592 ms
5,220 KB
testcase_17 AC 628 ms
5,144 KB
testcase_18 AC 637 ms
5,224 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <assert.h>
#include <limits.h>
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using ll = long long;
using P = std::pair<ll, ll>;

#define rep(i, a, b) for (ll(i) = (a); i < (b); i++)
#define all(i) i.begin(), i.end()
#define debug(i) std::cerr << "debug "<< i << std::endl

// const ll MOD = 998244353;
const ll MOD = 1e9 + 7;

//最大値セグ木 0-indexed 初期値はminで指定
template <class T>
class SegmentTree {
 private:
  T n, init;
  std::vector<T> dat;
  std::function<T(T, T)> fn;

 public:
  SegmentTree(T i, T para, std::function<T(T, T)> fun) : init(para), fn(fun) {
    n = 1;
    while (n < i) {
      n *= 2;
    }
    dat = std::vector<T>(2 * n - 1, init);
  }

  // k番目(0-indexed)を値aで更新,dest=trueのときは更新前を破壊して初期化する
  void update(T k, T a, bool dest) {
    k += n - 1;
    if (dest)
      dat[k] = a;
    else
      dat[k] = fn(dat[k], a);

    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = fn(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }

  T query(T a, T b, T k, T l, T r) {
    if (r <= a || b <= l) {
      return init;
    }
    if (a <= l && r <= b) {
      return dat[k];
    } else {
      T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
      T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
      return fn(vl, vr);
    }
  }

  //[a,b)での最大値を返す
  T query(T a, T b) { return query(a, b, 0, 0, n); }

  T lower_bound(T a,T b,T value){
    ll left=a,right=b;

    while(right-left>1){
      ll mid=left+(right-left)/2;
      if(query(a,mid)==value)right=mid;
      else left=mid;
    }

    return left;
  }
};

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  //問題文中の添え字が0-indexか1-indexか確認!

  ll n,q;
  std::cin>>n>>q;

  SegmentTree<ll> a(n+2,MOD,[](ll x,ll y){return (x<y)?x:y;});

  rep(i,0,n){
    ll temp;
    std::cin>>temp;
    a.update(i,temp,true);
  }

  rep(i,0,q){
    ll query,l,r;
    std::cin>>query>>l>>r;
    l--;r--;
    if(query==1){
      ll al=a.query(l,l+1),ar=a.query(r,r+1);
      a.update(l,ar,true);a.update(r,al,true);
    }else{
      ll value=a.query(l,r+1);
      std::cout<<a.lower_bound(l,r+1,value)+1<<"\n";
    }
  }

  return 0;
}
0