模板 - 杂项
快读+快写
整合
v1
点击查看代码
namespace IO{
template<typename TYPE> void read(TYPE &x)
{
x=0; bool neg=false; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')neg=true;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^'0');ch=getchar();}
if(neg) x=-x; return;
}
template<typename TYPE> void write(TYPE x)
{
if(!x){putchar('0');return;} if(x<0){putchar('-');x=-x;}
static int sta[55]; int statop=0; while(x){sta[++statop]=x%10;x/=10;}
while(statop) putchar('0'+sta[statop--]); return;
}
}
v2
点击查看代码
namespace IO{
template<typename TYPE> void read(TYPE &x)
{
x=0; bool neg=false; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')neg=true;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^'0');ch=getchar();}
if(neg){x=-x;} return;
}
template<typename TYPE> void write(TYPE x)
{
if(!x){putchar('0');return;} if(x<0){putchar('-');x=-x;}
static int sta[55];int statop=0; while(x){sta[++statop]=x%10;x/=10;}
while(statop){putchar('0'+sta[statop--]);} return;
}
template<typename TYPE> void write(TYPE x,char ch){write(x);putchar(ch);return;}
} using namespace IO;
v3
点击查看代码
namespace IO{
const int SIZE=1<<20;
char buf[SIZE],*p1=buf,*p2=buf;
inline char super_getchar()
{
if(p1==p2)
{
p1=buf,p2=buf+fread(buf,1,SIZE,stdin);
return p1==p2?EOF:*p1++;
}
else return *p1++;
}
template<typename TYPE> void read(TYPE &x)
{
x=0; bool neg=false; char ch=super_getchar();
while(ch<'0'||ch>'9'){if(ch=='-')neg=true;ch=super_getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^'0');ch=super_getchar();}
if(neg){x=-x;} return;
}
template<typename TYPE> void write(TYPE x)
{
if(!x){putchar('0');return;} if(x<0){putchar('-');x=-x;}
static int sta[55];int statop=0; while(x){sta[++statop]=x%10;x/=10;}
while(statop){putchar('0'+sta[statop--]);} return;
}
template<typename TYPE> void write(TYPE x,char ch){write(x);putchar(ch);return;}
} using namespace IO;
v4
点击查看代码
namespace IO{
#ifndef JC_LOCAL
const int SIZE=1<<20; char buf[SIZE],*p1=buf,*p2=buf;
#define getchar() ((p1==p2&&(p2=(p1=buf)+fread(buf,1,SIZE,stdin),p1==p2))?EOF:*p1++)
#endif
template<typename TYPE> void read(TYPE &x)
{
x=0; bool neg=false; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')neg=true;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+(ch^'0');ch=getchar();}
if(neg){x=-x;} return;
}
template<typename TYPE> void write(TYPE x)
{
if(!x){putchar('0');return;} if(x<0){putchar('-');x=-x;}
static int sta[55];int statop=0; while(x){sta[++statop]=x%10;x/=10;}
while(statop){putchar('0'+sta[statop--]);} return;
}
template<typename TYPE> void write(TYPE x,char ch){write(x);putchar(ch);return;}
} using namespace IO;
v4.1
点击查看代码
namespace IO{
#ifndef JC_LOCAL
const int SIZE=1<<20; char buf[SIZE],*p1=buf,*p2=buf;
#define getchar() ((p1==p2&&(p2=(p1=buf)+fread(buf,1,SIZE,stdin),p1==p2))?EOF:*p1++)
#endif
template<typename TYPE> void read(TYPE &x)
{
x=0; bool neg=false; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')neg=true;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+(ch^'0');ch=getchar();}
if(neg){x=-x;} return;
}
template<typename TYPE> void write(TYPE x)
{
if(!x){putchar('0');return;} if(x<0){putchar('-');x=-x;}
static int sta[55];int statop=0; while(x){sta[++statop]=x%10;x/=10;}
while(statop){putchar('0'+sta[statop--]);} return;
}
template<typename TYPE> void write(TYPE x,char ch){write(x);putchar(ch);return;}
} using IO::read; using IO::write;
v5
点击查看代码
namespace IO{
#ifndef JC_LOCAL
const int SIZE=1<<20; char buf[SIZE],*p1=buf,*p2=buf;
inline char _getchar() {return (p1==p2&&(p2=(p1=buf)+fread(buf,1,SIZE,stdin),p1==p2))?EOF:*p1++;}
#else
inline char _getchar() {return getchar();}
#endif
template<typename TYPE> void read(TYPE &x)
{
x=0; bool neg=false; char ch=_getchar();
while(ch<'0'||ch>'9'){if(ch=='-')neg=true;ch=_getchar();}
while(ch>='0'&&ch<='9'){x=x*10+(ch^'0');ch=_getchar();}
if(neg){x=-x;} return;
}
template<typename TYPE> void write(TYPE x)
{
if(!x){putchar('0');return;} if(x<0){putchar('-');x=-x;}
static int sta[55];int statop=0; while(x){sta[++statop]=x%10;x/=10;}
while(statop){putchar('0'+sta[statop--]);} return;
}
template<typename TYPE> void write(TYPE x,char ch){write(x);putchar(ch);return;}
} using IO::read; using IO::write;
v6
namespace IO{
#ifndef JC_LOCAL
const int SIZE = 1 << 20;
char inbuf[SIZE], *inp1 = inbuf, *inp2 = inbuf;
inline char _getchar()
{
if (inp1 == inp2)
{
inp1 = inbuf;
inp2 = inbuf + fread(inbuf, 1, SIZE, stdin);
if (inp1 == inp2) return EOF;
else return *inp1++;
}
return *inp1++;
}
char outbuf[SIZE], *outp = outbuf;
inline void flush()
{
fwrite(outbuf, 1, outp - outbuf, stdout);
outp = outbuf;
return;
}
struct Flush { ~Flush() { flush(); } } _;
inline char _putchar(char ch)
{
if (outp == outbuf + SIZE) flush();
*outp = ch;
return *outp++;
}
#else
inline char _getchar() { return getchar(); }
inline void _putchar(char ch) { putchar(ch); }
#endif
template<typename TYPE> inline void read(TYPE &x)
{
x=0;
bool neg = false;
char ch = _getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-') neg = true;
ch = _getchar();
}
while (ch >= '0' && ch <= '9')
{
x = x * 10 + (ch ^ '0');
ch = _getchar();
}
if (neg) x = -x;
return;
}
template<typename TYPE> inline void write(TYPE x)
{
if (x < 0) _putchar('-'), x = -x;
static int sta[55]; int statop = 0;
do sta[++statop] = x % 10, x /= 10; while (x);
while (statop) _putchar('0' | sta[statop--]);
return;
}
template<typename TYPE> inline void write(TYPE x, char ch) { write(x); _putchar(ch); }
} using IO::read; using IO::write;
离散化
for(int i=1;i<=n;i++) b[i]=a[i];
sort(b+1,b+n+1);
int len=unique(b+1,b+n+1)-(b+1);
for(int i=1;i<=n;i++)
a[i]=lower_bound(b+1,b+len+1,a[i])-b;
二分
整数二分
int BinarySearch(const int L,const int R)
{
int l=L-1,r=R+1;
while(l+1<r)
{
int mid=l+r>>1;
if(check(mid)) l=mid;
else r=mid;
}
return l;
}
浮点数二分
const double EPS=1e-6;
double BinarySearch(const double L,const double R)
{
double l=L,r=R;
while(r-l>EPS)
{
double mid=(l+r)/2.0;
if(check(mid)) l=mid;
else r=mid;
}
return l;
}
三分
浮点数三分求单峰函数极值
const double EPS=1e-6;
double TernarySearch(const double L,const double R)
{
double l=L,r=R;
while(r-l>EPS)
{
double lmid=l+(r-l)/3.0,rmid=r-(r-l)/3.0;
double lres=calc(lmid),rres=calc(rmid);
if(lres<=rres) l=lmid;
if(lres>=rres) r=rmid;
}
return l;
}
本文采用 「CC-BY-NC 4.0」 创作共享协议,转载请注明作者及出处,禁止商业使用。
作者:Jerrycyx,原文链接:https://www.cnblogs.com/jerrycyx/p/18538326

浙公网安备 33010602011771号