跳到正文

算法·理论

【模板】双端单调队列

xxxasybt2023约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年9月6日
收录
2026年9月23日
更新
2026年11月11日
篇幅
约 2 分钟

版本

  1. v1当前
C++
template <typename T,typename Ord = greater<T> >
struct monodeque
{
	list<pair<T,int> > q; Ord cmp;
	inline monodeque& main_front(T x) {for (;!q.empty()&&!cmp(x,q.front().first);q.pop_front());return*this;} 
	inline monodeque& main_back(T x) {for (;!q.empty()&&!cmp(q.back().first,x);q.pop_back());return*this;}
	inline monodeque& expr_front(int x) {for (;!q.empty()&&q.front().second<=x;q.pop_front());return*this;}
	inline monodeque& expr_back(int x) {for (;!q.empty()&&q.back().second<=x;q.pop_back());return*this;}
	inline monodeque& push_front(T x,int p) {q.push_front({x,p});return*this;}
	inline monodeque& push_back(T x,int p) {q.push_back({x,p});return*this;}
	inline monodeque& pop_front() {q.pop_front();return*this;}
	inline monodeque& pop_back() {q.pop_back();return*this;}
	inline bool empty() {return q.empty();}
	inline pair<T,int> front() {return q.front();}
	inline pair<T,int> back() {return q.back();}
	inline void clear() {q.clear();}
};
原文