模板
巨大多宏定义
点击查看代码
#include <bits/stdc++.h>
#define foe(i, now) for (int i = head[now]; ~i; i = edg[i].nxt)
#define fo(i, g1, g2) for (int i = (g1), __Endi__ = (g2); i <= __Endi__; ++i)
#define fd(i, g1, g2) for (int i = (g1), __Endi__ = (g2); i >= __Endi__; --i)
#define go(i, now) for (auto &i : edg[now])
#define mem(g1, g2) memset((g1), g2, sizeof((g1)))
#define lowbit(now) ((now) & (-now))
#define lson ((now) << 1)
#define mid ((l + r) >> 1)
#define rson (lson | 1)
#define ll long long
#define pb emplace_back
#define mp(g1, g2) make_pair((g1), (g2))
#define PII pair<int, int>
#define pow2(g2) (1ll << (g2))
#define II inline
#define INF (0x3f3f3f3f)
#define ps push
#define lsond lson, l, mid
#define rsond rson, mid + 1, r
IO优化
不支持double,long double,float等浮点类型,不支持 std::string。但支持 char及数组读入
请勿在交互题中使用。
点击查看代码
namespace Fread
{
const int SIZE = (1 << 26) + 1; // namespace Fread
char buf[SIZE], *S, *T;
}
namespace Fwrite
{
const int SIZE = (1 << 26) + 1; // namespace Fwrite
char buf[SIZE], *S = buf, *T = buf + SIZE;
inline void flush()
{
fwrite(buf, 1, S - buf, stdout),S = buf;
}
struct NTR {
~ NTR() {
flush();
}
}
ztr;
}
#ifdef ONLINE_JUDGE
#define getchar() (Fread::S==Fread::T&&(Fread::T=(Fread::S=Fread::buf)+fread(Fread::buf,1,1<<23,stdin),Fread::S==Fread::T) ? '\n' : *Fread::S++)
#define putchar(c) ( *Fwrite::S++ = c,( Fwrite::S != Fwrite::T)?0:( Fwrite::flush(),0))
#endif
namespace Fastio {
struct Reader {
template<typename T>inline Reader &operator >> (T &now) {
static char c;
static short f;
c = getchar(), f = 1;
while (!isdigit(c)) {
c^'-' ? c = getchar() : (f = -1, c = getchar());
}
now = 0;
while (isdigit(c))
{
now = now * 10 + (c ^ '0'), c = getchar();
}
return now *= f,*this;
}
inline Reader &operator >> (char &c)
{
c = getchar();
while (c == '\n' || c == ' ')
c = getchar();
return *this;
}
inline Reader &operator >> (char *str) {
static int len = 0;
len = 0;
static char c;
c = getchar();
while (c == '\n' || c == ' ')
c = getchar();
while (c ^ '\n' && c ^ ' ') {
str[len++] = c, c = getchar();
}
return str[len] = '\0',*this;
}
Reader() {}
}cin;
const char endl = '\n';
struct Writer {
template<typename T>inline Writer &operator << (T now)
{
static int top = 0;
now ? (now<0 ? putchar('-'), now = -now,top = 0 : top = 0) : (putchar('0') ,top=0);
static int sta[45];
while (now)
{
sta[++top] = now % 10, now /= 10;
}
while (top)
{
putchar(sta[top] | 48), --top;
}
return *this;
}
inline Writer &operator << (const char c) {
return putchar(c),*this;
}
inline Writer &operator << (const char *str) {
static int cur = 0;
cur = 0;
while (str[cur]) putchar(str[cur++]);
return *this;
}
inline Writer &operator << (char *str)
{
static int cur = 0;
cur = 0;
while (str[cur]) putchar(str[cur++]);
return *this;
}
Writer() {}
}
cout;
}
#define cin Fastio :: cin
#define cout Fastio :: cout
#define endl Fastio :: endl
冷门函数
点击查看代码
namespace Mystd {
const int mode=998244353;
inline unsigned highbit(const unsigned now) {return (1u<<(31-__builtin_clz(now)));}
inline long long qpow(ll a,ll b){ll ans=1;for (;b;b>>=1){ if (b&1) ans=ans*a;a=a*a;}return ans;}
inline long long modqpow(ll a,ll b,const int mode1 = mode){ll ans=1;for (;b;b>>=1){if (b&1) ans=ans*a%mode1;a=a*a%mode1;}return ans;}
template<typename _Tp>inline _Tp Max(const _Tp g1, const _Tp g2) {return g1 < g2 ? g2 : g1;}
template<typename _Tp, typename ...Args>inline _Tp Max(const _Tp g1, const _Tp g2, const Args... others) {return Max(Max(g1, g2), others...);}
template<typename _Tp>inline _Tp Min(const _Tp g1, const _Tp g2) {return g1 < g2 ? g1 : g2;}
template<typename _Tp, typename ...Args>inline _Tp Min(const _Tp g1, const _Tp g2, const Args... others) {return Min(Min(g1, g2), others...);}
inline void add(int &a,const int b) {((a+=b)>=mode) && (a-=mode);}
inline void sub(int &a,const int b) {((a-=b)<0) && (a+=mode);}
inline constexpr int add1(const int a,const int b) {return (a+b>=mode ? a+b-mode : a+b);}
inline constexpr int sub1(const int a,const int b) {return (a-b<0 ? a-b+mode : a-b);}
inline constexpr int mul(int a,int b) {return 1ll*a*b%mode;}
int fac[MAXN + 5],ifac[MAXN + 5];
inline void init(int lim = MAXN){fac[0] = 1;fo (i,1,lim) fac[i]=mul(fac[i-1],i);ifac[lim] = modqpow(fac[lim],mode-2);fd (i,lim-1,0) ifac[i]=mul(ifac[i+1],i+1);}
inline int comb(int n,int k){if (n<k) return 0;return mul(fac[n],mul(ifac[k],ifac[n-k]));}
}
网络最大流
点击查看代码
namespace Flow
{
const int maxm = 2e6 + 10;
struct Edge
{
int v;
int w;
Edge (int _v = 0,int _w = 0) : v(_v),w(_w){}
}edg[maxm];
int cntedge = 1,maxnode = 0;
int head[maxn],nxt[maxm],cur[maxn],d[maxn];
void Flowinit(bool qaq = false)
{
cntedge = 1;
if (qaq)
{
memset(head,-1,sizeof(int)*(maxnode+2));
// memset(head,-1,sizeof(int)*(maxnode+2));
// mem(nxt,-1),mem(head,-1),mem(edg,0);
}
}
inline void addedge(int u,int v,int w)
{
edg[++cntedge]=Edge(v,w),nxt[cntedge]=head[u],head[u]=cntedge;
edg[++cntedge]=Edge(u,0),nxt[cntedge]=head[v],head[v]=cntedge;
}
inline bool bfs(int s,int t)
{
//mem(cur,-1);
fo (i,0,maxnode){cur[i]=head[i],d[i]=0;}
queue <int> q;
q.ps(s);
d[s]=1;
while(!q.empty())
{
int now=q.front();q.pop();
for (int i=head[now];~i;i=nxt[i])
{
const int &v=edg[i].v,&w=edg[i].w;
if (!d[v] && w>0)
{
q.push(v);
d[v]=d[now]+1;
if (v==t) return 1;
}
}
}
return 0;
}
inline int dfs(int now,int w,const int &t)
{
if (now==t) return w;
int sum=0;
for (int &i=cur[now];~i;i=nxt[i])
{
const int &v=edg[i].v,&val=edg[i].w;
if (val>0 && d[now]+1==d[v])
{
const int cost=dfs(v,min(w-sum,val),t);
(cost)?edg[i].w-=cost,
edg[i^1].w+=cost,
sum+=cost:d[v]=0;
if (sum==w) return sum;
}
}
return sum;
}
inline int dinic(int s,int t,int _ma = -1)
{
if (_ma==-1) maxnode=t;else maxnode=_ma;
int ans=0,qaq=0;
while(bfs(s,t))
{
while ((qaq=dfs(s,INF,t)))
{
ans+=qaq;
}
}
return ans;
}
}
using namespace Flow;
最小费用最大流
点击查看代码
namespace mcmf
{
const int maxm = 2e6 + 10;
int head[maxn],nxt[maxm],cur[maxn],maxnode,edgcnt;
int dis[maxn];
struct Edge
{
int v;
int w;
int c;
Edge(int _v = 0,int _w = 0,int _c = 0):v(_v),w(_w),c(_c){}
}edg[maxm];
inline void init(bool flag=false)
{
edgcnt=1;
if (flag) memset(head,-1,sizeof(int)*(maxnode+2));
}
inline void addedge(int u,int v,int w,int c)
{
edg[++edgcnt]=Edge(v,w,c),nxt[edgcnt]=head[u],head[u]=edgcnt;
edg[++edgcnt]=Edge(u,0,-c),nxt[edgcnt]=head[v],head[v]=edgcnt;
}
bitset <maxn> vis;
inline bool spfa(int s,int t)
{
queue <int> q;
fo (i,0,maxnode) cur[i]=head[i],dis[i]=INF;
q.ps(s);
dis[s]=0;
while(!q.empty())
{
const int now=q.front();
q.pop();
vis.reset(now);
for (int i=head[now];~i;i=nxt[i])
{
const int &v=edg[i].v;
if (edg[i].w && dis[now]+edg[i].c<dis[v])
{
dis[v]=dis[now]+edg[i].c;
if (!vis[v]) q.push(v),vis.set(v);
}
}
}
//exit(0);
return dis[t]!=INF;
}
inline pair<int,int> dfs(int now,const int flow,const int &t)
{
if (now==t) return mp(flow,0);
vis.set(now);
int ans=0,cost=0;
for (int &i=cur[now];~i;i=nxt[i])
{
const int &v=edg[i].v,&w=edg[i].w,&c=edg[i].c;
if (!vis[v] && w && dis[v]==dis[now]+c)
{
pair<int,int> qaqq = dfs(v,min(w,flow-ans),t);
(qaqq.first) ?
ans+=qaqq.first ,
cost += qaqq.second + c*qaqq.first ,
edg[i].w-=qaqq.first ,
edg[i^1].w+=qaqq.first :dis[v]=INF;
if (ans==flow)
{
vis.reset(now);
return mp(ans,cost);
}
}
}
vis.reset(now);
return mp(ans,cost);
}
inline pair<int,int> mcmf(int s,int t,int flag = -1)
{
if (flag==-1) maxnode=t;else maxnode=flag;
pair <int,int> ans;
while(spfa(s,t))
{
pair<int,int> a;
while(a=dfs(s,INF,t),a.first) ans.first+=a.first,ans.second+=a.second;
}
return ans;
}
}
懒得封装(带负圈的有源汇最小费用最大流)
点击查看代码
const int maxn = 5e4 +10;
int n,m;
namespace mcmf
{
const int maxm = 2e6 + 10;
int head[maxn],nxt[maxm],cur[maxn],maxnode,edgcnt;
int dis[maxn];
struct Edge
{
int v;
int w;
int c;
Edge(int _v = 0,int _w = 0,int _c = 0):v(_v),w(_w),c(_c){}
}edg[maxm];
inline void init(bool flag=false)
{
edgcnt=1;
if (flag) memset(head,-1,sizeof(int)*(maxnode+2));
}
inline void addedge(int u,int v,int w,int c)
{
edg[++edgcnt]=Edge(v,w,c),nxt[edgcnt]=head[u],head[u]=edgcnt;
edg[++edgcnt]=Edge(u,0,-c),nxt[edgcnt]=head[v],head[v]=edgcnt;
}
bitset <maxn> vis;
inline bool spfa(int s,int t)
{
queue <int> q;
fo (i,0,maxnode) cur[i]=head[i],dis[i]=INF;
q.ps(s);
dis[s]=0;
while(!q.empty())
{
const int now=q.front();
q.pop();
vis.reset(now);
for (int i=head[now];~i;i=nxt[i])
{
const int &v=edg[i].v;
if (edg[i].w && dis[now]+edg[i].c<dis[v])
{
dis[v]=dis[now]+edg[i].c;
if (!vis[v]) q.push(v),vis.set(v);
}
}
}
//exit(0);
return dis[t]!=INF;
}
inline pair<int,int> dfs(int now,const int flow,const int &t)
{
if (now==t) return mp(flow,0);
vis.set(now);
int ans=0,cost=0;
for (int &i=cur[now];~i;i=nxt[i])
{
const int &v=edg[i].v,&w=edg[i].w,&c=edg[i].c;
if (!vis[v] && w && dis[v]==dis[now]+c)
{
pair<int,int> qaqq = dfs(v,min(w,flow-ans),t);
(qaqq.first) ?
ans+=qaqq.first ,
cost += qaqq.second + c*qaqq.first ,
edg[i].w-=qaqq.first ,
edg[i^1].w+=qaqq.first :dis[v]=INF;
if (ans==flow)
{
vis.reset(now);
return mp(ans,cost);
}
}
}
vis.reset(now);
return mp(ans,cost);
}
inline pair<int,int> mcmf(int s,int t,int flag = -1)
{
if (flag==-1) maxnode=t;else maxnode=flag;
pair <int,int> ans;
// return ans;
while(spfa(s,t))
{
pair<int,int> a;
while(a=dfs(s,INF,t),a.first) ans.first+=a.first,ans.second+=a.second;
// exit(0);
}
return ans;
}
}
int sum[maxn];
ll ansc=0;
signed main()
{
#ifndef ONLINE_JUDGE
freopen("txt.in", "r", stdin);
freopen("txt.out", "w", stdout);
#endif
mem(mcmf::head,-1);
int s, t;
mcmf::init();
cin >> n >> m >> s >> t;
while (m--)
{
int u, v, w, c;
cin >> u >> v >> w >> c;
if (c>=0)
{
mcmf::addedge(u, v, w, c);
}else
{
sum[u]-=w,sum[v]+=w;
ansc += c*w;
mcmf::addedge(v, u, w, -c);
}
}
int S=n+1,T=n+2;
fo (i,1,n)
{
if (sum[i]>0)
{
mcmf::addedge(S,i,sum[i],0);
}else
{
mcmf::addedge(i,T,-sum[i],0);
}
}
mcmf::addedge(t,s,INF,0);
int orz=mcmf::edgcnt;
pair<int,int> ans1 = mcmf::mcmf(S,T,n+2);
int anss=mcmf::edg[orz].w;
mcmf::edg[orz].w=0,mcmf::edg[orz^1].w=0;
pair<int,int> ans2 = mcmf::mcmf(s,t,n+2);
cout<<anss+ans2.first<<" "<<ansc+ans2.second+ans1.second;
return 0;
}
ST表
点击查看代码
struct ST
{
int maxx[MAXN][25];
int minn[MAXN][25];
int lg2[MAXN];
void init()
{
fo (i,1,n) maxx[i][0]=minn[i][0]=a[i];
lg2[0] = -1;
fo (i,1,n) lg2[i] = lg2[i>>1] + 1;
fo (i,1,lg2[n])
fo (j,1,n-(1<<i)+1)
{
maxx[j][i] = max(maxx[j][i-1],maxx[j+(1<<(i-1))][i-1]);
minn[j][i] = min(minn[j][i-1],minn[j+(1<<(i-1))][i-1]);
}
}
inline int querymax(const int l,const int r)
const{
return max(maxx[l][lg2[(r-l+1)]],maxx[r-(1<<lg2[(r-l+1)])+1][lg2[(r-l+1)]]);
}
inline int querymin(const int l,const int r)
const{
return min(minn[l][lg2[(r-l+1)]],minn[r-(1<<lg2[(r-l+1)])+1][lg2[(r-l+1)]]);
}
}st;
打表科技
点击查看代码
ll fp(ll a,ll k){
ll ans=1;
for(;k;k>>=1,a=a*a%mod)
if(k&1) ans=a*ans%mod;
return ans;
}
void BM(ll *a,int n,vector<ll>&ans){
ans.clear();
vector<ll> lst;
int w=0;ll delta=0;
for(int i=1;i<=n;i++){
ll tmp=0;
for(int j=0;j<ans.size();j++)
tmp=(tmp+a[i-1-j]*ans[j])%mod;
if((a[i]-tmp)%mod==0) continue;
if(!w){
w=i;delta=a[i]-tmp;
for(int j=i;j;j--) ans.push_back(0);
continue;
}
vector<ll> now=ans;
ll mul=(a[i]-tmp)*fp(delta,mod-2)%mod;
if(ans.size()<lst.size()+i-w) ans.resize(lst.size()+i-w);
ans[i-w-1]=(ans[i-w-1]+mul)%mod;
for(int j=0;j<lst.size();j++)
ans[i-w+j]=(ans[i-w+j]-mul*lst[j])%mod;
if(now.size()-i<lst.size()-w)
{
lst=now;
w=i;
delta=a[i]-tmp;
}
}
}
ll calc(int m,vector<ll>&coef,ll*h){
if(m<=coef.size()) return h[m];
int k=coef.size();
static ll f[N],g[N],res[N],p[N];
p[0]=-1;
for(int i=1;i<=k;i++) p[i]=coef[i-1];
for(int i=0;i<=2*k;i++) f[i]=g[i]=0;
f[0]=1;
if(k>1) g[1]=1;
else g[0]=p[0];
auto mul = [&](ll *a,ll *b,ll *c)
{
for(int i=0;i<=2*k;i++) res[i]=0;
for(int i=0;i<k;i++)
for(int j=0;j<k;j++)
res[i+j]=(res[i+j]+a[i]*b[j])%mod;
for(int i=2*k;i>=k;i--)
if(res[i]%mod)
for(int j=k;~j;j--)
res[i-j]=(res[i-j]+res[i]*p[j])%mod;
for(int i=0;i<2*k;i++) c[i]=res[i];
return 0;
};
for(;m;m>>=1,mul(g,g,g))
if(m&1) mul(f,g,f);
ll ans=0;
for(int i=0;i<k;i++)
ans=(ans+h[i+1]*f[i])%mod;
return ans;
}
点击查看代码
template<typename T,const T mode>
class modint
{
private:
T x;
public:
modint(int o=0){x=o;}
modint &operator = (int o){return x=o,*this;}
modint &operator +=(modint o){return x=x+o.x>=mode?x+o.x-mode:x+o.x,*this;}
modint &operator -=(modint o){return x=x-o.x<0?x-o.x+mode:x-o.x,*this;}
modint &operator *=(modint o){return x=1ll*x*o.x%mode,*this;}
modint &operator ^=(int b){modint a=*this,c=1;for(;b;b>>=1,a*=a)if(b&1)c*=a;return x=c.x,*this;}
modint &operator /=(modint o){return *this *=o^=mode-2;}
modint &operator +=(int o){return x=x+o>=mode?x+o-mode:x+o,*this;}
modint &operator -=(int o){return x=x-o<0?x-o+mode:x-o,*this;}
modint &operator *=(int o){return x=1ll*x*o%mode,*this;}
modint &operator /=(int o){return *this *= ((modint(o))^=mode-2);}
template<class I>friend modint operator +(modint a,I b){return a+=b;}
template<class I>friend modint operator -(modint a,I b){return a-=b;}
template<class I>friend modint operator *(modint a,I b){return a*=b;}
template<class I>friend modint operator /(modint a,I b){return a/=b;}
template<class I>friend bool operator <(modint a,I b) {return a.x<b;}
friend modint operator ^(modint a,int b){return a^=b;}
friend bool operator ==(modint a,int b){return a.x==b;}
friend bool operator !=(modint a,int b){return a.x!=b;}
bool operator ! () {return !x;}
modint operator - () {return x?mode-x:0;}
bool operator <(const modint&b)const{return x<b.x;}
operator bool() {return x;}
operator int() {return x;}
};
typedef modint<int,998244353> mint1;
typedef modint<int,1000000007> mint2;