自制有用工具
快速读入
:::info[代码]
//快速读入
namespace mqy_std{
class fast_io{
template<typename Type>
void pwrite(Type n) const{
if(n<0)putchar('-'),n*=-1;
if(n==0)return;
pwrite(n/10);
putchar((char)(n%10+'0'));
}
public:
template<typename Type = int>
Type read() const{
Type x=0,f=1;char ch=getchar();
while(ch<'0'&&ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=x*10+(ch-'0'),ch=getchar();
return x*f;
}
template<typename Type>
void read(Type &n) const{
Type x=0,f=1;char ch=getchar();
while(ch<'0'&&ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=x*10+(ch-'0'),ch=getchar();
n=x*f;
}
template<typename Type>
void write(Type n) const{
if(n==0){cout<<0;return;}
pwrite(n);
}
};
const static fast_io io;
}
:::
fast_io快读类io.read()读入,默认返回intio.read<Type>()读入,在Type处填写类型(仅限整形),返回对应io.read(x)读入xio.write(x)输出x,只能为int,long long,short,long。排序
按需添加(其实只需要归并和快速) :::info[归并排序] namespace mqy_std{ //冒泡排序 void maopaosort(inta,int n) { for(int i=1;i<n;i++) { bool flag=true; for(int j=1;j<=n-i;j++) if(a[j]>a[j+1]) swap(a[j],a[j+1]),flag=false; if(flag)break; } } //选择排序 void xuanzesort(inta) { // 时间 n ^ 2 稳定 // 空间 1 不稳定 for(int i=1;i <= n;i++) { int minn = i; for(int j=i;j<=n;j++) if(a[minn]>a[j]) minn=j; swap(a[minn],a[i]); } } //插入排序 void charusort(inta) { for(int i=2;i<=n;i++) { int t=a[i],j; for(j=i-1;j>=1;j--) if(a[j]>t)a[j+1]=a[j]; else break; a[j]=t; } } //归并排序 void mergesort(int a,int l,int r) { if(l>=r) return; int mid=l+r>>1; mergesort(a,l,mid); mergesort(a,mid+1,r); int i=l,j=mid+1,x=l; while(x<=r) { if(i>mid)b[x++]=a[j++]; else if(j>r)b[x++]=a[i++]; else if(a[i]<=a[j])b[x++]=a[i++]; else b[x++]=a[j++]; } for(int i=l;i<=r;i++)a[i]=b[i]; } } :::