板子

· · 个人记录

离散化

将长为 n 的 a 数组从 min\{a_i\} \backsim max\{a_i\} 变为 1 \backsim n , p_i 表示排在第 i 位的数

#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e6+5;
map<int,int> vis,id;
int n;
int a[MAXN];
int p[N];
int tot;
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=n;i++) vis[a[i]]=1;
    for(auto d:vis) id[d.first]=++tot;
  for(auto d:id) p[d.second]=d.first;
    for(int i=1;i<=n;i++) a[i]=id[a[i]];
    for(int i=1;i<=n;i++) cout<<a[i]<<" ";
    return 0;
}

并查集

基础版:

初始化:

for(int i=1;i<=n;i++) f[i]=i;
int f[MAXN];
int fd(int x) {return x==f[x]?x:fd(f[x]);}
void un(int u,int v) {f[fd(u)]=fd(v);}

优化版(按秩合并):

初始化:

for(int i=1;i<=n;i++) f[i]=i,h[i]=1;
int f[MAXN],h[MAXN];
int fd(int x) {return x==f[x]?x:fd(f[x]);}
void un(int u,int v) 
{
    int fu=fd(u),fv=fd(v); if(fu==fv) return ;
    if(h[fu]!=h[fv]) f[(h[fu]<h[fv])?fu:fv]=(h[fu]>h[fv])?fu:fv;
    else h[f[fv]=fu]++;
}

优化版(路径压缩):

int f[MAXN],h[MAXN];
int fd(int x) {return x==f[x]?x:f[x]=fd(f[x]);}
void un(int u,int v) {f[fd(u)]=fd(v);}

优化版(按秩合并&路径压缩):

int f[MAXN],h[MAXN];
int fd(int x) {return x==f[x]?x:f[x]=fd(f[x]);}
void un(int u,int v) 
{
    int fu=fd(u),fv=fd(v); if(fu==fv) return ;
    if(h[fu]!=h[fv]) f[(h[fu]<h[fv])?fu:fv]=(h[fu]>h[fv])?fu:fv;
    else h[f[fv]=fu]++;
}

带帮派大小的并查集

初始化:

for(int i=1;i<=n;i++) f[i]=i,h[i]=1,si[i]=1;
int f[MAXN],h[MAXN],si[MAXN];
int fd(int x) {return x==f[x]?x:f[x]=fd(f[x]);}
void un(int u,int v) 
{
    int fu=fd(u),fv=fd(v); if(fu==fv) return ;
    if(h[fu]!=h[fv]) {f[(h[fu]<h[fv])?fu:fv]=(h[fu]>h[fv])?fu:fv; si[(h[fu]>h[fv])?fu:fv]+=si[(h[fu]<h[fv])?fu:fv];}
    else { h[f[fv]=fu]++; si[fu]+=si[fv];}
}

随机数

mt19937 rnd(time(NULL)^clock());
long long rad(int x,int y) { return rnd()%(y-x+1)+x; }

Dijkstra

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
const long long inf=0x3f3f3f3f;
int n,m,s;
bool vis[N];
long long dis[N];
struct node
{
    long long to,w;
    node(){}
    node(int vv,int ww)
    {
        to=vv; w=ww;
    }
};
struct dij
{
    long long dis,pos;
    bool operator <(const dij &x)const
    {
        return x.dis<dis;
    }
    dij(){}
    dij(int d,int p) {dis=d;pos=p;} 
};
vector<node> G[N];
void Dijkstra(int s)
{
    memset(dis,inf,sizeof dis);
    priority_queue<dij> q;
    dis[s]=0;
    q.push(dij(0,s));
    while(q.size())
    {
        dij now=q.top();q.pop();
        if(dis[now.pos]<now.dis) continue;
        for(auto v:G[now.pos])
        if(dis[v.to]>dis[now.pos]+v.w)
        {
            dis[v.to]=dis[now.pos]+v.w;
            q.push(dij(dis[v.to],v.to));
        }
    }

}
int main()
{
    cin>>n>>m>>s;
    for(int i=1,u,v,w;i<=m;i++) 
    {
        cin>>u>>v>>w;
        G[u].push_back(node(v,w));
    }
    Dijkstra(s);
    for(int i=1;i<=n;i++) cout<<dis[i]<<" ";
    cout<<endl;
    return 0;
}
void Dijkstra(int s)
{
    memset(dis,inf,sizeof dis);
    priority_queue<dij> q;
    q.push(dij(0,s));
    while(q.size())
    {
        dij now=q.top();q.pop();
        if(vis[now.pos])
        {
            continue;
        }
        dis[now.pos]=now.dis;
        vis[now.pos]=1;
        for(auto v:G[now.pos])
        {
            q.push(dij(now.dis+v.w,v.to));
        }
    }
}

