<前言>
之前异地登录,恰巧绑定的手机没用了,所以csdn一直登不上。
之前还注册了一个小号,但是没啥用。现在,几分钟前,我用了邮箱才搞出来这个玩意。
现在,我回来了。
翻看以前的记录,真的是从字逢间读出那股欢快与喜悦。
csdn还是尽量不要荒掉,毕竟还是有很多人用的,高质量文章也不少。
今天就当庆祝账号找回,我也要连发两三篇。
这篇就先放一下最近的一些封装模板吧。
<正文>
贴出一些我的结构体模板,为了在外也能看到这些模板。
一些标准模板
// 以下文件超读。
using std::cin;
using std::cout;
using std::endl;
#define MAX_INPUT 1000000
#define cinchar() ((fs == ft && (ft = (fs = buf) + fb->sgetn(buf, MAX_INPUT))) ? 0 : *fs++)
namespace io_file
{
char buf[MAX_INPUT], *fs, *ft;
inline int read()
{
static std::streambuf *fb = cin.rdbuf();
register int x = 0;
register bool f = false;
register char ch = cinchar();
while (!isdigit(ch))
{
if (ch == '-')
f = true;
ch = cinchar();
}
while (isdigit(ch))
{
x = (x << 1) + (x << 3) + ch - '0';
ch = cinchar();
}
return f ? -x : x;
}
inline char getc()
{
static std::streambuf *fb = cin.rdbuf();
return cinchar();
}
inline void put(int x)
{
static std::streambuf *fb = cout.rdbuf();
static char stack[11];
static int top = 0;
if (!x)
{
fb->sputc('0');
fb->sputc('\n');
return;
}
while (x)
{
stack[++top] = x % 10 + '0';
x /= 10;
}
while (top)
{
fb->sputc(stack[top]);
--top;
}
fb->sputc('\n');
}
inline void putc(char x)
{
static std::streambuf *fb = cout.rdbuf();
fb->sputc(x);
}
int main()
{
freopen("add.in", "r", stdin);
freopen("add.out", "w", stdout);
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int a = read(), b = read();
put(a + b);
return 0;
}
}
#undef MAX_INPUT
#undef cinchar
//以下标准快读
namespace io_std
{
inline int read()
{
int s = 0, w = 1;
char c = getchar();
while((c < '0' || c > '9') && c != '-')
c = getchar();
if(c == '-')w = -1, c = getchar();
while(c <= '9' && c >= '0')
s = (s << 3) + (s << 1) + c - '0', c = getchar();
return s * w;
}
int cnt = 0;
char out[25];
inline void print(int x)
{
if(!x)return putchar('0'), putchar(32), void();
while(x)out[++cnt] = x % 10 + 48, x /= 10;
while(cnt)putchar(out[cnt]), --cnt;
return putchar(32), void();
}
void write(int x)
{
if(x > 9)write(x / 10);
putchar(x % 10 + '0');
}
inline void writeln(int x, int op)
{
return (op == 1 ? (write(x), putchar(32)) : (write(x), putchar(10))), void();
}
}
//以下树&图
int a[N] = {};
struct Tree
{
int to[N << 1], net[N << 1], w[N << 1];
int fl[N], len;
inline void clear()
{
memset(to, 0, sizeof(to));
memset(net, 0, sizeof(net));
memset(w, 0, sizeof(w));
memset(fl, 0, sizeof(fl));
len = 0;
}
inline void inc(int x, int y, int z)
{
to[++len] = y;
w[len] = z;
net[len] = fl[x];
fl[x] = len;
}
} T;
struct Graph
{
int to[M << 1], net[M << 1], w[M << 1];
int fl[N], len;
inline void clear()
{
memset(to, 0, sizeof(to));
memset(net, 0, sizeof(net));
memset(w, 0, sizeof(w));
memset(fl, 0, sizeof(fl));
len = 0;
}
inline void inc(int x, int y, int z)
{
to[++len] = y;
w[len] = z;
net[len] = fl[x];
fl[x] = len;
}
} G;
//并查集
struct US_find
{
int f[N], n;
inline void clear()
{
for(int i = 1; i <= n; ++i)f[i] = i;
}
inline void set(int m)
{
return n = m, void();
}
inline void build(int m)
{
return set(m), clear(), void();
}
inline int get(int x)
{
return x == f[x] ? x : f[x] = get(f[x]);
}
inline void merge(int x, int y)
{
int u = get(x), v = get(y);
if(u != v)f[u] = v;
return void();
}
};
//线段树
namespace Segmentree
{
struct SegmenTree
{
int l, r;
int sum;
} t[N << 2] = {};
inline void push(int p)
{
return t[p].sum = t[p << 1].sum + t[p << 1 | 1].sum, void();
}
void build(int p, int l, int r)
{
t[p].l = l;
t[p].r = r;
if(l == r)
{
t[p].sum = a[l];
return ;
}
int mid = (l + r) >> 1;
build(p << 1, l, mid);
build(p << 1 | 1, mid + 1, r);
push(p);
}
int ask(int p, int l, int r)
{
if(l <= t[p].l && t[p].r <= r)return t[p].sum;
int mid = (t[p].l + t[p].r) >> 1, sum = 0;
if(l <= mid)sum += ask(p << 1, l, r);
if(r > mid)sum += ask(p << 1 | 1, l, r);
return sum;
}
void change(int p, int x, int v)
{
if(t[p].l == t[p].r)
{
t[p].sum = v;
return ;
}
int mid = (t[p].l + t[p].r) >> 1;
if(x <= mid)change(p << 1, x, v);
else change(p << 1 | 1, x, v);
push(p);
}
};
//欧拉序LCA
struct LCA
{
int dfn[N << 1], tr[N], d[N << 1];
int vs;
void dfs(int u, int fa, int deh)
{
dfn[++vs] = u;
d[vs] = deh;
tr[u] = vs;
for(int i = T.fl[u]; i; i = T.net[i])
{
int v = T.to[i];
if(v == fa)continue;
dfs(v, u, deh + 1);
dfn[++vs] = u;
d[vs] = deh;
}
}
int f[N << 1][31];
inline int calc(int x, int y)
{
return d[x] < d[y] ? x : y;
}
int lca(int x, int y)
{
int l = tr[x], r = tr[y];
if(l > r)swap(l, r);
int block = log(r - l + 1) / log(2);
return dfn[calc(f[l][block], f[r - (1 << block) + 1][block])];
}
void pre()
{
for(int i = 1; i <= vs; ++i)
f[i][0] = i;
for(int i = 1; i < 30; ++i)
for(int j = 1; j + (1 << i) - 1 <= vs; ++j)
f[j][i] = calc(f[j][i - 1], f[j + (1 << (i - 1))][i - 1]);
}
void work(int root, int m)
{
dfs(root, 0, 1);
pre();
for(int i = 1; i <= m; ++i)
{
int l = read(), r = read();
printf("%d\n", lca(l, r));
}
}
} K;
//以下字符trie
//代码源自异或最大值
struct trie
{
int tr[N << 1][2];
int cnt = 0;
inline void inc(int x)
{
int now = 0;
for(int i = 30; i >= 0; --i)
{
int t = x >> i & 1;
if(!tr[now][t])tr[now][t] = ++cnt;
now = tr[now][t];
}
}
inline int ask(int x)
{
int now = 0, ans = 0;
for(int i = 30; i >= 0; --i)
{
int t = x >> i & 1;
if(tr[now][t ^ 1])
ans += 1 << i, now = tr[now][t ^ 1];
else now = tr[now][t];
}
return ans;
}
inline void clear()
{
memset(tr, 0, sizeof(tr));
cnt = 0;
}
} tr;
//输入输出测试结论
/*
conclusion:
---------------
文件比较:
基本无差别
文本多次测试结果为后来者更优秀。
码量而言也相差无几。
stdin、stdout有待测试.
模板未定。
---------------------------------------
loj架构测试结果(duoxiaoCODE):
一测:
write速度快于print,测试时间大约47:42。
模板定为:
namespace io
{
inline int read()
{
int s = 0, w = 1;
char c = getchar();
while((c < '0' || c > '9') && c != '-')
c = getchar();
if(c == '-')w = -1, c = getchar();
while(c <= '9' && c >= '0')
s = (s << 3) + (s << 1) + c - '0', c = getchar();
return s * w;
}
int cnt = 0;
char out[25];
void write(int x)
{
if(x > 9)write(x / 10);
putchar(x % 10 + '0');
}
inline void writeln(int x, int op)
{
return (op == 1 ? (write(x), putchar(32)) : (write(x), putchar(10))), void();
}
}
*/
以下大项目。
手写分数类。
inline int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
inline int lcm(int x, int y)
{
return x / gcd(x, y) * y;
}
struct frac
{
int so, ma;
frac(int x = 0, int y = 1)
{
so = x;
ma = y;
}
inline void hua(void)
{
int gg = gcd(so, ma);
if (gg == 0)
return;
so /= gg;
ma /= gg;
}
friend frac operator +(frac a, frac b)
{
int mother = lcm(a.ma, b.ma);
frac c = frac(b.so * a.ma + a.so * b.ma, mother);
return c.hua(), c;
}
friend frac operator -(frac a, frac b)
{
int mother = lcm(a.ma, b.ma);
frac c = frac(a.so * b.ma - b.so * a.ma, mother);
return c.hua(), c;
}
friend frac operator *(frac a, frac b)
{
frac c = frac(a.so * b.so, a.ma * b.ma);
return c.hua(), c;
}
friend frac operator /(frac a, frac b)
{
b.so ^= b.ma ^= b.so ^= b.ma;
return a * b;
}
friend bool operator <(frac a, frac b)
{
return 1LL * a.so * b.ma < 1LL * b.so * a.ma;
}
friend bool operator >(frac a, frac b)
{
return 1LL * a.so * b.ma > 1LL * b.so * a.ma;
}
friend bool operator <=(frac a, frac b)
{
return 1LL * a.so * b.ma <= 1LL * b.so * a.ma;
}
friend bool operator >=(frac a, frac b)
{
return 1LL * a.so * b.ma >= 1LL * b.so * a.ma;
}
friend bool operator ==(frac a, frac b)
{
return 1LL * a.so * b.ma == 1LL * b.so * a.ma;
}
};
手写向量类
#include <bits/stdc++.h>
using namespace std;
const double ect = 1e-8;
struct vec
{
double x, y;
vec()
{
}
vec(double _x, double _y)
{
x = _x; y = _y;
}
inline vec operator +(vec b)
{
return vec(x + b.x, y + b.y);
}
inline vec operator -(vec b)
{
return vec(x - b.x, y - b.y);
}
inline vec operator -()
{
return vec(-x, -y);
}
inline double operator *(vec b)
{
return x * b.x + y * b.y;
}
inline double operator %(vec b)
{
return x * b.y - y * b.x;
}
inline double operator ~()
{
return x * x + y * y;
}
inline bool operator ==(vec b)
{
return fabs(x - b.x) <= ect && fabs(y - b.y) <= ect;
}
inline bool operator !=(vec b)
{
return fabs(x - b.x) > ect || fabs(y - b.y) > ect;
}
inline vec Unit()
{
static double _; return _ = sqrt(x * x + y * y), vec(x / _, y / _);
}
inline vec Norm()
{
static double _; return _ = sqrt(x * x + y * y), vec(-y / _, x / _);
}
inline bool Quad()
{
return y > ect || (fabs(y) <= ect && x >= -ect);
}
inline bool operator <(vec b)
{
return fabs(y - b.y) <= ect ? (x < b.x) : (y < b.y);
}
inline vec operator /(double b)
{
return vec(x / b, y / b);
}
inline vec operator *(double a)
{
return vec(a * x, a * y);
}
inline bool para(vec a, vec b)
{
return fabs(a % b) <= ect;
}
inline bool toleft(vec a, vec b)
{
return b % a > ect;
}
inline void out(vec a, char c = ' ')
{
printf("(%.3lf,%.3lf)%c", a.x, a.y, c);
}
} vec, point;
<后记>
暂时整这么多。
我还有圆方树的博客没补。
对了我博客园的博客是一直在写的。
欢迎前往支持。