結果

問題 No.1471 Sort Queries
ユーザー cutmdocutmdo
提出日時 2022-08-31 02:11:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 32 ms / 2,000 ms
コード長 2,226 bytes
コンパイル時間 1,350 ms
コンパイル使用メモリ 114,320 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-25 08:37:25
合計ジャッジ時間 3,177 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 12 ms
5,376 KB
testcase_14 AC 11 ms
5,376 KB
testcase_15 AC 13 ms
5,376 KB
testcase_16 AC 6 ms
5,376 KB
testcase_17 AC 10 ms
5,376 KB
testcase_18 AC 8 ms
5,376 KB
testcase_19 AC 6 ms
5,376 KB
testcase_20 AC 11 ms
5,376 KB
testcase_21 AC 8 ms
5,376 KB
testcase_22 AC 8 ms
5,376 KB
testcase_23 AC 21 ms
5,376 KB
testcase_24 AC 21 ms
5,376 KB
testcase_25 AC 25 ms
5,376 KB
testcase_26 AC 16 ms
5,376 KB
testcase_27 AC 28 ms
5,376 KB
testcase_28 AC 27 ms
5,376 KB
testcase_29 AC 16 ms
5,376 KB
testcase_30 AC 21 ms
5,376 KB
testcase_31 AC 23 ms
5,376 KB
testcase_32 AC 20 ms
5,376 KB
testcase_33 AC 16 ms
5,376 KB
testcase_34 AC 32 ms
5,376 KB
testcase_35 AC 32 ms
5,376 KB
testcase_36 AC 11 ms
5,376 KB
testcase_37 AC 10 ms
5,376 KB
testcase_38 AC 12 ms
5,376 KB
testcase_39 AC 13 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <map>

#include <vector>
#include <algorithm>
#include <cmath>
#include <numeric>
class Mo {

    const int q;
    const std::vector<int> lq;
    const std::vector<int> rq;
    const std::vector<int> order;
    int ni, nl, nr;

    auto sort_query() const {
        std::vector<int> order(q);
        std::iota(order.begin(), order.end(), 0);
        int w = std::sqrt(q);
        std::sort(order.begin(), order.end(), [&](int a, int b) {
            if(lq[a] / w != lq[b] / w) return lq[a] < lq[b];
            if(rq[a] == rq[b]) { return false; }
            bool less = (rq[a] < rq[b]);
            bool flg = (lq[a] / w) & 1;
            return static_cast<bool>(less ^ flg);
        });

        return order;
    }

public:
    Mo(int q,
       const std::vector<int>& lq,
       const std::vector<int>& rq)
        :q(q), lq(lq), rq(rq), order(sort_query()),
        ni(0), nl(0), nr(-1) {
    }

    template<class Lambda1, class Lambda2>
    auto process(const Lambda1& add, const Lambda2& del) {
        const auto l = lq[order[ni]];
        const auto r = rq[order[ni]];
        while(nl > l) { add(--nl); }
        while(nr < r) { add(++nr); }
        while(nl < l) { del(nl++); }
        while(nr > r) { del(nr--); }
        return order[ni++];
    }
};

using ll = long long;
using std::cout;
using std::cin;
constexpr char endl = '\n';

signed main() {
    int n, q;
    cin >> n >> q;
    std::string s;
    cin >> s;

    std::vector<int> lq; lq.reserve(q);
    std::vector<int> rq; rq.reserve(q);
    std::vector<int> xv; xv.reserve(q);
    for(int _ = 0; _ < q; ++_) {
        int l, r, x;
        cin >> l >> r >> x;
        --l; --r;
        lq.emplace_back(l);
        rq.emplace_back(r);
        xv.emplace_back(x);
    }

    auto mo = Mo(q, lq, rq);

    std::map<char, int> mp;
    std::vector<char> ans(q);
    for(int _ = 0; _ < q; ++_) {
        auto i = mo.process([&](int i) {
            mp[s[i]]++;
        }, [&](int i) {
            mp[s[i]]--;
        });

        int cnt = 0;
        for(const auto& [c, x] : mp) {
            cnt += x;
            if(cnt >= xv[i]) { ans[i] = c; break; }
        }
    }

    for(const auto& c : ans) { cout << c << endl; }
}
0