洛谷 - P9588
Chthohepta · · 个人记录
题意
你需要维护一个队列,操作共四种,参数分别如下:
从队尾依次插入
思路
每一个数都插入队列末尾,肯定会TLE,MLE,所以我们可以在队列中存区间,形如
- 对于操作一,我们往队列后端插入一个区间
\left[\ 1,x \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;
}