結果

問題 No.875 Range Mindex Query
ユーザー onigawara_kypronigawara_kypr
提出日時 2019-09-06 22:01:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 93 ms / 2,000 ms
コード長 2,914 bytes
コンパイル時間 1,255 ms
コンパイル使用メモリ 109,532 KB
実行使用メモリ 10,180 KB
最終ジャッジ日時 2023-09-06 23:54:03
合計ジャッジ時間 2,891 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 83 ms
9,516 KB
testcase_12 AC 65 ms
7,024 KB
testcase_13 AC 58 ms
10,180 KB
testcase_14 AC 56 ms
10,004 KB
testcase_15 AC 78 ms
10,176 KB
testcase_16 AC 86 ms
10,136 KB
testcase_17 AC 93 ms
10,176 KB
testcase_18 AC 89 ms
10,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <array>
#include <algorithm>
#include <vector>
#include <set>
#include <unordered_set>
#include <cmath>
#include <complex>
#include <deque>
#include <iterator>
#include <numeric>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <limits>
#include <iomanip>
using namespace std;

using ll=long long;
template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i=k; i<(ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(),v.end()
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;}

const ll MOD = 1000000007;
const ll HIGHINF = (ll)1e18;

template < typename T >
struct SegmentTree {
    vector<T> data;
    long long n;

    SegmentTree(vector<T> vec) {
        long long sz = vec.size();
        n=1;
        while(n<sz) n<<=1;
        data.resize(2*n-1, make_pair(0, HIGHINF));

        for (long long i=0; i<sz; i++) data[i+n-1] = vec[i];
        for (long long i=n-2; i>=0; i--) {
            if (data[2*i+1].second > data[2*i+2].second) data[i] = data[2*i+2];
            else data[i] = data[2*i+1];
        }
    }

    void update(ll val1, ll val2) {
        val1 += (n-1);
        val2 += (n-1);
        ll tmp = data[val1].second;
        data[val1].second = data[val2].second;
        data[val2].second = tmp;
        while(val1 > 0) {
            val1 = (val1-1) / 2;
            if (data[2*val1+1].second > data[2*val1+2].second) data[val1] = data[2*val1+2];
            else data[val1] = data[2*val1+1];
        }
        while(val2 > 0) {
            val2 = (val2-1) / 2;
            if (data[2*val2+1].second > data[2*val2+2].second) data[val2] = data[2*val2+2];
            else data[val2] = data[2*val2+1];
        }
    }

    T query(long long a, long long b, long long k=0, long long l=0, long long r=-1) {
        if (r<0) r=n;
        if (b<=l || r<=a) return make_pair(0, HIGHINF);
        if(a <= l && r <= b) return data[k];
        P<ll, ll> vl = query(a, b, 2*k+1, l, (l+r)/2);
        P<ll, ll> vr = query(a, b, 2*k+2, (l+r)/2, r);
        if (vl.second < vr.second) return vl;
        else return vr;
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n, q; cin >> n >> q;
    V<P<ll, ll> > a(n);
    REP(i, n) {
        ll tmpa; cin >> tmpa;
        a[i] = make_pair(i, tmpa);
    }
    SegmentTree<P<ll, ll> > seg = SegmentTree<P<ll, ll> >(a);
    REP(_, q) {
        ll qi, l, r; cin >> qi >> l >> r;
        if (qi == 1) {
            seg.update(l-1, r-1);
        } else {
            P<ll, ll> ans = seg.query(l-1, r);
            cout << ans.first+1 << '\n';
        }
    }
    return 0;
}
0