贝塔c2的笔记

· · 个人记录

1.单调队列,例题:滑动窗口 /【模板】单调队列

代码/题解:

#include<bits/stdc++.h>
using namespace std;
int a[11451400];
int main()
{
    int n, k;
    cin >> n >> k;
    for(int i = 1; i <= n; i ++)
    {
        cin >> a[i];
    }
    deque<int> dq;//用双端队列来实现单调队列(找最大值max)
    deque<int> pd;//用双端队列来实现单调队列(找最小值min)
    for(int i = 1; i <= n; i ++)//i表示滑动窗口右端点 
    {
        //队首已经位于滑动窗口左边了,移除 
        if(!pd.empty() && pd.front() + k == i)
        {
            pd.pop_front();
        }
        //以单调(严格)递增队列为例,新入队元素必须大于队尾元素 
        while(!pd.empty() && a[pd.back()] >= a[i])
        {
            pd.pop_back();//否则,删除队尾(递减或严格递减类似) 
        }
        pd.push_back(i);//压入队尾元素 
        if(i >= k)
        {
            //这里处理队首元素,区间最小 
            cout << a[pd.front()] << " ";
        }
    }
    cout << endl;
    for(int i = 1; i <= n; i ++)//i表示滑动窗口右端点 
    {
        //队首已经位于滑动窗口左边了,移除 
        if(!dq.empty() && dq.front() + k == i)
        {
            dq.pop_front();
        }
        //以单调(严格)递减队列为例,新入队元素必须小于队尾元素 
        while(!dq.empty() && a[dq.back()] <= a[i])
        {
            dq.pop_back();//否则,删除队尾(递减或严格递减类似) 
        }
        dq.push_back(i);//压入队尾元素 
        if(i >= k)
        {
            //这里处理队首元素,区间最大
            cout << a[dq.front()] << " ";
        }
    }
    return 0;
}

2.手写队列,例题:扫描

代码/题解

#include<bits/stdc++.h>
using namespace std;
int a[11451400], q[11451400];//手写队列 
int main()
{
    int n, k;
    cin >> n >> k;
    for(int i = 1; i <= n; i ++)
    {
        cin >> a[i];
    }
    int head = 1, tail = 0;///手写队首尾指针 
    for(int i = 1; i <= n; i ++)
    {
        //队首元素已经位于滑动窗口左(外)边了,移除 
        if(head <= tail && q[head] + k == i)
        {
            head ++;
        }
        //以单调(严格)递减队列为例,新入队元素必须小于队尾元素 
        while(head <= tail && a[q[tail]] < a[i])
        {
            tail --;//否则,删除队尾。(递减或严格递增类似) 
        }
        //压入队尾元素 
        q[++ tail] = i;
        //这里处理队首元素,一般队首为区间最大或最小 
        if(i >= k)
        {
            cout << a[q[head]] << endl;
        }
    }
    return 0;
}

3.单调栈,例题:【模板】单调栈

#include <bits/stdc++.h>
using namespace std;
long long a[3000005], z[3000005]; 
int main()
{
    int n;
    cin >> n;
    for(int i = 1; i <= n; i ++)
    {
        cin >> a[i];
    } 
    //递减栈,从栈底向栈顶递减 
    stack<int> stck;
    //这道题目需要反方向搜
    for(int i = n; i >= 1; i --)
    {
        //比目标元素i小的,从栈顶弹出,维护严格单调递增栈 
        while(!stck.empty() && a[stck.top()] <= a[i])
        {
            stck.pop();
        }
        //处理栈顶元素 
        z[i] = stck.empty() ? 0 : stck.top();
        //目标元素下标入栈 
        stck.push(i);
    }
    for(int i = 1; i <= n; i ++)
    {
        cout << z[i] << " ";
    }
    cout << endl;
    return 0;
}

3.ST表:P3865 【模板】ST 表 && RMQ 问题

#include <bits/stdc++.h>
using namespace std;
#define MAXN 100002// 最多1e5个元素 
#define LOGN 30// ceil(log2(MAXN))

int n, m, arr[MAXN];// 存n个数的序列。下标从1开始 
int st[MAXN][LOGN];// st[i][j]表示,从i开始的2^j个数中最大的一个 

// 构建st表,注意+优先于<< 
void build_spqrse_table()
{
    for(int i = 1; (1 << i) <= n; i ++)
    {
        for(int j = 1; j + (1 << i) - 1 <= n; j ++)
        {
            st[j][i] = max(st[j][i - 1], st[j + (1 << (i - 1))][i - 1]);
        }
    }
}
// 查询区间[l,r]的最大值 
int query(int l, int r)
{
    int j = (int)log2(r - l + 1);
    return max(st[l][j], st[r - (1 << j) + 1][j]);
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= n; i ++)
    {
        cin >> st[i][0];
    }
    build_spqrse_table();
    for(int i = 1; i <= m; i ++)
    {
        int l, r;
        cin >> l >> r;
        cout << query(l, r) << '\n';
    }
    return 0;
}

4.拓补排序,例题:B3644 【模板】拓扑排序 / 家谱树

