結果

問題 No.875 Range Mindex Query
ユーザー koi_kotyakoi_kotya
提出日時 2019-09-06 21:36:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 287 ms / 2,000 ms
コード長 1,837 bytes
コンパイル時間 1,892 ms
コンパイル使用メモリ 173,996 KB
実行使用メモリ 6,804 KB
最終ジャッジ日時 2023-09-06 22:56:33
合計ジャッジ時間 5,217 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 3 ms
4,384 KB
testcase_02 AC 3 ms
4,384 KB
testcase_03 AC 2 ms
4,384 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,384 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 234 ms
6,136 KB
testcase_12 AC 189 ms
4,844 KB
testcase_13 AC 162 ms
6,536 KB
testcase_14 AC 158 ms
6,488 KB
testcase_15 AC 218 ms
6,552 KB
testcase_16 AC 266 ms
6,476 KB
testcase_17 AC 287 ms
6,788 KB
testcase_18 AC 278 ms
6,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> P;

#define p_ary(ary,a,b,i) do { cout << "["; for (int (i) = (a);(i) < (b);++(i)) cout << ary[(i)] << ((b)-1 == (i) ? "" : ", "); cout << "]\n"; } while(0)
#define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0)

int const INF = INT_MAX;
struct SegmentTree {
private:
    int n;
    vector<P> node;
public:
    SegmentTree(vector<P> v) {
        int sz = v.size();
        n = 1;
        while (n < sz) n *= 2;
        node.resize(2*n-1,P(INF,-1));
        for (int i = 0;i < sz;++i) node[i+n-1] = v[i];
        for (int i = n-2;i >= 0;--i) node[i] = min(node[2*i+1],node[2*i+2]);
    }
    void update(int x,P val) {
        x += (n-1);
        node[x] = val;
        while (x > 0) {
            x = (x-1)/2;
            node[x] = min(node[2*x+1],node[2*x+2]);
        }
    }

    P getmin(int a,int b,int k = 0,int l = 0,int r = -1) {
        if (r < 0) r = n;
        if (r <= a || b <= l) return P(INF,-1);
        if (a <= l && r <= b) return node[k];
        P vl = getmin(a,b,2*k+1,l,(l+r)/2);
        P vr = getmin(a,b,2*k+2,(l+r)/2,r);
        return min(vl,vr);
    }
};

int main() {
    int n,q;
    cin >> n >> q;
    vector<P> a(n);
    for (int i = 0;i < n;++i) {
        cin >> a[i].first;
        a[i].second = i;
    }
    SegmentTree seg(a);
    for (int z = 0;z < q;++z) {
        int c,l,r;
        cin >> c >> l >> r;
        if (c == 1) {
            P u = seg.getmin(l-1,l),v = seg.getmin(r-1,r);
            u.second = r-1;
            v.second = l-1;
            seg.update(l-1,v);
            seg.update(r-1,u);
        } else cout << seg.getmin(l-1,r).second+1 << endl;
    }
}
0