結果

問題 No.1921 Range LthKth Query
ユーザー りあんりあん
提出日時 2022-05-01 22:37:21
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3,564 ms / 4,000 ms
コード長 3,176 bytes
コンパイル時間 4,981 ms
コンパイル使用メモリ 274,964 KB
実行使用メモリ 175,096 KB
最終ジャッジ日時 2023-09-13 16:43:32
合計ジャッジ時間 45,449 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,768 KB
testcase_01 AC 1 ms
175,096 KB
testcase_02 AC 1 ms
8,768 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 7 ms
4,380 KB
testcase_11 AC 69 ms
5,168 KB
testcase_12 AC 142 ms
8,288 KB
testcase_13 AC 1,275 ms
40,568 KB
testcase_14 AC 1,986 ms
50,004 KB
testcase_15 AC 3,564 ms
60,276 KB
testcase_16 AC 1,806 ms
42,232 KB
testcase_17 AC 2,150 ms
45,880 KB
testcase_18 AC 3,210 ms
59,792 KB
testcase_19 AC 3,138 ms
60,652 KB
testcase_20 AC 2,307 ms
60,844 KB
testcase_21 AC 406 ms
32,648 KB
testcase_22 AC 3,198 ms
62,104 KB
evil_01.txt TLE -
evil_02.txt TLE -
evil_03.txt TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

// #pragma GCC target("avx2")
#pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
using namespace std;

using P = pair<int, int>;
const int M = 1000000007;


template<typename T> class BIT {
private:
    int n,m; vector<vector<T> > bit;
public:
    // (i, j) に val を加算する
    void add(int i, int j, T val){
        for(int i_ = i+1; i_ < n; i_ += i_ & -i_)
            for(int j_ = j+1; j_ < m; j_ += j_ & -j_)
                bit[i_][j_] += val;
    }
    // [0,i]×[0,j]の範囲の和を求める
    T sum(int i, int j){
        T s = 0;
        for(int i_ = i+1; i_ > 0; i_ -= i_ & -i_)
            for(int j_ = j+1; j_ > 0; j_ -= j_ & -j_)
                s += bit[i_][j_];
        return s;
    }
    // [lx, rx)×[ly, ry)の範囲の和を求める
    T sum(int lx, int rx, int ly, int ry){
        return sum(rx-1, ry-1) - sum(lx-1, ry-1) - sum(rx-1, ly-1) + sum(lx-1, ly-1);
    }
    BIT(int sz1, int sz2){
        n = sz1 + 1, m = sz2 + 1;
        bit.resize(n, vector<T>(m, 0));
    }
    BIT(vector<vector<T> >& v){
        n = (int)v.size() + 1, m = (int)v[0].size() + 1;
        bit.resize(n, vector<T>(m, 0));
        for(int i = 0; i < n - 1; i++)
            for(int j = 0; j < m - 1; j++)
                add(i, j, v[i][j]);
    }
    void print(){
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                cout<< sum(i-1, i, j-1, j) << " ";
            }
            cout << "\n";
        }
    }
    void print_sum(){
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                cout<< sum(i-1, j-1) << " ";
            }
            cout << "\n";
        }
    }
};

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

    int n, k, l;
    cin >> n >> k >> l;
    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    vector<vector<P>> ps(n + 1);
    for (int i = 0; i < n; ++i) {
        priority_queue<int> lower;
        for (int j = i; j < n; ++j) {
            lower.push(a[j]);
            if ((int)lower.size() > k) lower.pop();
            if ((int)lower.size() == k) {
                ps[lower.top()].emplace_back(i, j);
            }
        }
    }
    int q;
    cin >> q;
    vector<int> ls(q), rs(q);
    for (int i = 0; i < q; ++i) {
        cin >> ls[i] >> rs[i];
        --ls[i];
    }

    const int MAXLOG = 12;
    vector<int> ng(q, 0), ok(q, n + 1);
    for (int _ = 0; _ < MAXLOG; ++_) {
        vector<vector<int>> qs(n + 1);
        for (int i = 0; i < q; ++i) {
            int m = (ng[i] + ok[i]) / 2;
            qs[m].push_back(i);
        }
        BIT<int> bit(n + 1, n + 1);
        for (int i = 0; i < n + 1; ++i) {
            for (auto& p : ps[i]) {
                bit.add(p.first, p.second, 1);
            }
            for (auto& j : qs[i]) {
                int cnt = bit.sum(ls[j], rs[j], ls[j], rs[j]);
                if (cnt >= l) {
                    ok[j] = i;
                }
                else {
                    ng[j] = i;
                }
            }
        }
    }
    for (auto& i : ok) {
        cout << i << '\n';
    }
    return 0;
}
0