使得队列可以快速求解出队列中的某些值,使用于没有逆元(最大最小值)或者逆元很难求(矩阵的逆(有时不存在))。
struct MinQueue {
stack<pii> F;
stack<pii> B;
int Min() {
int res = INF;
if (!F.empty())
res = min(res, F.top().second);
if (!B.empty())
res = min(res, B.top().second);
return res;
}
void Push(int x) {
int bMin = x;
if (!B.empty())
bMin = min(bMin, B.top().second);
B.push({x, bMin});
}
void Pop() {
if (!F.empty()) {
F.pop();
return;
}
int fMin = INF;
while (!B.empty()) {
int x = B.top();
B.pop();
fMin = min(fMin, x);
F.push({x, fMin});
}
if (!F.empty()) {
F.pop();
return;
}
}
};