結果

問題 No.875 Range Mindex Query
ユーザー Imperi_NightImperi_Night
提出日時 2019-11-10 15:27:39
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 75 ms / 2,000 ms
コード長 2,975 bytes
コンパイル時間 960 ms
コンパイル使用メモリ 104,032 KB
実行使用メモリ 7,208 KB
最終ジャッジ日時 2023-10-13 07:46:27
合計ジャッジ時間 2,558 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,484 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 1 ms
4,352 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 1 ms
4,368 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 75 ms
7,208 KB
testcase_12 AC 61 ms
5,072 KB
testcase_13 AC 56 ms
7,124 KB
testcase_14 AC 55 ms
7,180 KB
testcase_15 AC 72 ms
7,184 KB
testcase_16 AC 68 ms
7,172 KB
testcase_17 AC 75 ms
7,176 KB
testcase_18 AC 72 ms
7,204 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 <unordered_map>
#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
 
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, std::pair<T1, T2> pa) {
  return os << pa.first << " " << pa.second;
}
 
template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> vec) {
  for (int i = 0; i < vec.size(); i++)os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  return os;
}
 
template<typename T1,typename T2>
inline bool chmax(T1& a,T2 b){return a<b && (a=b,true);}
 
template<typename T1,typename T2>
inline bool chmin(T1& a,T2 b){return a>b && (a=b,true);}
 
ll pow_mod(ll a, ll b, ll mod=-1) {
  if ((a == 0)||(mod!=-1&&a%mod==0)) {
    return 0;
  }
 
  ll x = 1;
 
  while (b > 0) {
    if (b & 1) {
      x = (mod!=-1)?(x * a) % mod:x*a;
    }
    a = (mod!=-1)?(a * a) % mod:a*a;
    b >>= 1;
  }
  return x;
}
 
// const ll MOD = 998244353;
const ll MOD = 1e9 + 7;

//非再帰抽象セグ木 0-indexed 単位元はiで指定
template <typename T,typename F>
class SegmentTree {
 private:
  long long n;
  T init;
  std::vector<T> dat;
  F fn;

 public:
  SegmentTree(long long i, T para, F 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(long long 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(long long a, long long b){
    assert(0<=a&&b<=n);
    T val=init;

    long long l=a+n-1,r=b+n-1;
    for(;l<r;l=(l>>1),r=(r>>1)){
      if(!(r&1)){
        r--;
        val=fn(val,dat[r]);
      }
      if(!(l&1)){
        val=fn(val,dat[l]);
        l++;
      }
    }

    return val;
  }
};
 
int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);

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

  auto f=[](P x,P y){return (x.first<y.first)?x:y;};

  SegmentTree<P,decltype(f)> a(n+2,{MOD,-1},f);

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

  rep(i,0,q){
    ll query,l,r;
    std::cin>>query>>l>>r;
    l--;r--;

    if(query==1){
      P al=a.query(l,l+1),ar=a.query(r,r+1);
      a.update(l,{ar.first,l},true);a.update(r,{al.first,r},true);
    }else{
      std::cout<<a.query(l,r+1).second+1<<"\n";
    }
  }

  return 0;
}
0