SPFA

#include<bits/stdc++.h>
using namespace std;
const int N=2e3+5;
const int inf=1e16;
int n,m;
long long dis[N];
bool vis[N],c;
int cnt[N];
struct node
{
    int to,w;
    node(){}
    node(int t,int ww) {to=t; w=ww;}
};
vector<node> G[N];
void SPFA(int s)
{
    queue<int> q;
    for(int i=1;i<=n;i++) dis[i]=inf;
    memset(vis,0,sizeof vis);
    memset(cnt,0,sizeof cnt);
    dis[s]=0; vis[s]=1;
    q.push(s);
    while(q.size())
    {
        int u=q.front(); q.pop();
        vis[u]=0;
        for(auto v:G[u])
        if(dis[v.to]>dis[u]+v.w)
        {
            dis[v.to]=dis[u]+v.w;
            cnt[v.to]=cnt[u]+1;
            if(cnt[v.to]>=n)
            {
                c=1;
                return ;
            }
            vis[v.to]=1;
            q.push(v.to);
        }
    }
    return ;
}
void solve()
{
    for(int i=0;i<N;i++) G[i].clear();
    cin>>n>>m;
    for(int i=1,v,u,w;i<=m;i++)
    {
        cin>>u>>v>>w;
        if(w>=0) G[u].push_back(node(v,w)),G[v].push_back(node(u,w));
        else G[u].push_back(node(v,w));

    }
    c=0;
    SPFA(1);
    if(c) cout<<"YES\n";
    else cout<<"NO\n";
}

int main()
{
    int t; cin>>t; while(t--) solve();
}

线段树

静态开点

#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e5+5;
long long sum[MAXN<<2],lz[MAXN<<2];
int n,m;
void up(int i) {sum[i]=sum[i*2]+sum[(i*2)+1]; return;}

void down(int i,int l,int r) 
{
    int mid=(l+r)>>1;
    sum[i*2]+=(mid-l+1)*lz[i];
    sum[(i*2)+1]+=(r-mid)*lz[i];
    lz[i*2]+=lz[i]; lz[(i*2)+1]+=lz[i]; lz[i]=0;
    return;
}

void ins(int i,int l,int r,int L,int R,long long s)
{
    if(L<=l&&r<=R)
    {
        sum[i]+=(r-l+1)*s;
        lz[i]+=s;
        return;
    }
    if(lz[i]) down(i,l,r);
    int mid=(l+r)>>1;
    if(L<=mid) ins(i*2,l,mid,L,R,s);
    if(mid+1<=R) ins(i*2+1,mid+1,r,L,R,s);
    up(i);
    return;
}

long long get(int i,int l,int r,int L,int R)
{
    if(L<=l&&r<=R) return sum[i];
    if(lz[i])  down(i,l,r);
    int mid=(l+r)>>1;
    long long ans=0;
    if(L<=mid) ans+=get(i*2,l,mid,L,R);
    if(mid+1<=R) ans+=get((i*2)+1,mid+1,r,L,R);
    up(i); 
    return ans;
} 

int main()
{
    cin>>n>>m;
    for(long long i=1,x;i<=n;i++)
    {
        cin>>x; 
        ins(1,1,n,i,i,x);
    }
    while(m--)
    {
        long long op,l,r,k;
        cin>>op;
        if(op==1)
        {
            cin>>l>>r>>k;
            ins(1,1,n,l,r,k);
        }else
        {
            cin>>l>>r;
            cout<<get(1,1,n,l,r)<<endl;
        }
    }
    return 0;
} 

组合数初始化

C(n,m)
long long C[MAXN][MAXN];

void init()
{
    C[0][0]=1;
    for(int i=1;i<MAXN;i++)
    for(int j=0;j<=i;j++)
    {
        if(j==0||j==i) C[i][j]=1;
        else C[i][j]=C[i-1][j-1]+C[i-1][j];
    }
    return ;
}