数据结构 and 扫描线、离散化
单调栈(单调不递增、单调不递减、严格单调递减、严格单调递增),单调队列,二叉堆,二叉搜索树,线段树,树状数组,并查集,ST表,LCA,HASH,TRIE,双端栈,双端队列,优先队列,扫描线,离散化
重点
树状数组,并查集,ST表,LCA,HASH,TRIE
⭐⭐线段树,扫描线,离散化
单调栈代码
//模板P5788
#include<bits/stdc++.h>
using namespace std;
const int maxx=3e7+5;
int n,a[maxx],f[maxx];
stack<int>s;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=n;i>=1;i--){
while(!s.empty()&&a[s.top()]<=a[i]) s.pop;
//重点<=(严格单调递增)、<(单调不递减)、>(单调不递增)、>=(严格单调递减)
// f[i]=s.empty()?0:s.top();
if(s.empty()) f[i]=0;
else f[i]=s.top();
s.push(i);
}
for(int i=1;i<=n;i++) printf("%d ",f[i]);
return 0;
}单调队列代码
//模板P1886
#include <iostream>
using namespace std;
const int maxn = 1e6 + 10;
struct My_pair{
int idx, val;
//结构体:输入的第idx个数字值为val
My_pair(): idx(0), val(0) {}
My_pair(int idx, int val): idx{idx}, val{val} {}
bool operator < (const My_pair& p) const
{
return val < p.val;
//自定义小于号
}
bool operator > (const My_pair& p) const
{
return val > p.val;
//自定义大于号
}
};
//下为计算最小值的单调队列(单调不递减)
int min_head = 1, min_tail = 0, min_ans[maxn]; //首=1,尾=0表示空队列
My_pair min_queue[maxn];
void min_enqueue(My_pair element)
{
while (min_head <= min_tail && element < min_queue[min_tail])
min_tail--;
min_tail++;
min_queue[min_tail] = element;
}
int min_dequeue(int left_bound)
{
//idx比左边界小的出队
while (min_head <= min_tail && min_queue[min_head].idx < left_bound)
min_head++;
return min_queue[min_head].val;
}
//下为计算最大值的单调队列(单调不递增)
int max_head = 1, max_tail = 0, max_ans[maxn];
My_pair max_queue[maxn];
void max_enqueue(My_pair element)
{
while (max_head <= max_tail && element > max_queue[max_tail])
max_tail--;
max_tail++;
max_queue[max_tail] = element;
}
int max_dequeue(int left_bound)
{
//idx比左边界小的出队
while (max_head <= max_tail && max_queue[max_head].idx < left_bound)
max_head++;
return max_queue[max_head].val;
}
int main()
{
int n, m, x;
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
cin >> x;
min_enqueue(My_pair(i, x));
max_enqueue(My_pair(i, x));
if (i >= m) //记录答案
{
//左边界是i-m+1,idx比左边界小的要出队
min_ans[i] = min_dequeue(i-m+1);
max_ans[i] = max_dequeue(i-m+1);
}
}
for (int i = m; i <= n; i++)
cout << min_ans[i] << ' ';
cout << endl;
for (int i = m; i <= n; i++)
cout << max_ans[i] << ' ';
cout << endl;
return 0;
}字典树代码
//例题P10470
#include <iostream>
using namespace std;
const int maxS = 1e6 + 10;
int trie_size, son[maxS][26], is_end[maxS];
int main()
{
int n, m, x;
string str;
cin >> n >> m;
while (n--) //循环n次(每次n减1,直到减为0)
{
cin >> str;
int node = 0; //trie的树根是0节点
for (int i = 0; i < str.size(); i++)
{
int ch = str[i] - 'a';
if (son[node][ch] == 0) //说明trie里没有这条边
{
trie_size++;
son[node][ch] = trie_size;
//新建节点并连边
}
node = son[node][ch];
//往ch对应的边走一步
}
is_end[node]++;
//记录单词结束位置
}
while (m--)
{
cin >> str;
int node = 0, ans = 0;
for (int i = 0; i < str.size(); i++)
{
int ch = str[i] - 'a';
node = son[node][ch];
//往ch对应的边走一步
if (node == 0) //没有这条边
break;
ans += is_end[node];
}
cout << ans << endl;
}
return 0;
}优先队列代码
//模板P3378
#include <iostream>
using namespace std;
const int maxn = 1e6 + 10;
int heap_size, heap[maxn];
void swap(int &a, int &b){
int t = a;
a = b;
b = t;
}
void push(int val){
heap[++heap_size] = val;
for (int node = heap_size; node > 1; node /= 2){
if (heap[node] < heap[node/2])
swap(heap[node], heap[node/2]); //父节点值更大,交换
else
break; //否则停止
}
}
int pop(){
swap(heap[1], heap[heap_size]);
heap_size--;
int node = 1;
while (node * 2 <= heap_size){
int child = node * 2;
if (child + 1 <= heap_size && heap[child + 1] < heap[child]) //确保右儿子存在
child++;
if (heap[child] < heap[node]){ //子节点值更小,交换
swap(heap[child], heap[node]);
node = child;
}
else //否则停止
break;
}
return heap[heap_size+1]; //返回被弹出的元素,当然本题用不到
}
int main(){
int n, op, x;
cin >> n;
while (n--){
cin >> op;
if (op == 1){
cin >> x;
push(x);
}
else if (op == 2) cout << heap[1] << endl;
else pop();
}
return 0;
}
树状数组代码
#include <iostream>
using namespace std;
const int maxn = 5e5 + 10;
int n;
long long tree[maxn]; //初始全为0
int lowbit(int x) {return x & -x;}
//lowbit可以将一个数的二进制的最后一个1出现的位置显示出来
//例如 10101100的lowbit为100,101001的lowbit为1
void add(int idx, int val){
for (; idx <= n; idx += lowbit(idx))
tree[idx] += val;
}
long long query(int idx){
long long ans = 0;
for (; idx; idx -= lowbit(idx))
ans += tree[idx];
return ans;
}
int main(){
int m, val;
cin >> n >> m;
for (int idx = 1; idx <= n; idx++){
cin >> val;
add(idx, val); //直接对第idx个位置增加数字val,不需要用a[]数组来初始化
}
while (m--){
int op, idx, l, r;
cin >> op;
if (op == 1){
cin >> idx >> val;
add(idx, val);
}
else{
cin >> l >> r;
cout << query(r) - query(l - 1) << endl;
}
}
return 0
}链式前向星和邻接表没有本质区别
链式前向星模板
#include <iostream>
using namespace std;
const int maxn = 1e5 + 10;
const int maxm = 2e5 + 10;
int n, m;
int head[maxn];
int next[maxm*2];
//head[]与next[]维护一堆单向链表,每个链表储存以u为起点的所有边的编号,其链表头为head[u],next[]是指针
int to[maxm*2]; //to[idx] 表示编号为idx的边,它的目的地
int val[maxm*2]; //to[idx] 表示编号为idx的边,它的权重
void add_edge(int idx, int u, int v, int weight) //idx是这条边的编号
{
//u是这条边的起点,所以维护u相关的那条单向链表
next[idx] = head[u]; //idx的指针指向老的表头
head[u] = idx; //idx成为新表头
to[idx] = v; //v是这条边的目的地
val[idx] = weight; //weight是这条边的权重
}
bool vis[maxn];
void DFS(int u)
{
vis[u] = 1;
for (int idx = head[u]; idx; idx = next[idx])
{
int v = to[idx], weight = val[idx];
if (!vis[v]) DFS(v);
}
}
int main()
{
memset(head, 0, sizeof(head));
int u, v;
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
cin >> u >> v >> weight;
add_edge(i*2-1, u, v, weight);
add_edge(i*2+1, v, u, weight);
}
}邻接表
#include<iostream>
using namespace std;
class Graph {
private:
// 边节点结构体(邻接表的核心组成)
struct EdgeNode {
int vertex; // 目标顶点编号
int weight; // 边权重(支持带权图)
EdgeNode *next; // 指向下一个邻接节点的指针
};
// 顶点节点结构体
struct VertexNode {
int vertex; // 顶点编号
EdgeNode *firstEdge;// 指向第一条邻接边的指针
};
int vertices; // 顶点总数
VertexNode *nodes; // 顶点数组(邻接表的存储主体)
public:
Graph(int vertices); // 构造函数初始化顶点数组
~Graph(); // 析构函数释放动态内存
void addEdge(int u, int v, int w); // 添加有向边(u->v)及权重
void printGraph(); // 打印邻接表结构
};
/* 构造函数:初始化邻接表结构 */
Graph::Graph(int vertices) {
this->vertices = vertices;
this->nodes = new VertexNode[vertices]; // 动态分配顶点数组
for(int i = 0; i < vertices; ++i) {
nodes[i].vertex = i; // 设置顶点编号
nodes[i].firstEdge = nullptr; // 初始无边连接
}
}
/* 析构函数:释放所有动态分配的内存 */
Graph::~Graph() {
for(int i = 0; i < vertices; ++i) {
EdgeNode *curr = nodes[i].firstEdge;
// 遍历删除顶点i的所有邻接边
while(curr) {
EdgeNode *temp = curr;
curr = curr->next;
delete temp; // 释放边节点
}
}
delete[] nodes; // 释放顶点数组
}
/* 添加有向边(u->v)及权重(头插法高效插入) */
void Graph::addEdge(int u, int v, int w) {
EdgeNode *newNode = new EdgeNode; // 创建新边节点
newNode->vertex = v;
newNode->weight = w;
// 将新边插入链表头部(时间复杂度O(1))
newNode->next = nodes[u].firstEdge;
nodes[u].firstEdge = newNode;
}
/* 打印邻接表结构(验证图构建正确性) */
void Graph::printGraph() {
for(int i = 0; i < vertices; ++i) {
EdgeNode *curr = nodes[i].firstEdge;
cout << "Vertex" << i << " -> ";
// 遍历输出所有邻接顶点及权重
while(curr) {
cout << curr->vertex << "(" << curr->weight << ") ";
curr = curr->next;
}
cout << endl;
}
}
int main() {
// 测试用例1:创建5个顶点的有向图
Graph graph(5);
// 添加带权边(模拟交通网络)
graph.addEdge(0, 1, 2); // 0→1权值2
graph.addEdge(0, 3, 4); // 0→3权值4
graph.addEdge(1, 2, 5); // 1→2权值5
graph.addEdge(2, 4, 3); // 2→4权值3
graph.addEdge(3, 4, 7); // 3→4权值7
graph.addEdge(4, 0, 1); // 4→0权值1(环路测试)
cout << "--- 邻接表结构 ---" << endl;
graph.printGraph();
// 测试用例2:空图验证
Graph emptyGraph(3);
cout << "\n--- 空图结构 ---" << endl;
emptyGraph.printGraph(); // 所有顶点无边
// 测试用例3:重复边处理
Graph repeatGraph(2);
repeatGraph.addEdge(0, 1, 10);
repeatGraph.addEdge(0, 1, 20); // 允许重复边(代表多重图)
cout << "\n--- 重复边结构 ---" << endl;
repeatGraph.printGraph();
return 0;
}邻接矩阵
#include<iostream>
using namespace std;
#define inf -1 // 定义无穷大为-1,表示顶点间无直接连接
class Graph{
private:
int vertices; // 顶点总数
int ?**edges; // 二维数组表示的邻接矩阵
public:
Graph(int vertices); // 构造函数初始化邻接矩阵
~Graph(); // 析构函数释放内存
void addEdge(int u, int v, int w); // 添加带权边
void printGraph(); // 打印邻接矩阵
};
// 构造函数实现:初始化邻接矩阵为全-inf
Graph::Graph(int vertices){
this->vertices = vertices;
edges = new int* [vertices]; // 创建动态二维数组
for(int i = 0; i < vertices; ++i){
edges[i] = new int[vertices];
for(int j = 0; j < vertices; ++j){
edges[i][j] = inf; // 初始时所有顶点间无连接[6](@ref)
}
}
}
// 析构函数:释放动态分配的内存
Graph::~Graph(){
for(int i = 0; i < vertices; ++i){
delete[] edges[i]; // 释放每行内存
}
delete[] edges; // 释放指针数组
}
// 添加带权边(u -> v)
void Graph::addEdge(int u, int v, int w){
edges[u][v] = w; // 仅设置单向边,说明是有向图[1,6](@ref)
}
// 打印邻接矩阵
void Graph::printGraph(){
for(int i = 0; i < vertices; ++i){
for(int j = 0; j < vertices; ++j){
cout << edges[i][j] << " "; // 输出矩阵元素
}
cout << endl;
}
}
int main(){
int vertices = 5;
Graph graph(vertices); // 创建5个顶点的图
// 添加带权边(构建有向图)
graph.addEdge(0, 1, 1); // 0→1权值1
graph.addEdge(0, 2, 3); // 0→2权值3
graph.addEdge(1, 2, 2); // 1→2权值2
graph.addEdge(2, 3, 7); // 2→3权值7
graph.addEdge(3, 4, 9); // 3→4权值9
graph.addEdge(4, 0, 4); // 4→0权值4
graph.addEdge(4, 2, 5); // 4→2权值5
graph.printGraph(); // 输出邻接矩阵
return 0;
}作业
必做题:
P5788 √
P1886
P10798(比较难的单调栈题,可以放最后做)
P10470 √ and ×(老师的代码)
P3379(是必做也是选作,必做:算法三选一)
P6121
P3378 √
P1801
P3865 √ and ×(题解代码)
P3374
P3368
选做题:
P6510
P3820
P6008
P11266(慎选,需自学左偏树)
P3379(是必做也是选作,选做:三种算法都打一遍)
P4280