結果

問題 No.875 Range Mindex Query
ユーザー yasunori
提出日時 2025-02-19 20:22:23
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 226 ms / 2,000 ms
コード長 5,472 bytes
コンパイル時間 3,947 ms
コンパイル使用メモリ 289,688 KB
実行使用メモリ 6,528 KB
最終ジャッジ日時 2025-02-19 20:22:31
合計ジャッジ時間 7,488 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template <typename T> using min_priority_queue = priority_queue<T,vector<T>,greater<T>>;

random_device seed_gen;
mt19937 engine(seed_gen());

int64_t get_time(){
    struct::timespec t;
    clock_gettime(CLOCK_MONOTONIC, &t);
    return t.tv_sec * int64_t(1'000'000'000) + t.tv_nsec;
}

template<typename T>
void invec(vector<T> &v){
    for(auto &i : v) cin >> i;
    return;
}

template<typename T>
void outvec(vector<T> &v, string s = " "){
    bool b = false;
    for(auto i : v){
        if(b) cout << s;
        else b = true;
        cout << i;
    }
    cout << endl;
    return;
}

template <typename T>
bool chmin(T &a, const T& b) {
    if (a > b) {
        a = b;  // aをbで更新
        return true;
    }
    return false;
}

template <typename T>
bool chmax(T &a, const T& b) {
    if (a < b) {
        a = b;  // aをbで更新
        return true;
    }
    return false;
}

template<typename T>
class segtree {
private:
    int n;
    vector<T> tree;
    function<T(T, T)> op;
    T identity;

public:
    segtree(int n_, function<T(T, T)> op_, T identity_)
        : op(op_), identity(identity_) {
        int x = 1;
        while(x < n_) x *= 2;
        n = x;
        tree.resize(n * 2 - 1, identity);
    }
    
    void build(vector<T> &data) {
        assert(data.size() <= n);
        for (int i = 0; i < data.size(); i++) {
            tree[i + n - 1] = data[i];
        }
        for (int i = n - 2; i >= 0; i--) {
            tree[i] = op(tree[i * 2 + 1], tree[i * 2 + 2]);
        }
    }

    void update(int i, T value) {
        i += n - 1;
        tree[i] = value;
        while (i > 0) {
            i = (i - 1) / 2;
            tree[i] = op(tree[i * 2 + 1], tree[i * 2 + 2]);
        }
    }

    T query_sub(int a, int b, int i, int l, int r) {
        if(r <= a || b <= l) {
            return identity;
        }
        else if(a <= l && r <= b) {
            return tree[i];
        }
        else {
            T res_l = query_sub(a, b, i * 2 + 1, l, (l + r) / 2);
            T res_r = query_sub(a, b, i * 2 + 2, (l + r) / 2, r);
            return op(res_l, res_r);
        }
    }

    T query(int a, int b) {
        return query_sub(a, b, 0, 0, n);
    }

    class reference {
    private:
        segtree &seg;
        int idx;

    public:
        reference(segtree& seg, int idx) : seg(seg), idx(idx) {}

        reference& operator=(const T& value) {
            seg.update(idx, value);
            return *this;
        }

        operator T() const {
            return seg.query(idx, idx + 1);
        }
    };

    reference operator[](int idx) {
        return reference(*this, idx);
    }
};

bool yn(bool b){
    if(b) cout << "Yes" << endl;
    else cout << "No" << endl;
    return b;
}

bool check_with_simple_solution;
bool calc_time;
bool random_input;
bool debug_output;
int num_of_testcase;

using ans_type = int;

void input(){
    if(num_of_testcase > 1){
    }
    if(random_input){
    }   
    else{
    }
    return;
}

void output_input(){
    return;
}

void pre_solve(){
    return;
}

using pi = pair<int,int>;
pi e = {1<<30,1<<30};
pi op(pi x, pi y) {
    return min(x, y);
}

void swap(segtree<pi> &S, int i, int j) {
    int x = ((pi)S[i]).first;
    int y = ((pi)S[j]).first;
    S[i] = {y, i};
    S[j] = {x, j};
    return;
}

ans_type solve(){
    int n, q; cin >> n >> q;
    vector<int> A(n); invec(A);

    vector<pi> B(n);
    for(int i = 0; i < n; i++) {
        B[i] = {A[i], i};
    }

    segtree<pi> S(n, op, e);
    S.build(B);

    for(int i = 0; i < q; i++) {
        int t; cin >> t;
        if(t == 1) {
            int l, r; cin >> l >> r;
            l--; r--;
            swap(S, l, r);
        }
        else {
            int l, r; cin >> l >> r;
            l--;
            cout << S.query(l, r).second + 1 << endl;
        }
    }
    return ans_type();
}

ans_type solve_simple(){
    return ans_type();
}

void output(ans_type ans){
    return;
}

int main(){
    ios::sync_with_stdio(false);
	cin.tie(nullptr);
    cout << fixed << setprecision(20);

    check_with_simple_solution = 0;
    calc_time = 0;
    random_input = 0;
    debug_output = 0;
    num_of_testcase = 1;
    
    if(num_of_testcase == 0) cin >> num_of_testcase;

    pre_solve();

    if(check_with_simple_solution){
        for(int i = 0; i < num_of_testcase; i++){
            if(debug_output) cout << "begin case " << i << " " << string(16, '=') << endl;
            input();
            ans_type ans = solve();
            if(debug_output) cout << string(16, '=') << endl;
            ans_type ans_simple = solve_simple();
            if(ans != ans_simple){
                cout << "input: " << endl;
                output_input();
                cout << "output: " << endl;
                output(ans);
                cout << "expected: " << endl;
                output(ans_simple);
            }
            if(debug_output) cout << "end case " << i << " " << string(16, '=') << endl;
        }
    }
    else{
        int64_t time_start = get_time();
        for(int i = 0; i < num_of_testcase; i++){
            input();
            output(solve());
        }
        int64_t time_end = get_time();

        if(calc_time){
            double time_per_case = (time_end - time_start) / (1e9 * num_of_testcase);
            cout << fixed << setprecision(3) << time_per_case << "[second/case]" << endl;
        }
    }

    return 0;
}
0