結果

問題 No.875 Range Mindex Query
ユーザー Plan8Plan8
提出日時 2020-01-18 16:54:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 295 ms / 2,000 ms
コード長 3,583 bytes
コンパイル時間 1,837 ms
コンパイル使用メモリ 175,584 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2023-09-10 03:54:21
合計ジャッジ時間 5,575 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 3 ms
4,384 KB
testcase_11 AC 207 ms
6,196 KB
testcase_12 AC 166 ms
4,948 KB
testcase_13 AC 142 ms
6,556 KB
testcase_14 AC 141 ms
6,468 KB
testcase_15 AC 194 ms
6,540 KB
testcase_16 AC 278 ms
6,408 KB
testcase_17 AC 295 ms
6,820 KB
testcase_18 AC 280 ms
6,668 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//include
//------------------------------------------
#include <bits/stdc++.h>
using namespace std;
//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}
//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef long long ll;
//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);
const long long INF = 1000000007;
//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))
//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
// chmax chmin
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

template<typename T>
struct SegmentTree {
	private:
        using Func = function<T(T, T)>;

        int n;
        vector<T> node;

        T init_v;
        Func func;

    public:

        SegmentTree(vector<T> a, Func _func, T _init_v) {
            int sz = a.size();
            n = 1;
            init_v = _init_v;
            func = _func;
    
            while (n < sz) n <<= 1;
            node.resize(2 * n - 1, init_v);
            for (int i = 0; i < sz; i++) node[i + n - 1] = a[i];
            for (int i = n - 2; i >= 0; i--) node[i] = func(node[i * 2 + 1], node[i * 2 + 2]);
        }

        void update(int pos, T v) {
            int k = pos + n - 1;
            node[k].first = v.first;
            while (k > 0) {
                k = (k - 1) / 2;
                node[k] = func(node[k * 2 + 1], node[k * 2 + 2]);
            }
        }

        T get(int pos) {
            int k = pos + n - 1;
            return node[k];
        }

        T query(int a, int b, int k = 0, int l = 0, int r = -1) {
            if (r < 0) r = n;
            if (r <= a || b <= l) return init_v;
            if (a <= l && r <= b) return node[k];
            T lv = query(a, b, k * 2 + 1, l, (l + r) / 2);
            T rv = query(a, b, k * 2 + 2, (l + r) / 2, r);
            return func(lv, rv);
        }

        int size(){
            return n;
        }
};

P _calc(P a, P b){
    return a.first > b.first ? b : a;
}

int main(void){
    int n, q; cin >> n >> q;
    vector<P> a(n); REP(i,n){cin >> a[i].first; a[i].second = i+1;}
    SegmentTree<P> st(a, _calc, P(n+1, n+1));
    
    REP(i,q){
        int c, l, r; cin >> c >> l >> r;
        l--; r--;
        if(c == 2) cout << st.query(l, r+1, 0, 0, st.size()).second << endl;
        else{
            P al = st.get(l), ar = st.get(r);
            st.update(l, ar); st.update(r, al);
        }
    }
    return 0;
}
0