模版

· · 算法·理论

c++模版

基础

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);

    return 0;
}

快读快写

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using ull=unsigned long long;
using lll=__int128;
namespace FastIO {
    const int SZ=1<<20;
    char ib[SZ], *ip=ib, *ie=ib;
    char ob[SZ], *op=ob;
    void rf() {ie=ib+fread(ib,1,SZ,stdin);ip=ib;}
    char gc() {if (ip==ie)rf();return (ip<ie)?*ip++:EOF;}
    void read(int &x) {int a=0,b=1;char c=gc();while (c<'0'||c>'9') {if (c=='-')b=-1;c=gc();}while (c>='0'&&c<='9') {a=a*10+(c^48);c=gc();}x=a*b;}
    void read(ll &x) {ll a=0,b=1;char c=gc();while (c<'0'||c>'9') {if (c=='-')b=-1;c=gc();}while (c>='0'&&c<='9') {a=a*10ll+(c^48);c=gc();}x=a*b;}
    void read(string &s) {s.clear();char c=gc();while (c==' '||c=='\n'||c=='\r'||c=='\t')c=gc();while (c!=' '&&c!='\n'&&c!='\r'&&c!='\t'&&c!=EOF) {s+=c;c=gc();}}
    void pc(char c) {if (op==ob+SZ) {fwrite(ob,1,SZ,stdout);op=ob;}*op++=c;}void print_u(unsigned int x) {if (x>9)print_u(x/10);pc(x%10+'0');}
    void print_u(unsigned long long x) {if (x>9)print_u(x/10);pc(x%10+'0');}void print(int x) {if (x<0) {pc('-');print_u(-static_cast<unsigned int>(x));} else print_u(static_cast<unsigned int>(x));}
    void print(ll x) {if (x<0) {pc('-');print_u(-static_cast<unsigned long long>(x));} else print_u(static_cast<unsigned long long>(x));}
    void print(const string &s) {for (char c:s)pc(c);}
    void flush() {fwrite(ob,1,op-ob,stdout);op=ob;}
}
namespace IOS {
    void read(int &x) {cin>>x;}
    void read(ll &x) {cin>>x;}
    void read(string &s){cin>>s;}
    void pc(char c) {cout<<c;}
    void print(int x) {cout<<x;}
    void print(ll x) {cout<<x;}
    void print(string x) {cout<<x;}
    void flush() {}
}
#ifdef ONLINE_JUDGE
    using namespace FastIO;
#else
    using namespace IOS;
#endif
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);

    flush();
    return 0;
}

随机

random_device rd;
mt19937 gen(rd());
ll get_random(ll l,ll r){
    uniform_int_distribution<ll> distrib(l,r);
    ll dic=distrib(gen);
    return dic;
}

线段树

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int maxN=1<<18;
int n;
ll st[maxN],tg[maxN],sz[maxN];
void pushup(int u) {
    if (!u) return;
    st[u]=st[u<<1]+st[u<<1|1];
}
void upd(int u,ll val) {
    st[u]+=sz[u]*val;
    tg[u]+=val;
}
void pushdown(int u) {
    if (tg[u]) {
        upd(u<<1,tg[u]);
        upd(u<<1|1,tg[u]);
        tg[u]=0;
    }
}
void modify(int l,int r,ll val) {
    l+=n,r+=n+1;
    for (int i=__lg(n)+1;i;i--) pushdown(l>>i),pushdown(r>>i);
    for (int u=0,v=0;l<r;l>>=1,r>>=1) {
        if (l&1) u=l,upd(l++,val);
        if (r&1) v=r,upd(--r,val);
        do pushup(u>>=1);
        while (l==r&&u);
        do pushup(v>>=1);
        while (l==r&&v);
    }
}
ll query(int l,int r) {
    l+=n,r+=n+1;
    for (int i=__lg(n)+1;i;i--) pushdown(l>>i),pushdown(r>>i);
    ll res=0;
    for (;l<r;l>>=1,r>>=1) {
        if (l&1) res+=st[l++];
        if (r&1) res+=st[--r];
    }
    return res;
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>n;
    for (int i=1;i<=n;i++) {
        cin>>st[i+n];
        sz[i+n]=1;
    }
    for (int i=n-1;i;i--) {
        st[i]=st[i<<1]+st[i<<1|1];
        sz[i]=sz[i<<1]+sz[i<<1|1];
    }

    return 0;
}

数学类

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const ll maxV=1<<20;
const ll mod=998244353;
ll fact[maxV],inv_fact[maxV];
ll qpow(ll x,ll alpha) {
    x%=mod;
    ll res=1;
    for (;alpha;alpha>>=1,x=(x*x)%mod) if (alpha&1) res=(x*res)%mod;
    return res;
}
ll inv(ll x) {
    return qpow(x,mod-2);
}
void init_f() {
    fact[0]=1;
    for (ll i=1;i<maxV;i++)
        fact[i]=fact[i-1]*i%mod;
    inv_fact[maxV-1]=inv(fact[maxV-1]);
    for (int i=maxV-2;i>=0;i--)
        inv_fact[i]=inv_fact[i+1]*(i+1)%mod;
}
ll fac(ll x) {
    return fact[x];
}
ll inv_fac(ll x) {
    return inv_fact[x];
}
ll A(ll x,ll y) {
    if (y<0||y>x) return 0;
    return fac(x)*inv_fac(x-y)%mod;
}
ll C(ll x,ll y) {
    if (y<0||y>x) return 0;
    return fac(x)*inv_fac(y)%mod*inv_fac(x-y)%mod;
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    init_f();

    return 0;
}

++RP