模板库 ~ Template library

已精炼 .

bashrc
  • title x:设置标题为 x
  • compile x:编译 x.cpp 并写入到可执行文件 x
  • compilerun xcompile x 并运行 x

基于 NOI Linux 相关内容 . 有常用 alias .

case $- in
    *i*) ;;
      *) return;;
esac
HISTCONTROL=ignoreboth
shopt -s histappend
HISTSIZE=1000
HISTFILESIZE=2000
shopt -s checkwinsize
# shopt -s globstar
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi
case "$TERM" in
    xterm-color|*-256color) color_prompt=yes;;
esac
# force_color_prompt=yes
if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
	color_prompt=yes
    else
	color_prompt=
    fi
fi
if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    alias dir='dir --color=auto'
    alias vdir='vdir --color=auto'
    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi
# export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
# sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi
[ -r /home/hzoi/.byobu/prompt ] && . /home/hzoi/.byobu/prompt   #byobu-prompt#

# ------------------------------ ABOUT OI ------------------------------

function title() {
  if [[ -z "$ORIG" ]]; then
    ORIG=$PS1
  fi
  TITLE="\[\e]2;$*\a\]"
  PS1=${ORIG}${TITLE}
}
compile() {
  g++ $1.cpp -lm -O2 -Wall -Wextra -fconcepts -std=c++14 -Wno-unused-result -fsanitize=address,undefined -o $1
}
compilefst() {
  g++ $1.cpp -lm -O2 -Wall -Wextra -fconcepts -std=c++14 -Wno-unused-result -o $1
}
compilerun() {
  compile $1 && time ./$1
}
compilerunfst() {
  compilefst $1
  time ./$1
}
fc() {
  diff -BZbs --color=always --strip-trailing-cr --suppress-common-lines $1 $2
}
alias cr='compilerun'
alias crf='compilerunfst'
alias cls='clear'
alias clr='clear'
alias hc='history -c'
alias hcs='clear && history -c'
alias python='python3'
alias calc='gnome-calculator'
alias screenshot='gnome-screenshot'

相关 Bash 命令:

  • source ~/.bashrc:快速应用 bashrc
  • alias x=y:设置 y 的别名为 x,等号没有空格 .
  • unalias x:取消 x 这个别名
vimrc

注意插入前先按 i!!

https://www.cnblogs.com/fushao2yyj/p/8404511.htmltabstop 改成 4 .

nanorc

注意反人类 nano 的快捷键

nano 快捷键
  • 保存:Ctrl + O 保存 .
  • 复制一整行:Alt + 6
  • 剪贴一整行:Ctrl + K
  • 复制/剪贴多行或者一行中的一部分:先将光标移动到需要复制/剪贴的文本的开头,按 Ctrl + 6Alt + A 做标记,然后移动光标到待复制/剪贴的文本末尾 . 这时选定的文本会反白,然后按复制一行的方法复制 . Ctrl + 6 取消 .
  • 粘贴:Ctrl + U
  • 搜索:Ctrl + W 然后输入搜索单词,Alt + W 跳转下一个
  • 退出:Ctrl + X,然后输入 Y 表示保存修改,N 不保存 . Ctrl + C 取消 .

Ref. https://zhuanlan.zhihu.com/p/259380222 .

set tabsize 4       # 设置制表符宽度
set autoindent      # 允许自动缩进
set cut             # 设置 CTRL-K 可以剪贴到行末
set noconvert       # 不要转换 DOS/UNIX 换行符
set nowrap          # 不要自动换行
set nohelp          # 不显示下面两行帮助 (Alt+X 显示)
set morespace       # 隐藏标题下的空白行,换取更多编辑空间
set smooth          # 平滑卷屏
set suspend         # 允许 ctrl-z 将 nano 置于后台
set smarthome       # 第一次 Home 跳到行首非空字符,第二次到行首
set tabstospaces    # 展开制表符为空格(如果需要的话)
set mouse           # 允许鼠标
# set linenumbers     # 显示行号(可以在编辑时 ALT-# 切换),NOI Linux 1.0 的 nano 太旧了不支持!
set backupdir path  # 设置备份路径
set backup          # 允许保存备份
set casesensitive   # 搜索使用大小写敏感
set multibuffer     # 使用 CTRL-r 读取文件时,默认读取到新缓存
set nonewlines      # 不在文件末尾添加新行
pool (Mixed)

