跳到正文

个人记录

模板(不完整)

yangziqiao约 4 分钟
原文
文章信息与版本

文章信息

发布
2026年5月16日
收录
2026年5月17日
更新
2026年11月11日
篇幅
约 4 分钟

版本

  1. v2当前
  2. v1

cc++框架

C++
#include<bits/stdc++.h>
using namespace std;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    return 0;
}

跳出循环

C++
continue;

结构体

C++
struct node{
    //结构体
}

栈

C++
stack<unsigned long long>st;

ST表模版

C++
#include<bits/stdc++.h>
using namespace std;
int a[100010];
int n,m;
int st[100010][20];
int lg[100010];
void init()
{
    for(int i=1;i<=n;i++)st[i][0]=a[i];
    for(int j=1;(1<<j)<=n;j++)
    {
        for(int i=1;i+(1<<(j-1)-1+1)<=n;i++)
        {
            st[i][j]=max(st[i][j-1],st[i+(1<<(j-1))-1+1][j-1]);
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    cin>>n>>m;
    for(int i=1;i<=n;i++)cin>>a[i];
    init();
    lg[0]=-1;
    for(int i=1;i<=n;i++)lg[i]=lg[i>>1]+1;
    while(m--)
    {
        int l,r;
        cin>>l>>r;
        int x=lg[r-l+1];
        cout<<max(st[l][x],st[r-(1<<x)+1][x])<<'\n';
    }
    return 0;
}

拓扑排序模板

C++
#include<bits/stdc++.h>
using namespace std;
vector<long long>a[1000010];
long long vis[1000010],vis1[10000010];
queue<int>q;
long long n;
void bfs(){
    for(int i=1;i<=n;i++)
    {
        if(vis1[i]==0)q.push(i);
    }
    
    while(q.size())
    {
        int p=q.front();
        cout<<p<<' ';
        q.pop();
        for(int i=0;i<a[p].size();i++)
        {
            vis1[a[p][i]]--;
            
            if(vis1[a[p][i]]==0)
            {
                q.push(a[p][i]);
                
            }
        }
        
    }
    
    
    return;
}
int main(){
    
    cin>>n;
    
    
    for(long long i=1;i<=n;i++)
    {
        long long x;
        
        while(cin>>x)
        {
            if(x==0)
            {
                break;
            }
            
            a[i].push_back(x);
            vis1[x]++;
        }
        
    }
    
    bfs();
    return 0;
}
原文