結果

問題 No.1921 Range LthKth Query
ユーザー 👑 tatyam
提出日時 2022-05-01 20:52:38
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,196 bytes
コンパイル時間 1,003 ms
コンパイル使用メモリ 89,748 KB
実行使用メモリ 10,140 KB
最終ジャッジ日時 2024-07-01 01:17:26
合計ジャッジ時間 7,772 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 10 TLE * 1 -- * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cstdint>
#include <iostream>
#include <numeric>
#include <vector>

int main() {
    int n,k,L;
    std::cin >> n >> k >> L;
    std::vector<int> a(n);
    for(auto &val: a) {
        std::cin >> val;
    }
    auto solve = [&](int l, int r) -> int {
        int ok = n, ng = 0;
        while(std::abs(ok - ng) > 1) {
            int mid = (ok + ng)/2;
            int cnt = 0;
            int nl = l;
            int ret = 0;
            for(int i = l; i < r; i++) {
                if(a[i] <= mid) {
                    cnt++;
                }
                if(cnt < k) continue;
                if(cnt > k) {
                    while(a[nl] > mid) nl++;
                    if(a[nl] <= mid) {
                        cnt--;
                    }
                    nl++;
                }
                assert(cnt == k);
                while(a[nl] > mid) nl++;
                ret += nl-l+1;
            }
            if(ret >= L) ok = mid;
            else ng = mid;
        }
        return ok;
    };
    int q;
    std::cin >> q;
    while(q--) {
        int l,r;
        std::cin >> l >> r;
        l--;
        std::cout << solve(l, r) << '\n';
    }
}
0