結果

問題 No.875 Range Mindex Query
ユーザー tarattata1tarattata1
提出日時 2019-09-06 21:48:09
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 123 ms / 2,000 ms
コード長 2,867 bytes
コンパイル時間 1,416 ms
コンパイル使用メモリ 90,832 KB
実行使用メモリ 4,772 KB
最終ジャッジ日時 2023-09-06 23:30:29
合計ジャッジ時間 3,547 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 123 ms
4,704 KB
testcase_12 AC 101 ms
4,380 KB
testcase_13 AC 93 ms
4,712 KB
testcase_14 AC 89 ms
4,764 KB
testcase_15 AC 121 ms
4,756 KB
testcase_16 AC 105 ms
4,772 KB
testcase_17 AC 112 ms
4,716 KB
testcase_18 AC 106 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <list>
#include <iterator>
#include <assert.h>
#pragma warning(disable:4996) 
 
typedef long long ll;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF 9223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
using namespace std;


const long long INF2 = (((ll)1<<31)-1);

template<class T> class SegTree   // 0-indexed
{
private:
    int n;             // 葉の数
    vector<T> data;    // ノードの値を持つ配列
    T def;                // 初期値かつ単位元
    
    T operation(T a, T b)    // 区間クエリで使う処理
    {
        return min(a, b);   // 区間minクエリ
    }

    T update(T a, T b)      // 点更新で使う処理
    {
        //return a+b;   // 加算の場合
        return b;       // 更新の場合
    }

    // 区間[a,b)の総和。ノードk=[l,r)に着目している。
    T _query(int a, int b, int k, int l, int r) {
        if (r <= a || b <= l) return def; // 交差しない
        if (a <= l && r <= b)
            return data[k]; // a,l,r,bの順で完全に含まれる
        else {
            T c1 = _query(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子
            T c2 = _query(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子
            return operation(c1, c2);
        }
    }
 
public:
    SegTree(int _n) {     // _n:必要サイズ
        def = make_pair(INF,INF);        // 初期値かつ単位元
        n = 1;
        while (n < _n) n *= 2;
        data = vector<T>(2 * n - 1, def);
    }

    // 場所i(0-indexed)の値をxで更新
    void change(int i, T x) {
        i += n - 1;
        data[i] = update(data[i], x);
        while (i > 0) {
            i = (i - 1) / 2;
            data[i] = operation(data[i * 2 + 1], data[i * 2 + 2]);
        }
    }

    // [a, b)の区間クエリを実行
    T query(int a, int b) {
        return _query(a, b, 0, 0, n);
    }
 
    // 添字でアクセス
    T operator[](int i) {
        return data[i + n - 1];
    }
};

int main(int argc, char* argv[])
{
    int n,q;
    scanf("%d%d", &n, &q);


    SegTree<pair<int,int> > S(n);

    int i;
    for(i=0; i<n; i++) {
        int a;
        scanf("%d", &a);
        S.change(i, make_pair(a,i));
    }

    for(i=0; i<q; i++) {
        int t,x,y;
        scanf("%d%d%d", &t, &x, &y); x--; y--;
        if(t==1) {
            auto tmp0=S.query(x,x+1);
            auto tmp1=S.query(y,y+1);
            S.change(x, make_pair(tmp1.first,x));
            S.change(y, make_pair(tmp0.first,y));
        }
        else {
            auto ans=S.query(x,y+1);
            printf("%d\n", ans.second+1);
        }
    }

    return 0;
}
0