跳到正文

算法·理论

数据结构

pzzzz约 5 分钟
原文
文章信息与版本

文章信息

发布
2024年10月25日
收录
2026年6月17日
更新
2026年8月18日
篇幅
约 5 分钟

版本

  1. v1当前

引用大神的数据结构模板

@mediocre_

C++
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 4e7;
inline int read() {
	char ch = getchar();
	int x = 0, f = 1;
  	while (!isdigit(ch)) if (ch == '-') f = -1, ch = getchar();
  	while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
  	return x * f;
}
struct priority_queue_min {
	int w[MAXN], tot = 0;
	int top() { return w[1]; }
	void modify(int x) {
		if (x == 1 || w[x] > w[x >> 1]) return ;
		swap(w[x], w[x >> 1]);
		modify(x >> 1);
	}
	void push(int x) {
		w[++tot] = x;
		modify(tot);
	}
	void repair(int x) {
		if (x << 1 > tot) return ;
		int tar = x << 1;
		if ((x << 1) + 1 <= tot) tar = w[x << 1] < w[(x << 1) + 1] ? x << 1 : (x << 1) + 1;
		if (w[x] > w[tar]) {
			swap(w[x], w[tar]);
			repair(tar);
		}
	}
	void pop() {
		swap(w[1], w[tot--]);
		repair(1);
	} 
	int size() { return tot; }
	bool empty() { return !tot; }
} ;
struct priority_queue_max {
	int w[MAXN], tot = 0;
	int top() { return w[1]; }
	void modify(int x) {
		if (x == 1 || w[x] < w[x >> 1]) return ;
		swap(w[x], w[x >> 1]);
		modify(x >> 1);
	}
	void push(int x) {
		w[++tot] = x;
		modify(tot);
	}
	void repair(int x) {
		if (x << 1 > tot) return ;
		int tar = x << 1;
		if ((x << 1) + 1 <= tot) tar = w[x << 1] > w[(x << 1) + 1] ? x << 1 : (x << 1) + 1;
		if (w[x] < w[tar]) {
			swap(w[x], w[tar]);
			repair(tar);
		}
	}
	void pop() {
		swap(w[1], w[tot--]);
		repair(1);
	}
	int size() { return tot; }
	bool empty() { return !tot; }
} ;
struct Queue{
	int w[MAXN], head = 0, tail = -1;
	int front() { return w[head % MAXN]; }
	void push(int x) { w[++tail % MAXN] = x; }
	void pop() { if (head <= tail) ++head % MAXN; }
	int size() { return tail - head + 1; }
	bool empty() { return tail < head; }
	void clear() { head = 0, tail = -1; }
} ;
struct Stack {
	int w[MAXN], tail = 0;
	int front() { return w[tail]; }
	void push(int x) { w[++tail] = x; }
	void pop() { if (tail > 0) --tail; }
	int size() { return tail + 1; }
	bool empty() { return !tail; }
	void clear() { tail = 0; }
} ;
struct Deque {
	int w[MAXN], head = 2e5, tail = 2e5 - 1;
	int front() { return w[head]; }
	void push_front(int x) { w[--head] = x; }
	void push_back(int x) { w[++tail] = x; }
	void pop_front() { if (head <= tail) ++head; }
	void pop_back() { if (tail >= head) --tail; }
	int size() { return tail - head + 1; }
	bool empty() { return tail < head; }
	void clear() { head = 2e5, tail = 2e5 - 1; }
} ;
priority_queue_min q1;
priority_queue_max q2;
Queue q3;
Stack q4;
Deque q5;
int main() { 
	
	for (int i = 1; i <= 100; ++i) {
		q3.push(i);
		cout << i << " ";
		q3.pop();
	}
	while (!q3.empty()) {
		printf("%d ", q3.front());
		q3.pop();
	}
	
	return 0;
}
原文