結果

問題 No.875 Range Mindex Query
ユーザー yy
提出日時 2022-11-08 20:44:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 277 ms / 2,000 ms
コード長 7,572 bytes
コンパイル時間 1,696 ms
コンパイル使用メモリ 173,304 KB
実行使用メモリ 51,600 KB
最終ジャッジ日時 2023-09-29 09:56:29
合計ジャッジ時間 5,685 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
51,400 KB
testcase_01 AC 43 ms
51,544 KB
testcase_02 AC 44 ms
51,468 KB
testcase_03 AC 43 ms
51,448 KB
testcase_04 AC 43 ms
51,408 KB
testcase_05 AC 44 ms
51,388 KB
testcase_06 AC 44 ms
51,432 KB
testcase_07 AC 44 ms
51,400 KB
testcase_08 AC 43 ms
51,476 KB
testcase_09 AC 43 ms
51,508 KB
testcase_10 AC 44 ms
51,464 KB
testcase_11 AC 277 ms
51,476 KB
testcase_12 AC 228 ms
51,464 KB
testcase_13 AC 223 ms
51,380 KB
testcase_14 AC 221 ms
51,564 KB
testcase_15 AC 273 ms
51,484 KB
testcase_16 AC 252 ms
51,448 KB
testcase_17 AC 268 ms
51,600 KB
testcase_18 AC 263 ms
51,468 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
// #define LOCAL // 提出時はコメントアウト
#define DEBUG_

typedef long long ll;
const double EPS = 1e-9;
const ll INF = ((1LL<<62)-(1LL<<31));
typedef vector<ll> vecl;
typedef pair<ll, ll> pairl;
template<typename T> using uset = unordered_set<T>;
template<typename T, typename U> using mapv = map<T,vector<U>>;
template<typename T, typename U> using umap = unordered_map<T,U>;

#define ALL(v) v.begin(), v.end()
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define sz(x) (ll)x.size()
ll llceil(ll a,ll b) { return (a+b-1)/b; }
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<class T> vector<vector<T>> genarr(ll n, ll m, T init) { return vector<vector<T>>(n,vector<T>(m,init)); }

///// DEBUG
#define DUMPOUT cerr
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
template<typename T>istream&operator>>(istream&is,vector<T>&vec){for(T&x:vec)is>>x;return is;}
template<typename T,typename U>ostream&operator<<(ostream&os,pair<T,U>&pair_var){os<<"("<<pair_var.first<<", "<<pair_var.second<<")";return os;}
template<typename T>ostream&operator<<(ostream&os,vector<T>&vec){os<<"{";for(int i=0;i<vec.size();i++){os<<vec[i]<<(i+1==vec.size()?"":", ");}
os<<"}";return os;}
template<typename T,typename U>ostream&operator<<(ostream&os,map<T,U>&map_var){os<<"{";repi(itr,map_var){os<<*itr;itr++;if(itr!=map_var.end())os<<", ";itr--;}
os<<"}";return os;}
template<typename T>ostream&operator<<(ostream&os,set<T>&set_var){os<<"{";repi(itr,set_var){os<<*itr;itr++;if(itr!=set_var.end())os<<", ";itr--;}
os<<"}";return os;}
template<typename T>ostream&operator<<(ostream&os,uset<T>&set_var){os<<"{";repi(itr,set_var){if(itr!=set_var.begin())os<<", "; os<<*itr;}
os<<"}";return os;}
template<typename T,typename U>ostream&operator<<(ostream&os,umap<T,U>&map_var){os<<"{";repi(itr,map_var){if(itr!=map_var.begin())os<<", "; os<<*itr;}
os<<"}";return os;}
void dump_func(){DUMPOUT<<endl;}
template<class Head,class...Tail>void dump_func(Head&&head,Tail&&...tail){DUMPOUT<<head;if(sizeof...(Tail)>0){DUMPOUT<<", ";}
dump_func(std::move(tail)...);}
#ifndef LOCAL
#undef DEBUG_
#endif
#ifdef DEBUG_
#define DEB
#define dump(...)                                                          \
DUMPOUT << "  " << string(#__VA_ARGS__) << ": "                            \
        << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]"        \
        << endl                                                            \
        << "    ",                                                         \
    dump_func(__VA_ARGS__)
