結果

問題 No.878 Range High-Element Query
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2019-09-10 11:22:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 283 ms / 2,000 ms
コード長 3,115 bytes
コンパイル時間 2,236 ms
コンパイル使用メモリ 205,840 KB
実行使用メモリ 11,140 KB
最終ジャッジ日時 2023-09-15 13:03:47
合計ジャッジ時間 5,342 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,204 KB
testcase_01 AC 4 ms
8,128 KB
testcase_02 AC 5 ms
8,204 KB
testcase_03 AC 4 ms
7,992 KB
testcase_04 AC 4 ms
8,020 KB
testcase_05 AC 4 ms
8,280 KB
testcase_06 AC 4 ms
7,972 KB
testcase_07 AC 4 ms
8,036 KB
testcase_08 AC 4 ms
8,288 KB
testcase_09 AC 5 ms
8,040 KB
testcase_10 AC 3 ms
7,968 KB
testcase_11 AC 231 ms
10,548 KB
testcase_12 AC 235 ms
10,788 KB
testcase_13 AC 181 ms
10,024 KB
testcase_14 AC 144 ms
9,780 KB
testcase_15 AC 219 ms
10,628 KB
testcase_16 AC 272 ms
11,140 KB
testcase_17 AC 283 ms
11,052 KB
testcase_18 AC 281 ms
11,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
#define def -1
template<class V, int NV> struct SegTree { //[l,r)
    V comp(V& l, V& r) { return max(l, r); };

    vector<V> val; SegTree() { val = vector<V>(NV * 2, def); }
    V get(int x, int y, int l = 0, int r = NV, int k = 1) {
        if (r <= x || y <= l)return def; if (x <= l && r <= y)return val[k];
        auto a = get(x, y, l, (l + r) / 2, k * 2); 
        auto b = get(x, y, (l + r) / 2, r, k * 2 + 1);
        return comp(a, b);
    }
    void update(int i, V v) {
        i += NV; val[i] = v;
        while (i>1) i >>= 1, val[i] = comp(val[i * 2], val[i * 2 + 1]);
    }
    void add(int i, V v) { update(i, val[i + NV] + v); }
    V operator[](int x) { return get(x, x + 1); }
};
/*---------------------------------------------------------------------------------------------------
            ∧_∧
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     @hamayanhamayan
    /   \     | |
    /   / ̄ ̄ ̄ ̄/  |
  __(__ニつ/     _/ .| .|____
     \/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/

int N, Q, A[101010];
int dp[17][101010];
SegTree<int, 1 << 17> st;
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N >> Q;
    rep(i, 0, N) cin >> A[i];
    rep(i, 0, N) st.update(i, A[i]);

    rep(p, 0, 17) rep(cu, 0, N + 1) dp[p][cu] = N;
    rep(cu, 0, N) {
        if(A[cu] < st.get(cu, N)) {
            int ok = N, ng = cu;
            while(ng + 1 != ok) {
                int md = (ng + ok) / 2;
                if(A[cu] < st.get(cu, md))
                    ok = md;
                else
                    ng = md;
            }
            dp[0][cu] = ok-1;
        }
    }
    rep(p, 1, 17) rep(cu, 0, N) dp[p][cu] = dp[p - 1][dp[p - 1][cu]];

    rep(i, 0, Q) {
        int t, l, r; cin >> t >> l >> r;
        l--;
        r--;

        int ans = 1;
        int p = 16;
        int pp = 1;
        rep(i, 0, 16) pp *= 2;
        int cu = l;
        while (0 <= p and cu <= r) {
            int to = dp[p][cu];
            if(to <= r) {
                ans += pp;
                cu = to;
            }
            p--;
            pp /= 2;
        }
        printf("%d\n", ans);
    }
}



0