結果

問題 No.3317 ワロングアンサーロングアンサーンスワロンガー
コンテスト
ユーザー elphe
提出日時 2025-08-10 17:31:42
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 141 ms / 2,000 ms
コード長 3,022 bytes
コンパイル時間 3,590 ms
コンパイル使用メモリ 293,800 KB
実行使用メモリ 7,724 KB
最終ジャッジ日時 2025-11-01 09:58:17
合計ジャッジ時間 8,962 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 63
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
const ull INF = 1000000000000000000ULL;
const int MAXT = 60;

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

    int N, Q;
    cin >> N >> Q;
    string S;
    cin >> S;

    struct Query { unsigned long long T, X; int idx; };
    vector<Query> qs(Q);
    for(int i = 0; i < Q; i++){
        cin >> qs[i].T >> qs[i].X;
        qs[i].idx = i;
    }

    // Precompute lengths: len[c][t] = length of expansion of char c after t replacements
    static ull len[26][MAXT+1];
    for(int c = 0; c < 26; c++){
        len[c][0] = 1;
    }
    string rep_a = "answer";
    string rep_w = "warong";
    for(int t = 1; t <= MAXT; t++){
        for(int c = 0; c < 26; c++){
            char ch = 'a' + c;
            ull v = 0;
            if(ch == 'a'){
                for(char d: rep_a){
                    v = min(INF, v + len[d-'a'][t-1]);
                }
            } else if(ch == 'w'){
                for(char d: rep_w){
                    v = min(INF, v + len[d-'a'][t-1]);
                }
            } else {
                v = 1;
            }
            len[c][t] = v;
        }
    }

    // Gather distinct clamped times
    vector<int> times;
    times.reserve(Q);
    for(auto &q: qs){
        int t = (q.T > (unsigned long long)MAXT ? MAXT : (int)q.T);
        times.push_back(t);
    }
    sort(times.begin(), times.end());
    times.erase(unique(times.begin(), times.end()), times.end());

    // Prepare prefix sums for each distinct time
    // pre[t][i] = sum of lengths of S[0..i-1] at time t, clamped to INF
    vector<vector<ull>> pre(MAXT+1);
    for(int t: times){
        auto &P = pre[t];
        P.assign(N+1, 0ULL);
        for(int i = 0; i < N; i++){
            ull add = len[S[i]-'a'][t];
            P[i+1] = P[i] + add < INF ? P[i] + add : INF;
        }
    }

    // Recursive function to get k-th char of expansion of c after t steps
    function<char(char,int,ull)> get_char = [&](char c, int t, ull k)->char {
        if(t == 0 || (c != 'a' && c != 'w')){
            // either no more replacements or this char never expands
            return c;
        }
        const string &rep = (c == 'a' ? rep_a : rep_w);
        for(char d: rep){
            ull clen = len[d-'a'][t-1];
            if(k <= clen){
                return get_char(d, t-1, k);
            }
            k -= clen;
        }
        // should never reach here
        return '?';
    };

    // Answer each query
    string answer(Q, '?');
    for(auto &q: qs){
        int t = (q.T > (unsigned long long)MAXT ? MAXT : (int)q.T);
        auto &P = pre[t];
        // binary search for the block in S
        int pos = int(lower_bound(P.begin()+1, P.end(), q.X) - P.begin()) - 1;
        // pos in [0..N-1], and P[pos] < X <= P[pos+1]
        ull offset = q.X - P[pos];
        char c0 = S[pos];
        answer[q.idx] = get_char(c0, t, offset);
    }

    // output
    cout << answer << "\n";
    return 0;
}
0