#else
#define DEB if (false)
#define dump(...)
#endif

//////////

#ifndef DEBUG_
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#endif



// SegTree: https://atcoder.github.io/ac-library/document_ja/segtree.html

// @param n `0 <= n`
// @return minimum non-negative `x` s.t. `n <= 2**x`
int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
}

// @param n `1 <= n`
// @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
int bsf(unsigned int n) {
#ifdef _MSC_VER
    unsigned long index;
    _BitScanForward(&index, n);
    return index;
#else
    return __builtin_ctz(n);
#endif
}


template <class S, S (*op)(S, S), S (*e)()> struct segtree {
  public:
    segtree() : segtree(0) {}
    segtree(int n) : segtree(std::vector<S>(n, e())) {}
    segtree(const std::vector<S>& v) : _n(int(v.size())) {
        log = ceil_pow2(_n);
        size = 1 << log;
        d = std::vector<S>(2 * size, e());
        for (int i = 0; i < _n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }

    void set(int p, S x) { // O(logn)
        assert(0 <= p && p < _n);
        p += size;
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S get(int p) { // O(1)
        assert(0 <= p && p < _n);
        return d[p + size];
    }

    S prod(int l, int r) { // [l,r): O(logn)
        assert(0 <= l && l <= r && r <= _n);
        S sml = e(), smr = e();
        l += size;
        r += size;

        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return op(sml, smr);
    }

    S all_prod() { return d[1]; } // [0,n)

    template <bool (*f)(S)> int max_right(int l) { // segtree上で二分探索 O(logn)
        return max_right(l, [](S x) { return f(x); });
    }
    template <class F> int max_right(int l, F f) { // segtree上で二分探索 O(logn)
        assert(0 <= l && l <= _n);
        assert(f(e()));
        if (l == _n) return _n;
        l += size;
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!f(op(sm, d[l]))) {
                while (l < size) {
                    l = (2 * l);
                    if (f(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }

    template <bool (*f)(S)> int min_left(int r) { // segtree上で二分探索 O(logn)
        return min_left(r, [](S x) { return f(x); });
    }
    template <class F> int min_left(int r, F f) { // segtree上で二分探索 O(logn)
        assert(0 <= r && r <= _n);
        assert(f(e()));
        if (r == 0) return 0;
        r += size;
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!f(op(d[r], sm))) {
                while (r < size) {
                    r = (2 * r + 1);
                    if (f(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

  private:
    int _n, size, log;
    std::vector<S> d;

    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
};

// (S,*): 結合律の成立と単位元の存在を満たせばOK
// 交換律は必要ないので演算の向きを恣意的に決めてOK

struct S {
    ll a;
    ll size;
};

ll B = 1e9+10;
#define val(x) (x/B)
#define idx(x) (x%B)

S op(S l, S r) { return {val(l.a) < val(r.a) ? l.a : r.a, l.size+r.size}; } // S * S

S e() { return {B*B-1,0}; } // モノイドの単位元



int solve(ostringstream &cout) {
    #ifdef LOCAL
    ifstream in("../../input.txt");
    cin.rdbuf(in.rdbuf());
    #endif

    ll n,q;
    cin>>n>>q;
    segtree<S,op,e> seg(1e6);
    rep(i,n) {
        ll x;
        cin>>x;
        seg.set(i,S{B*x+i,0});
    }

    rep(_,q) {
        ll c,l,r;
        cin>>c>>l>>r;
        l--;
        if (c==1) {
            ll lv = seg.get(l).a, rv = seg.get(--r).a;
            lv = val(lv) * B + r, rv = val(rv) * B + l;
            seg.set(l,S{rv,0}), seg.set(r,S{lv,0});
        }
        else {
            ll x = seg.prod(l,r).a;
            dump(l,r,val(x),idx(x),x);
            cout << idx(x) + 1 << endl;
        }
    }

    return 0;
}

int main() {
    ostringstream oss;
    int res = solve(oss);

    cout << oss.str();
    return res;
}
0