跳到正文

个人记录

自制有用工具

__maqiyue约 4 分钟
原文
文章信息与版本

文章信息

发布
2025年12月14日
收录
2026年10月5日
更新
2026年11月12日
篇幅
约 4 分钟

版本

  1. v1当前

快速读入

代码
C++
//快速读入
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() 读入,默认返回 int
  • io.read<Type>() 读入,在 Type 处填写类型(仅限整形),返回对应
  • io.read(x) 读入 x
  • io.write(x) 输出 x,只能为 int,long long,short,long。

排序

按需添加(其实只需要归并和快速)

归并排序

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]; } }

原文