接口:

构建 pool : T -> unsigned 的映射 .

\(M\)pool<T> 类型变量,满足若 \(x\neq y\),则 \(\operatorname{get}(x)\neq\operatorname{get}(y)\) .

template<typename T>
class pool
{
	unordered_map<T, unsigned> pol;
	unsigned cc;
public:
	pool(){cc = 0;}
	~pool() = default;
	unsigned get(T x)
	{
		auto ptr = pol.find(x);
		if (ptr == pol.end()){pol[x] = ++cc; return cc;}
		else return ptr -> second;
	}
	unsigned size() const {return cc;} 
	void clear(){cc = 0; pol.clear();}
};
光速幂(底数不变 O(1) 求幂)
// 值域 2^31-1
template <const int MOD>
struct FastPow
{
private:
	int p1[66666], p2[66666];
	static int qpow(int a, int n){/**/}
public:
	explicit FastPow(int a)
	{
		ll t1 = a, t2 = qpow(t1,65536); p1[0] = p2[0] = 1;
		for (int i=1; i<65536; i++) p1[i] = 1ll * p1[i-1] * t1 % MOD;
		for (int i=1; i<65536; i++) p2[i] = 1ll * p2[i-1] * t2 % MOD;
	}
	int operator()(int n) const {return 1ll * p2[n>>16] % MOD * p1[n&65535] % MOD;}
};
取模还原分数 (by Elegia)

https://blog.csdn.net/EI_Captain/article/details/117172239

