結果

問題 No.878 Range High-Element Query
ユーザー merom686merom686
提出日時 2019-09-07 12:18:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 57 ms / 2,000 ms
コード長 1,232 bytes
コンパイル時間 700 ms
コンパイル使用メモリ 80,868 KB
実行使用メモリ 11,032 KB
最終ジャッジ日時 2023-09-08 14:56:07
合計ジャッジ時間 2,745 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 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,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 52 ms
9,308 KB
testcase_12 AC 37 ms
10,196 KB
testcase_13 AC 40 ms
7,744 KB
testcase_14 AC 31 ms
7,036 KB
testcase_15 AC 38 ms
9,596 KB
testcase_16 AC 51 ms
10,948 KB
testcase_17 AC 54 ms
10,936 KB
testcase_18 AC 57 ms
11,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n, q;
    cin >> n >> q;

    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    int k = 1, t = 1;
    while (t * 2 <= n - 1) t *= 2, k++;

    vector<pair<int, int>> b(n);
    vector<vector<int>> c(k, vector<int>(n));
    auto it = b.end();
    for (int i = n - 1; i >= 0; i--) {
        pair<int, int> t = { a[i], i };
        it = upper_bound(it, b.end(), t);
        c[0][i] = it == b.end() ? n : it->second;
        *--it = t;
    }

    for (int l = 1; l < k; l++) {
        for (int i = 0; i < n; i++) {
            int j = c[l - 1][i];
            c[l][i] = j < n ? c[l - 1][j] : n;
        }
    }

    for (int h = 0; h < q; h++) {
        int t, l, r;
        cin >> t >> l >> r;
        l--;

        int i = l, a = 1;
        for (int l = k - 1; l >= 0; l--) {
            int t = c[l][i];
            if (t < r) {
                i = t;
                a += 1 << l;
            }
        }
        cout << a << '\n';
    }

    return 0;
}
0