#include <bits/stdc++.h>
using namespace std;
vector<int> adj[101];
int in_degree[101], n;
int topo[101], t;
void toposort()
{
    // 计算所有点的入度
    for (int i = 1; i <= n; i++)
    {
        for (int neighbor : adj[i])
        {
            in_degree[neighbor]++;
        }
    }
    // 初始化队列,将所有入度为0的点入队
    queue<int> q;
    for (int i = 1; i <= n; i++)
    {
        if (in_degree[i] == 0)
        {
            q.push(i);
        }
    }
    // 拓扑排序过程
    int t = 0; // 拓扑序列的索引
    while (!q.empty()) {
        int u = q.front();
        q.pop(); // 弹出队首元素
        topo[++t] = u; // 将当前点添加到拓扑序列尾部

        // 遍历当前点的所有邻居
        for (int v : adj[u]) {
            in_degree[v]--; // 邻居的入度减1
            if (in_degree[v] == 0) {
                q.push(v); // 如果邻居的入度为0,则入队
            }
        }
    }
}
int main()
{
    cin >> n;
    for(int i = 1; i <= n; i ++)
    {
        int x;
        while(cin >> x)
        {
            if(x == 0)
            {
                break;
            }
            adj[i].push_back(x);
        }
    }
    toposort();
    for(int i = 1; i <= n; i ++)
    {
        cout << topo[i] << " ";
    }
    cout << endl;
    return 0;
}

5.欧拉路径,例题:P1333 瑞瑞的木棍

#include <bits/stdc++.h>
using namespace std;
#define MAXN 500002
vector<int> adj[MAXN]; // 邻接表
int n, degree[MAXN]; // 记录度
bool visited[MAXN];
// 判断连通性
void dfs(int u)
{
    if (visited[u])
    {
        return;
    }
    visited[u] = true;
    for (auto v : adj[u])
    {
        dfs(v);
    }
}
int main()
{
    map<string, int> color2int;
    string str1, str2;
    while (cin >> str1 >> str2)
    {
        if (color2int.find(str1) == color2int.end())
        {
            color2int[str1] = color2int.size() + 1;
        }
        if (color2int.find(str2) == color2int.end())
        {
            color2int[str2] = color2int.size() + 1;
        }
        int u = color2int[str1], v = color2int[str2];
        adj[u].push_back(v);
        adj[v].push_back(u);
        degree[u]++;
        degree[v]++;
    }
    n = color2int.size();
    dfs(1);
    int cnt = 0;
    for (int i = 1; i <= n; i++)
    {
        if (visited[i])
        {
            cnt++;
        }
    }
    // 检查连通性
    if (cnt != n)
    {
        cout << "Impossible" << endl;
        return 0;
    }
    // 统计奇数度节点数量
    int odd_cnt = 0;
    for (int i = 1; i <= n; i++)
    {
        if (degree[i] % 2 != 0)
        {
            odd_cnt++;
        }
    }
    // 欧拉路径条件:0个或2个奇数度节点
    if (odd_cnt != 0 && odd_cnt != 2)
    {
        cout << "Impossible" << endl;
    }
    else
    {
        cout << "Possible" << endl;
    }
    return 0;
}

6.线段树:P3372 【模板】线段树 1

#include <iostream>
#include <cmath>
using namespace std;
#define MAXN 400002
#define lson (root*2)
#define rson (root*2+1)

long long sgmt[MAXN], lazy_add[MAXN];

void push_up(int root)
{
    sgmt[root] = sgmt[lson] + sgmt[rson];
}

void push_down(int root, int len)
{
    if (lazy_add[root] == 0)
    {
        return;
    }
    lazy_add[lson] += lazy_add[root];
    lazy_add[rson] += lazy_add[root];
    sgmt[lson] += (len / 2) * lazy_add[root];
    sgmt[rson] += (len / 2) * lazy_add[root];
    lazy_add[root] = 0;
}

void update(int a, int b, long long c, int l, int r, int root)
{
    if (a <= l && r <= b)
    {
        sgmt[root] += (r - l + 1) * c;
        lazy_add[root] += c;
        return;
    }
    push_down(root, r - l + 1);
    int mid = (l + r) / 2;
    if (a <= mid)
    {
        update(a, b, c, l, mid, lson);
    }
    if (b > mid)
    {
        update(a, b, c, mid + 1, r, rson);
    }
    push_up(root);
}

long long query(int a, int b, int l, int r, int root)
{
    if (a <= l && r <= b)
    {
        return sgmt[root];
    }
    push_down(root, r - l + 1);

    long long ans = 0;
    int mid = (l + r) / 2;
    if (a <= mid)
    {
        ans += query(a, b, l, mid, lson);
    }
    if (b > mid)
    {
        ans += query(a, b, mid + 1, r, rson);
    }
    return ans;
}

void build(int l, int r, int root)
{
    if (l == r)
    {
        return;
    }
    int mid = (l + r) / 2;
    build(l, mid, lson);
    build(mid + 1, r, rson);
    push_up(root);
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n, m;
    cin >> n >> m;
    int npot = 1 << (int)ceil(log2(n));
    for (int i = 0; i < n; i++)
    {
        cin >> sgmt[npot + i];
    }
    n = npot;

    int l = 1, r = n, root = 1;
    build(l, r, root);

    while (m--)
    {
        int op, x, y;
        cin >> op >> x >> y;
        if (op == 1)
        {
            long long val;
            cin >> val;
            update(x, y, val, l, r, root);
        }
        else
        {
            cout << query(x, y, l, r, root) << endl;
        }
    }
    return 0;
}