[RMI 2020] 软盘 Floppy

题目要求 \(L\) 的长度为 \(2n\) 级别,那么要用 \(\mathcal{O}(n)\) 级别的数据结构存储,然后又是求最大值的位置,考虑单调栈,把询问挂右端点上,求的就是栈里第一个 \(\ge l\) 的位置。

时间复杂度 \(\mathcal{O}(n \log n)\),串长 \(\le 2n\)

void save_to_floppy(const string &s);
void read_array(int tc, const vector<int> &a) {
    int n = a.size(), top = 0;
    string s;
    vector<int>stk;
    for (int i = 0; i < n; i++) {
        while (!stk.empty() && a[stk.back()] < a[i]) s += '0', stk.pop_back();
        stk.push_back(i);
        s += '1';
    }
    save_to_floppy(s);
}
vector<int> solve_queries(int tc, int n, const string &s, const vector<int> &a, const vector<int> &b) {
    int m = a.size();
    vector<int> ans(m), stk;
    vector<vector<int>> q(n);
    for (int i = 0; i < m; i++) q[b[i]].push_back(i);
    for (int i = 0, j = 0; i < n; i++) {
        while (s[j] == '0') {
            stk.pop_back();
            j++;
        }
        j++;
        stk.push_back(i);
        for (int id : q[i]) ans[id] = *lower_bound(stk.begin(), stk.end(), a[id]);
    }
    return ans;
}
posted @ 2026-04-10 20:13  循环一号  阅读(7)  评论(0)    收藏  举报