洛谷 - P9588

· · 个人记录

题意

你需要维护一个队列,操作共四种,参数分别如下:

从队尾依次插入 1,2,3,⋯,x; 弹出队头的前 k 个元素; 查询队列中的第 k 个元素; 查询队列中所有元素的最大值。 维护队列的同时对于每个操作三和操作四,输出查询的答案。

思路

每一个数都插入队列末尾,肯定会TLE,MLE,所以我们可以在队列中存区间,形如 \left\{ \left[\ 1,x_1 \right], \left[\ 1,x_2 \right], \cdots, \left[\ 1,x_m \right] \right\}

代码

#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int N = 2e5 + 3;
int n, h = 1, t, c;
ll x, y;
ll q[N][2], cnt[N], sc;
int yh = 1, yt, l, r, mid;
ll yq[N];

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

    cin >> n >> n;
    while(n --){
        cin >> x;
        if(x == 1){
            cin >> y;
            q[++ t][0] = 1, q[t][1] = y;
            cnt[t] = cnt[t - 1] + q[t][1];
            while(yh <= yt && q[yq[yt]][1] < q[t][1]) yt --;
            yq[++ yt] = t;
        }
        if(x == 2){
            cin >> y;
            sc += y;
            while((q[h][1] - q[h][0] + 1) <= y){
                y -= (q[h][1] - q[h][0] + 1), h ++;
            }
            q[h][0] += y;
            while(yh <= yt && yq[yh] < h) yh ++;
        }
        if(x == 3){
            cin >> y;
            c = lower_bound(cnt + 1, cnt + 1 + t, y + sc) - cnt - 1;
            cout << y + sc - cnt[c] << '\n';
        }
        if(x == 4){
            cout << q[yq[yh]][1] << '\n';
        }
    }

    return 0;
}