卡常相关
卡常宝典
快读 & 快写(快写实测不如关同步流的cout和printf)
inline int read()
{
int 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 << 1) + (x << 3) + (ch ^ 48); ch = getchar(); }
return x * f;
}
inline void write(int x)
{
int top = 0;
bool flag = 0;
static char sta[35];
if (x < 0) flag = 1, x = -x;
do { sta[top++] = x % 10 + 48, x /= 10; } while (x);
if (flag) sta[top++] = '-';
while (top) putchar(sta[--top]);
return;
}lqz的快读
使用:把cin,cout换成fin,fout即可。 缺点:我没写浮点数。
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
namespace fastio
{
static const int BUFSZ = 65536;
int obuftop(0);
char obuf[BUFSZ + 2], *rbuf;
inline void init()
{
struct stat st; int f = fileno(stdin); fstat(f, &st);
rbuf = (char *) mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, f, 0);
return;
}
inline char getchar() { return *(rbuf++); }
inline void flush() { write(1, obuf, obuftop); obuftop = 0; return; }
inline void putchar(char ch) { if (obuftop > BUFSZ) flush(); obuf[obuftop++] = ch; return; }
inline bool blank(char ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t' || ch == '\0'; }
struct reader
{
reader() { init(); }
template <typename T>
reader& operator >> (T &x)
{
x = 0; bool flag = 0; char ch = fastio::getchar();
while (!isdigit(ch))
{ if (ch == '-') flag = 1; ch = fastio::getchar(); }
while (isdigit(ch))
x = (x << 1) + (x << 3) + (ch ^ 48), ch = fastio::getchar();
x = flag ? -x : x; return *this;
}
reader& operator >> (char &x)
{ x = fastio::getchar(); while (blank(x)) x = fastio::getchar(); return *this; }
reader& operator >> (char *x)
{
int xtop = 0; x[0] = fastio::getchar();
while (blank(x[0])) x[0] = fastio::getchar();
while (!blank(x[xtop])) x[++xtop] = fastio::getchar();
x[xtop] = '\0'; return *this;
}
reader& operator >> (string &x)
{
x.clear();
char ch = fastio::getchar();
while (blank(ch)) ch = fastio::getchar();
while (!blank(ch)) x.push_back(ch), ch = fastio::getchar();
return *this;
}
}fin;
struct writer
{
~writer() { flush(); }
template <typename T>
writer& operator << (T tmppp)
{
char st[37];
int x(tmppp), sttop(0);
bool flag = 0;
if (x < 0) x = -x, flag = 1;
if (x == 0) st[++sttop] ='0';
while (x) st[++sttop] = x % 10 + 48, x /= 10;
if (flag) st[++sttop] = '-';
while (sttop) fastio::putchar(st[sttop--]);
return *this;
}
writer& operator << (char ch)
{ fastio::putchar(ch); return *this; }
writer& operator << (char *ch)
{
int xtop = 0;
while (ch[xtop] != '\0')
fastio::putchar(ch[xtop++]);
return *this;
}
writer& operator << (const char *ch)
{
int xtop = 0;
while (ch[xtop] != '\0')
fastio::putchar(ch[xtop++]);
return *this;
}
writer& operator << (string str)
{
int i = 0, len = str.length();
while (i < len) fastio::putchar(str[i++]);
return *this;
}
}fout;
}
using fastio::fin;
using fastio::fout;超级快读(实测速度不如上一个)
使用:同正常cin,cout
#include <bits/stdc++.h>
#define ONLINE_JUDGE
using namespace std;
namespace Fread
{
const int SIZE = 1 << 16;
char buf[SIZE], *S, *T;
inline char getchar() { if (S == T) { T = (S = buf) + fread(buf, 1, SIZE, stdin); if (S == T) return '\n'; } return *S++; }
}
using namespace Fread;
namespace Fwrite
{
const int SIZE = 1 << 16;
char buf[SIZE], *S = buf, *T = buf + SIZE;
inline void flush() { fwrite(buf, 1, S - buf, stdout); S = buf; }
inline void putchar(char c) { *S++ = c; if (S == T) flush(); }
struct NTR { ~NTR() { flush(); } } ztr;
}
using namespace Fwrite;
#define getchar Fread::getchar
#define putchar Fwrite::putchar
namespace Fastio
{
struct Reader
{
template <typename T> Reader& operator >> (T &x)
{
x = 0;
short f = 1;
char c = getchar();
while (c < '0' || c > '9') { if (c == '-') f *= -1; c = getchar(); }
while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
x *= f;
return *this;
}
Reader& operator >> (double &x)
{
x = 0;
double t = 0;
short f = 1, s = 0;
char c = getchar();
while ((c < '0' || c > '9') && c != '.') { if (c == '-') f *= -1; c = getchar(); }
while (c >= '0' && c <= '9' && c != '.') x = x * 10 + (c ^ 48), c = getchar();
if (c == '.') c = getchar();
else { x *= f; return *this; }
while (c >= '0' && c <= '9') t = t * 10 + (c ^ 48), s++, c = getchar();
while (s--) t /= 10.0;
x = (x + t) * f;
return *this;
}
Reader& operator >> (long double &x)
{
x = 0;
long double t = 0;
short f = 1, s = 0;
char c = getchar();
while ((c < '0' || c > '9') && c != '.') { if (c == '-') f *= -1; c = getchar(); }
while (c >= '0' && c <= '9' && c != '.') x = x * 10 + (c ^ 48), c = getchar();
if (c == '.') c = getchar();
else { x *= f; return *this; }
while (c >= '0' && c <= '9') t = t * 10 + (c ^ 48), s++, c = getchar();
while (s--) t /= 10.0;
x = (x + t) * f;
return *this;
}
Reader& operator >> (__float128 &x)
{
x = 0;
__float128 t = 0;
short f = 1, s = 0;
char c = getchar();
while ((c < '0' || c > '9') && c != '.') { if (c == '-') f *= -1; c = getchar(); }
while (c >= '0' && c <= '9' && c != '.') x = x * 10 + (c ^ 48), c = getchar();
if (c == '.') c = getchar();
else { x *= f; return *this; }
while (c >= '0' && c <= '9') t = t * 10 + (c ^ 48), s++, c = getchar();
while (s--) t /= 10.0;
x = (x + t) * f;
return *this;
}
Reader& operator >> (char &c)
{
c = getchar();
while (c == ' ' || c == '\n' || c == '\r') c = getchar();
return *this;
}
Reader& operator >> (char *str)
{
int len = 0;
char c = getchar();
while (c == ' ' || c == '\n' || c == '\r') c = getchar();
while (c != ' ' && c != '\n' && c != '\r') str[len++] = c, c = getchar();
str[len] = '\0';
return *this;
}
Reader& operator >> (string &str)
{
str.clear();
char c = getchar();
while (c == ' ' || c == '\n' || c == '\r') c = getchar();
while (c != ' ' && c != '\n' && c != '\r') str.push_back(c), c = getchar();
return *this;
}
Reader() {}
} cin;
const char endl = '\n';
struct Writer
{
const int Setprecision = 6;
typedef int mxdouble;
template <typename T> Writer& operator << (T x)
{
if (x == 0) { putchar('0'); return *this; }
if (x < 0) putchar('-'), x = -x;
static short sta[40];
short top = 0;
while (x > 0) sta[++top] = x % 10, x /= 10;
while (top > 0) putchar(sta[top] + '0'), top--;
return *this;
}
Writer& operator << (double x)
{
if (x < 0) putchar('-'), x = -x;
mxdouble _ = x;
x -= (double)_;
static short sta[40];
short top = 0;
while (_ > 0) sta[++top] = _ % 10, _ /= 10;
if (top == 0) putchar('0');
while (top > 0) putchar(sta[top] + '0'), top--;
putchar('.');
for (int i = 0; i < Setprecision; i++) x *= 10;
_ = x;
while (_ > 0) sta[++top] = _ % 10, _ /= 10;
for (int i = 0; i < Setprecision - top; i++) putchar('0');
while (top > 0) putchar(sta[top] + '0'), top--;
return *this;
}
Writer& operator << (long double x)
{
if (x < 0) putchar('-'), x = -x;
mxdouble _ = x;
x -= (long double)_;
static short sta[40];
short top = 0;
while (_ > 0) sta[++top] = _ % 10, _ /= 10;
if (top == 0) putchar('0');
while (top > 0) putchar(sta[top] + '0'), top--;
putchar('.');
for (int i = 0; i < Setprecision; i++) x *= 10;
_ = x;
while (_ > 0) sta[++top] = _ % 10, _ /= 10;
for (int i = 0; i < Setprecision - top; i++) putchar('0');
while (top > 0) putchar(sta[top] + '0'), top--;
return *this;
}
Writer& operator << (__float128 x)
{
if (x < 0) putchar('-'), x = -x;
mxdouble _ = x;
x -= (__float128)_;
static short sta[40];
short top = 0;
while (_ > 0) sta[++top] = _ % 10, _ /= 10;
if (top == 0) putchar('0');
while (top > 0) putchar(sta[top] + '0'), top--;
putchar('.');
for (int i = 0; i < Setprecision; i++) x *= 10;
_ = x;
while (_ > 0) sta[++top] = _ % 10, _ /= 10;
for (int i = 0; i < Setprecision - top; i++) putchar('0');
while (top > 0) putchar(sta[top] + '0'), top--;
return *this;
}
Writer& operator << (char c) { putchar(c); return *this; }
Writer& operator << (char *str)
{
int cur = 0;
while (str[cur]) putchar(str[cur++]);
return *this;
}
Writer& operator << (const char *str)
{
int cur = 0;
while (str[cur]) putchar(str[cur++]);
return *this;
}
Writer& operator << (string str)
{
int st = 0, ed = str.size();
while (st < ed) putchar(str[st++]);
return *this;
}
Writer() {}
} cout;
}
using namespace Fastio;
#define cin Fastio::cin
#define cout Fastio::cout
#define endl Fastio::endl火车头
#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")基数排序
namespace radix
{
const int logbase = 4;
const int base = 1 << (1 << logbase);
const int imax = 2147483647;
const int szint(sizeof(int) << 3);
const int sz((sizeof(int) << 3) >> logbase);
int tmp[N], mp[base];
inline void sort(int *begin, int *end)
{
register int lm(0), rm(0);
for (register int r = 1; r <= sz; ++r)
{
lm = (szint - (1 << logbase) * r), rm = szint - (1 << logbase) - 1;
for (register int *t = begin; t != end; ++t) ++mp[(((*t) << lm) & imax) >> rm];
for (register int i = 1; i < base; ++i) mp[i] += mp[i - 1];
for (register int *t = end - 1; t >= begin; --t) tmp[--mp[(((*t) << lm) & imax) >> rm]] = *t;
for (register int *t = begin; t != end; ++t) *t = tmp[t - begin];
memset(mp, 0, sizeof(mp));
} return;
}
}快速gcd
inline int gcd(int x, int y)
{
int popx = __builtin_ctz(x);
int popy = __builtin_ctz(y);
const int popmin = min(popx, popy);
int tmp; y >>= popy;
while (x)
{
x >>= popx;
tmp = y - x;
popx = __builtin_ctz(tmp);
y = x < y ? x : y;
x = tmp > 0 ? tmp : -tmp;
}
return y << popmin;
}类
简易bitset
namespace lqzstl
{
template <size_t sz, unsigned int w = 6>
class bitset
{
private:
using ull = unsigned long long;
#define fdz(x) (x >> w)
#define fdf(x) (x - ((x >> w) << w))
const static ull siz = (sz + ((1 << w) - 1)) >> w;
const ull full = -1ull, empt = 0ull;
public:
ull bt[siz];
class reference
{
private:
size_t pos = 0;
bitset *b = nullptr;
bool val = 0;
inline void upd()
{
if (val) (b -> bt[fdz(pos)]) &= (~(1ull << fdf(pos)));
else (b -> bt[fdz(pos)]) |= (((ull)val) << fdf(pos));
return;
}
public:
reference() { return; }
reference(const size_t& pos, bitset *b)
{
this -> pos = pos, this -> b = b;
val = (b -> bt[fdz(pos)] >> fdf(pos)) & 1;
}
~reference() {}
operator bool () const { return val; }
bool operator ~() const { return !val; }
reference& flip() { val ^= 1; upd(); return *this; }
reference& operator = (bool x) { val = x; upd(); return *this; }
reference& operator = (const reference& x) { val = x.val; upd(); return *this; }
};
bitset() { memset(bt, 0, sizeof(bt)); return; }
inline size_t size() { return sz; }
inline void reset() { memset(bt, 0, sizeof(bt)); return; }
inline void set(const bool val = 1) { memset(bt, val ? 255 : 0, sizeof(bt)); return; }
inline void reset(const size_t &pos) { bt[fdz(pos)] &= (~(1ull << fdf(pos))); return; }
inline void set(const size_t &pos, const bool val = true)
{
if (!val) bt[fdz(pos)] &= (~(1ull << fdf(pos)));
else bt[fdz(pos)] |= (1ull << fdf(pos)); return;
}
inline void flip() { for (int i = 0; i < siz; ++i) bt[i] ^= full; return; }
inline bool test(int pos) { return (bt[fdz(pos)] >> fdf(pos)) & 1ull; }
inline reference operator [] (const size_t& pos) & { return reference(pos, this); }
inline bool any()
{
bool res(0);
for (int i = 0; i < siz; ++i)
if (bt[i]) { res = 1; break; }
return res;
}
inline bool none()
{
bool res(0);
for (int i = 0; i < siz; ++i)
if (bt[i]) { res = 1; break; }
return (!res);
}
inline bool all()
{
bool res(0);
for (int i = 0; i < siz; ++i)
if (bt[i] != 1) { res = 1; break; }
return (!res);
}
inline unsigned int count()
{
unsigned int res(0);
for (int i = 0; i < siz; ++i)
res += __builtin_popcountll(bt[i]);
return res;
}
private:
#undef fdz
#undef fdf
};
}