pair<int, int> approx(int p, int q, int A) {
  int x = q, y = p, a = 1, b = 0;
  while (x > A) {
	swap(x, y); swap(a, b);
	a -= x / y * b;
	x %= y;
  }
  return make_pair(x, a);
}
传引用的 exgcd
ll exgcd(ll a, ll b, ll& x, ll& y)
{
	if (!b){x = 1; y = 0; return a;}
	ll _ = exgcd(b, a%b, x, y), old_x = x, old_y = y;
	x = old_y; y = old_x - (a/b) * old_y;
	return _;
}
线性筛素数、欧拉函数、莫比乌斯函数
inline void linear_sieve(int n)
{
	notprime[1] = true; phi[1] = mu[1] = 1;
	for (int i=2; i<=n; i++)
	{
		if (!notprime[i]){plist.emplace_back(i); phi[i] = i-1; mu[i] = -1;}
		for (auto x : plist)
		{
			int now = i*x;
			if (now > n) break;
			notprime[now] = true;
			if (!(i%x)){phi[now] = phi[i] * x; mu[now] = 0; break;}
			phi[now] = phi[i] * phi[x]; mu[now] = mu[i] * mu[x];
		}
	}
}
Simple Graph
template <typename T>
struct SimpleGraph
{
	typedef vector<pair<int, T> > _;
	_ g[N];
	inline void addedge(int u, int v, T w){g[u].emplace_back(make_pair(v, w));}
	inline void ade(int u, int v, T w){addedge(u, v, w); addedge(v, u, w);}
	inline _ operator[](const int& id) const {return g[id];}
	inline _& operator[](const int& id){return g[id];}
	inline void clear(int n = N - 1){for (int i=0; i<=n; i++) g[i].clear();}
};
Future Graph
template <typename T>
struct FutureGraph
{
	typedef pair<int, pair<int, T> > pit;
	int id;
	vector<pit> g[N];
	inline void addedge(int u, int v, T w){g[u].emplace_back(make_pair(id++, make_pair(v, w)));}
	inline void ade(int u, int v, T w){addedge(u, v, w); addedge(v, u, w);}
	inline vector<pit> operator[](const int& id) const {return g[id];}
	inline vector<pit>& operator[](const int& id){return g[id];}
	inline static int rev(int id){return id ^ 1;}
	inline void clear(int n = N - 1){id = 0; for (int i=0; i<=n; i++) g[i].clear();}
	FutureGraph(){id = 0;}
};
树剖求 LCA
namespace LCA
{
	int fa[N], dep[N], siz[N], son[N], top[N], dfn[N], rnk[N], cc;
	void dfs1(int u)
	{
		siz[u] = 1;
		for (auto v : g[u])
		{
			if (fa[u] == v) continue;
			dep[v] = dep[u] + 1;
			fa[v] = u;
			dfs1(v);
			siz[u] += siz[v];
			if (!son[u]|| (siz[v] > siz[son[u]])) son[u] = v;
		}
	}
	void dfs2(int u, int t)
	{
		top[u] = t;
		rnk[++cc] = u; dfn[u] = cc;
		if (!son[u]) return ;
		dfs2(son[u], t);
		for (auto v : g[u])
			if ((v != son[u]) && (v != fa[u])) dfs2(v, v);
	}
	int lca(int u, int v)
	{
		while (top[u] != top[v])
		{
			if (dep[top[u]] > dep[top[v]]) u = fa[top[u]];
			else v = fa[top[v]];
		}
		return dep[u] > dep[v] ? v : u;
	}
}
KMP 字符串匹配
typedef char str[N];
str s1, s2;
int l1, l2, nxt[N], f[N];
void getnxt()
{
	nxt[1] = 0; int j = 0;
	for (int i=2; i<=l2; i++)
	{
		while (j && (s2[i] != s2[j+1])) j = nxt[j];
		if (s2[i] == s2[j+1]) ++j;
		nxt[i] = j;
	}
}
void kmp()
{
	int j = 0;
	for (int i=1; i<=l1; i++)
	{
		while ((j==l2) || (j && (s1[i] != s2[j+1]))) j = nxt[j];
		if (s1[i] == s2[j+1]) ++j;
		f[i] = j;
	}
}
AC Automaton
// 此处字符集为小写字母('a' ~ 'z')
const int N = 1e6 + 50, alphabet= 26; 
struct ACAM
{
	int tr[N][alphabet], fail[N], mark[N], cc, root;
	inline void insert(string s)
	{
		int u = root, l = s.length();
		for (int i=0; i<l; i++)
		{
			if (!tr[u][s[i] - 'a']) tr[u][s[i] - 'a'] = ++cc;
			u = tr[u][s[i] - 'a'];
		} ++mark[u];
	}
	inline void build()
	{
		queue<int> q; fail[root] = root;
		for (int i=0; i<alphabet; i++)
		{
			if (tr[root][i]){q.push(tr[root][i]); fail[tr[root][i]] = root;}
			else tr[root][i] = root;
		}
		while (!q.empty())
		{
			int u = q.front(); q.pop();
			for (int i=0; i<alphabet; i++)
			{
				if (tr[u][i]){fail[tr[u][i]] = tr[fail[u]][i]; q.push(tr[u][i]);}
				else tr[u][i] = tr[fail[u]][i];
			}
		}
	}
	inline int query(string s)
	{
		int u = root, ans = 0, l = s.length();
		for (int i=0; i<l; i++)
		{
			u = tr[u][s[i] - 'a'];
			for (int j = u; j && ~mark[j]; j = fail[j]){ans += mark[j]; mark[j] = -1;}
		} return ans;
	}
	inline void clear(){memset(tr, 0, sizeof tr); memset(mark, 0, sizeof mark); cc = 0;}
	AC(){root = cc = 0;}
}ac;
ST 表
// 传入一个 Functor 给 op,要求:
// 1. op(x, x) = x                      (消去律)
// 2. op(x, op(y, z)) = op(op(x, y), z) (结合律)
template<typename T, typename op>
struct ST
{
	typedef unsigned U;
	T f[N][_];
	template <typename rit>
	inline void reset(rit fst, rit sec)
	{
		const U n = sec - fst;
		for (U i=1; i<=n; i++) f[i][0] = *(fst + (i-1));
		for (U j=1; (1u<<j)<=n; j++)
			for (U i=1; i+(1u<<j)-1u<=n; i++) f[i][j] = op()(f[i][j-1], f[i + (1<<(j-1))][j-1]);
	}
	inline T query(U l, U r)const{U _ = __lg(r-l+1); return op()(f[l][_], f[r-(1<<_)+1][_]);}
	template <typename rit>
	ST(rit fst, rit sec){reset(fst, sec);}
	ST()  = default;
	~ST() = default;
};
ST 表,但是查询最值位置
// 传入一个 Functor 给 op,要求:
// 1. op(x, x) = x                      (消去律)
// 2. op(x, op(y, z)) = op(op(x, y), z) (结合律)
template<typename T, typename op>
struct ST
{
	typedef unsigned U;
	T f[N][_]; U pos[N][_];
	template <typename rit>
	inline void reset(rit fst, rit sec)
	{
		const U n = sec - fst;
		for (U i=1; i<=n; i++){f[i][0] = *(fst + (i-1)); pos[i][0] = i;}
		for (U j=1; (1u<<j)<=n; j++)
			for (U i=1; i+(1u<<j)-1u<=n; i++)
			{
				if (op()(f[i][j-1], f[i+(1<<(j-1))][j-1])){f[i][j] = f[i][j-1]; pos[i][j] = pos[i][j-1];}
				else{f[i][j] = f[i+(1<<(j-1))][j-1]; pos[i][j] = pos[i+(1<<(j-1))][j-1];}
			}
	}
	inline T query(U l, U r)const{U _ = __lg(r-l+1); return op()(f[l][_], f[r-(1<<_)+1][_]);}
	inline U querypos(U l, U r)const{U _ = __lg(r-l+1); return op()(f[l][_], f[r-(1<<_)+1][_]) ? pos[l][_] : pos[r-(1<<_)+1][_];}
	template <typename rit>
	ST(rit fst, rit sec){reset(fst, sec);}
	ST()  = default;
	~ST() = default;
};
ST 表,但是查询最值位置(偏序 ver.)
template <typename T, typename op = less<T> >
struct ST
{
	static const int LgN = __lg(N) + 10;
	typedef unsigned U;
	typedef pair<T, U> ptu;
	ptu f[N][_]; 
	bool compare(const ptu& A, const ptu& B){return op()(A.first, B.first);}
	ptu min(const ptu& A, const ptu& B){return compare(A, B) ? A : B;} 
	template <typename _ptr>
	inline void reset(_ptr fst, _ptr edp)
	{
		int n = 0;
		for (int i=1; fst!=edp; i++){f[i][0] = make_pair(*fst, i); ++fst; ++n;}
		for (int j=1; (1<<j)<=n; j++)
			for (int i=1; i+(1<<(j-1))-1<=n; i++)
				f[i][j] = min(f[i][j-1], f[i+(1<<(j-1))][j-1]);
	}
	inline ptu querybase(U l, U r){++l; ++r; int _ = __lg(r-l+1); return min(f[l][_], f[r-(1<<_)+1][_]);}
	inline T query(U l, U r){return querybase(l, r).first;}
	inline U querypos(U l, U r){return querybase(l, r).second - 1;}
	ST()  = default;
	~ST() = default;
	template <typename _ptr>
	ST(_ptr a, _ptr b){reset(a, b);}
	template <typename _contain>
	ST(_contain a){reset(a.begin(), a.end());}
};=
posted @ 2022-04-12 11:22  Jijidawang  阅读(105)  评论(0编辑  收藏  举报
😅​