最近没事想写个缺省源玩玩
包含部分有用的代码封装
Code
#include<bits/stdc++.h>
using namespace std;
//基本运算优化
namespace Basic_operator {
#define P 998244353
template<class T> T operator^(const T& a,const T& b) {
T res=0;
a%=P;
while(b) {
b&1 && (res=1ll*res*a%P);
b>>=1;
a=a*a%P;
}
return res;
}
template<class T> T operator^=(const T& a,const T& b) {
T res=0;
a%=P;
while(b) {
b&1 && (res=1ll*res*a%P);
b>>=1;
a=a*a%P;
}
a=res;
}
template<class T> T operator+(const T& a,const T& b) {
return (1ll*a%P+b%P)%P;
}
template<class T> T operator-(const T& a,const T& b) {
return (1ll*a%P-b%P+P)%P;
}
template<class T> T operator*(const T& a,const T& b) {
return (1ll*a%P)*(b%P)%P;
}
template<class T> T operator/(const T& a,const T& b) {
return (1ll*a%P)*(((1ll*b%P)^(P-2))%P)%P;
}
template<class T> T operator+=(T& a,const T& b) {
a=(1ll*a%P+b%P)%P;
}
template<class T> T operator-=(T& a,const T& b) {
a=(1ll*a%P-b%P+P)%P;
}
template<class T> T operator*=(T& a,const T& b) {
a=(1ll*a%P)*(b%P)%P;
}
template<class T> T operator/=(T& a,const T& b) {
a=(1ll*a%P)*(((1ll*b%P)^(P-2))%P)%P;
}
}
//高精度模板
namespace huge {
struct int128 {
#define N 1005
int p[N];
int128() {
memset(p,0,sizeof p);
}
void print() {
for(int i=p[0]; i>=1; i--)putchar(p[i]+48);
putchar('\n');
return;
}
friend int128 operator * (int128 x,int y) {
x.p[0]+=10;
for(int i=1; i<=x.p[0]; i++)x.p[i]*=y;
for(int i=1; i<=x.p[0]; i++)x.p[i+1]+=x.p[i]/10,x.p[i]%=10;
while(!x.p[x.p[0]]&&x.p[0]>1)x.p[0]--;
return x;
}
friend int128 operator / (int128 x,int y) {
for(int i=x.p[0]; i>=1; i--) {
x.p[i-1]+=x.p[i]%y*10;
x.p[i]/=y;
}
while(!x.p[x.p[0]]&&x.p[0]>1)x.p[0]--;
return x;
}
friend int128 operator + (int128 x,int128 y) {
if(x.p[0]<y.p[0])x.p[0]=y.p[0];
x.p[0]++;
for(int i=1; i<=x.p[0]; i++)x.p[i]+=y.p[i],x.p[i+1]+=x.p[i]/10,x.p[i]%=10;
while(!x.p[x.p[0]]&&x.p[0]>1)x.p[0]--;
return x;
}
friend int128 operator - (int128 x,int128 y) {
if(x.p[0]<y.p[0])x.p[0]=y.p[0];
for(int i=1; i<=x.p[0]; i++)x.p[i]-=y.p[i];
for(int i=1; i<=x.p[0]; i++)if(x.p[i]<0)x.p[i+1]--,x.p[i]+=10;
while(!x.p[x.p[0]]&&x.p[0]>1)x.p[0]--;
return x;
}
};
}
//快速 I/O
//namespace FastIO {
namespace IN {
// char buf[100],*q1=buf,*q2=buf;
// #define getchar() (q1==q2&&(q1=(q2=buf)+fread(buf,1,100,stdin),q1==q2)?EOF:*q1++)
inline void read(short &x) {
x=0;
bool flag=false;
char ch=getchar();
while(ch<'0'||ch>'9')ch=='-' && (flag=true), ch=getchar();
while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+(ch^48), ch=getchar();
flag && (x=-x);
}
inline void read(int &x) {
x=0;
bool flag=false;
char ch=getchar();
while(ch<'0'||ch>'9')ch=='-' && (flag=true), ch=getchar();
while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+(ch^48), ch=getchar();
flag && (x=-x);
}
inline void read(long long &x) {
x=0;
bool flag=false;
char ch=getchar();
while(ch<'0'||ch>'9')ch=='-' && (flag=true), ch=getchar();
while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+(ch^48), ch=getchar();
flag && (x=-x);
}
inline void read(__int128 &x) {
x=0;
bool flag=false;
char ch=getchar();
while(ch<'0'||ch>'9')ch=='-' && (flag=true), ch=getchar();
while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+(ch^48), ch=getchar();
flag && (x=-x);
}
inline void read(unsigned short &x) {
x=0;
char ch=getchar();
while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+(ch^48), ch=getchar();
}
inline void read(unsigned int &x) {
x=0;
char ch=getchar();
while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+(ch^48), ch=getchar();
}
inline void read(unsigned long long &x) {
x=0;
char ch=getchar();
while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+(ch^48), ch=getchar();
}
template<class T,class ...Args>
inline void read(T &x,Args ...rest) {
read(x);
read(rest...);
}
}
namespace OUT {
char Buf[1<<23],*p1=Buf,Stack[50];
const char *p2=Buf+sizeof(Buf);
#define putchar(a) (p1==p2&&put_buf, *p1++=a)
#define put_buf (fwrite(Buf,1,p1-Buf,stdout) && memset(Buf,0,p1-Buf) && (p1=Buf))
#define exit put_buf,exit
inline void write(short x) {
x<0 && putchar('-') && (x=-x) || x==0 && putchar('0');
register char *top=Stack;
while(x!=0) *top++=x%10^48, x/=10;
while(top--!=Stack) putchar(*top);
}
inline void write(int x) {
x<0 && putchar('-') && (x=-x) || x==0 && putchar('0');
register char *top=Stack;
while(x!=0) *top++=x%10^48, x/=10;
while(top--!=Stack) putchar(*top);
}
inline void write(long long x) {
x<0 && putchar('-') && (x=-x) || x==0 && putchar('0');
register char *top=Stack;
while(x!=0) *top++=x%10^48, x/=10;
while(top--!=Stack) putchar(*top);
}
inline void write(unsigned short x) {
x==0 && putchar('0');
register char *top=Stack;
while(x!=0) *top++=x%10^48, x/=10;
while(top--!=Stack) putchar(*top);
}
inline void write(unsigned int x) {
x==0 && putchar('0');
register char *top=Stack;
while(x!=0) *top++=x%10^48, x/=10;
while(top--!=Stack) putchar(*top);
}
inline void write(unsigned long long x) {
x==0 && putchar('0');
register char *top=Stack;
while(x!=0) *top++=x%10^48, x/=10;
while(top--!=Stack) putchar(*top);
}
template<class T,class ...Args>
inline void write(T x,Args ...rest) {
write(x);
write(rest...);
}
}
//}
//矩阵运算
namespace Mat {
#define ll long long
#define N 105
struct mat {
ll a[N][N];
mat() {
memset(a,0,sizeof a);
}
mat operator+(const mat& T) {
mat res;
for(int i=1; i<=N; i++)
for(int j=1; j<=N; j++)
res.a[i][j]=a[i][j]+T.a[i][j];
return res;
}
mat operator-(const mat& T) {
mat res;
for(int i=1; i<=N; i++)
for(int j=1; j<=N; j++)
res.a[i][j]=a[i][j]-T.a[i][j];
return res;
}
mat operator*(const mat& T) {
mat res;
int r;
for(int i=1; i<=N; i++)
for(int k=1; k<=N; k++) {
r=a[i][k];
for(int j=1; j<=N; j++)
res.a[i][j]+=T.a[k][j]*r;
}
return res;
}
mat operator^(ll x) {
mat res,bas;
for(int i=1; i<=N; i++)res.a[i][i]=1;
for(int i=1; i<=N; i++)
for(int j=1; j<=N; j++)
bas.a[i][j]=a[i][j];
while(x) {
if(x&1)res=res*bas;
bas=bas*bas;
x>>=1;
}
return res;
}
};
}
//字典树
namespace TRIE {
#define N 500005
class Trie {
private:
int nxt[N][26],cnt;
bool tag[N];
public:
void insert(const string &s) {
int len=s.size();
int u=0;
for(int i=0; i<len; i++) {
int c=s[i]-'a';
if(!nxt[u][c])nxt[u][c]=++cnt;
u=nxt[u][c];
}
tag[u]=true;
}
bool find(const string &s) {
int len=s.size();
int u=0;
for(int i=0; i<len; i++) {
int c=s[i]-'a';
if(!nxt[u][c])return 0;
u=nxt[u][c];
}
return tag[u];
}
} trie;
}
//AC自动机
namespace ACAM {
#define N 100005
class AC_auto {
private:
struct AC_data {
int fail;
int vis[26];
int num;
} AC[N];
int cnt;
public:
inline void build(const string &s) {
int now=0;
for(auto c:s) {
if(!AC[now].vis[c-'a'])AC[now].vis[c-'a']=++cnt;
now=AC[now].vis[c-'a'];
}
AC[now].num++;
}
inline void get_fail() {
queue<int>q;
for(int i=0; i<26; ++i)if(AC[0].vis[i])q.push(AC[0].vis[i]);
while(!q.empty()) {
int now=q.front();
q.pop();
for(int i=0; i<26; ++i) {
if(AC[now].vis[i]) {
AC[AC[now].vis[i]].fail=AC[AC[now].fail].vis[i];
q.push(AC[now].vis[i]);
} else {
AC[now].vis[i]=AC[AC[now].fail].vis[i];
}
}
}
}
inline int query(const string &s) {
int ans=0,now=0;
for(auto c:s) {
now=AC[now].vis[c-'a'];
for(int t=now; t&&AC[t].num!=-1; t=AC[t].fail) {
ans+=AC[t].num;
AC[t].num=0;
}
}
return ans;
}
};
}
//树状数组
namespace Fenwick_Tree {
struct FT {
#define N 500005
#define lowbit(x) x&-x
ll c[N];
template<class T> void add(const T& x,const T& k) {
for(; x<=N; x+=lowbit(x))c[x]+=k;
}
int ask(ll x){
ll res=0;
for(;x;x-=lowbit(x))res+=c[x];
return res;
}
};
}
//线段树
namespace Seg_Tree {
struct seg {
#define ll long long
#define N 100005
ll mod,a[N],d[4*N],laz[4*N],mul[4*N];
void build(ll s,ll t,ll p) {
mul[p] = 1;
if(s == t) {
d[p] = a[s];
return ;
}
ll mid = (s+t)/2;
build(s,mid,p*2);
build(mid+1,t,p*2+1);
d[p] = d[p*2] + d[p*2+1];
d[p] %= mod;
return;
}
void pushdown(ll s,ll t,ll p) {
ll l = p*2, r = p*2+1, mid = (s+t)/2;
if(mul[p] != 1) {
mul[l] *= mul[p];
mul[l] %= mod;
mul[r] *= mul[p];
mul[r] %= mod;
laz[l] *= mul[p];
laz[l] %= mod;
laz[r] *= mul[p];
laz[r] %= mod;
d[l] *= mul[p];
d[l] %= mod;
d[r] *= mul[p];
d[r] %= mod;
mul[p] = 1;
}
if(laz[p]) {
d[l] += laz[p] * (mid-s+1);
d[l] %= mod;
d[r] += laz[p] * (t-mid);
d[r] %= mod;
laz[l] += laz[p];
laz[l] %= mod;
laz[r] += laz[p];
laz[r] %= mod;
laz[p] = 0;
}
return ;
}
void upchen(ll s,ll t,ll l,ll r,ll p,ll k) {
if(l<=s && t <= r) {
mul[p] *= k;
mul[p] %= mod;
laz[p] *= k;
laz[p] %= mod;
d[p] *= k;
d[p] %= mod;
return ;
}
ll mid = (s+t)/2;
pushdown(s,t,p);
if(l <= mid) upchen(s,mid,l,r,p*2,k);
if(mid < r) upchen(mid+1,t,l,r,p*2+1,k);
d[p] = d[p*2] + d[p*2+1];
d[p] %= mod;
return;
}
void update(ll s,ll t,ll l,ll r,ll p,ll k) {
if(l <= s && t <= r) {
d[p] += (t-s+1)*k;
d[p] %= mod;
laz[p] += k;
laz[p] %= mod;
return ;
}
ll mid = (s+t)/2;
pushdown(s,t,p);
if(l <= mid) update(s,mid,l,r,p*2,k);
if(mid < r)update(mid+1,t,l,r,p*2+1,k);
d[p] = d[p*2] + d[p*2+1];
d[p] %= mod;
return;
}
ll getsum(ll s,ll t,ll l,ll r,ll p) {
if(l <= s && t <= r) return d[p];
ll mid = (s+t)/2;
ll sum=0;
pushdown(s,t,p);
if(l <= mid) sum += getsum(s,mid,l,r,p*2);
sum %= mod;
if(mid < r) sum += getsum(mid+1,t,l,r,p*2+1);
sum %= mod;
return sum;
}
};
}