結果

問題 No.878 Range High-Element Query
ユーザー milanis48663220milanis48663220
提出日時 2020-09-27 01:13:44
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 279 ms / 2,000 ms
コード長 1,402 bytes
コンパイル時間 921 ms
コンパイル使用メモリ 94,520 KB
実行使用メモリ 16,620 KB
最終ジャッジ日時 2023-09-12 13:47:41
合計ジャッジ時間 5,226 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 260 ms
15,420 KB
testcase_12 AC 182 ms
15,672 KB
testcase_13 AC 208 ms
12,552 KB
testcase_14 AC 155 ms
11,772 KB
testcase_15 AC 186 ms
15,424 KB
testcase_16 AC 257 ms
16,220 KB
testcase_17 AC 279 ms
16,620 KB
testcase_18 AC 274 ms
16,616 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int N, Q;

int nx[100000][20];
int a[100000];
set<int> st;

// iからl先
int calc(int i, int l){
    int ans = i;
    for(int j = 0; j < 20; j++){
        if(l&(1<<j)){
            ans = nx[ans][j];
        }
    }
    return ans;
}

vector<P> v;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    cin >> N >> Q;
    for(int i = 0; i < N; i++){
        cin >> a[i];
        v.push_back(P(a[i], i));
    }
    sort(v.begin(), v.end(), greater<P>());

    for(int i = 0; i < N; i++){
        int idx = v[i].second;
        
        auto p = st.upper_bound(idx);
        if(p == st.end()){
            nx[idx][0] = N;
        }else{
            nx[idx][0] = *p;
        }
        st.insert(idx);
    }

    for(int i = 0; i < 20; i++) nx[N][i] = N;
    for(int i = 1; i < 20; i++){
        for(int j = 0; j < N; j++){
            nx[j][i] = nx[nx[j][i-1]][i-1];
        }
    }
    
    for(int i = 0; i < Q; i++){
        int q, s, t; cin >> q >> s >> t; s--; t--;
        int l = 0, r = N;
        while(r-l > 1){
            int c = (l+r)/2;
            if(calc(s, c) > t) r = c;
            else l = c;
        }
        cout << l+1 << endl;
    }
}
0