语法 / STL 热知识
Bolaris0717 · · 个人记录
Mod.
白科技!!
建议配合 Ctrl + F 食用。
踩坑
123
lower_bound/upper_bound
- lower_bound(l,r,w,cmp):找到 [l,r) 首个使 cmp(*it,w) 为 false 的 it。需要保证所有 true 在 false 之前。
- upper_bound(l,r,w,cmp):找到 [l,r) 首个使 cmp(w,*it) 为 true 的 it。需要保证所有 false 在 true 之前。
cmp 的默认传参为 less。
vector
push_back 2e6.
set
cache 命中得可怜。
setprecision
注意 0.5 是不进一的,要严格大于 0.5 才会。
deque
deque 是加强的 vector?
初始化时一般创建八倍元素类型大小空间
swap()
注意 swap 两个数组复杂度是数组大小,但是 swap 两个 vector 是 O(1) 的。
find(l,r,x)
找到区间
复杂度线性。
__lg(x)
返回
复杂度
minmax(a,b)
返回 pair,first 是 min,second 是 max。
min_element / max_element / minmax_element
返回对应极值的位置。
for_each
顾名思义。
nth_element(l,mid,r)
区间第 k-=1,nth_element(begin(a),begin(a)+k,end(a)),cout<<a[k]<<'\n';
将
复杂度
stable_sort
稳定的 sort。
is_sorted
是否不降。
rotate
循环轮转。
rotate(l,mid,r) 将
复杂度
fill
区间赋值。
merge / inplace_merge
end=merge(l1,r1,l2,r2,begin)
inplace_merge(l,mid,r)
归并排序。
count / count_if
计数器。
transform
个人:
hypot
hypot(x,y)
functional
plus<>;//x+y
minus<>;//x-y
multiplies<>;//x*y
divides<>;//x/y
modulus<>;//x%y
negate<>;//-x
equal_to<>;//x==y
not_equal_to<>;//x!=y
greater<>;//x>y
less<>;//x<y
greater_equal<>;//x>=y
less_equal<>;//x<=y
logical_and<>;//x&&y
logical_or<>;//x||y
logical_not<>;//!x
bit_and<>;//x&y
bit_or<>;//x|y
bit_xor<>;//x^y
bit_not;//~x
__builtin_xxx / __builtin_xxxll
-
__builtin_popcount:返回1 的个数。 -
__builtin_parity:返回1 的个数奇偶性。 -
__builtin_ffs:最后一个 1 是从低往高第几位,是lg(lowbit)+1。 -
__builtin_sqrt:开平方,没有取整。 -
__builtin_ctz:后导0 个数,要求x \neq 0 。 -
__builtin_clz:前导0 个数,要求x \neq 0 。 -
__builtin_clrsb:最长同色前缀长度-1 。
iota
生成连续的数。
partition
将一个范围内的元素划分为两个部分,给定一元谓词为 True 的元素在前面,其他在后面。
is_partitioned
检查一个范围内的元素是否已经被划分为两个连续部分,第一部分满足特定的一元谓词,第二部分不满足一元谓词。
partition_point
在
string.append
string.append(str) 等价于 +=str
string.append(str,ind,len) 添加 str 从 ind 开始的 len 个元素
string.append(x,c) 添加
string.append(l,r) 添加
string.replace
string.replace(ind,len,str,Ind=0,Len=str.size()),将
str,Ind,Len 亦可以替换为 c,x。
string.find/rfind
str,ind.
find 从前往后匹配,返回第一个匹配串左端点,找不到返回
rfind 从后往前匹配,返回第一个匹配串右端点,找不到返回
string.find_first_not_of / find_last_not_of
找不匹配的。
string.substr
ind,len
但是 string 几个成员函数复杂度都是和原字符串长有关的。-.-!
all_of / any_of
l,r,F
& 和, | 和。
find_if | find_not
找到一个满足 / 不满足谓词的指针。
注意这个是线性的,不是二分。
equal_range
adjacent_find
改 appendix.html 为 convolution.html 即可看到英文版 https://atcoder.github.io/ac-library/production/document_en/convolution.html
dsu
-
dsu d(int n)
-
int d.merge(int a, int b)
-
bool d.same(int a, int b)
-
int d.leader(int a)
-
int d.size(int a)
-
vector<vector<int>> d.groups()
fenwicktree
-
fenwick_tree<T> fw(int n)
- T 只能是整数
-
void fw.add(int p, T x)
- a[p] += x
-
T fw.sum(int l, int r)
- [l, r)
lazysegtree
\dag https://atcoder.github.io/ac-library/production/document_en/lazysegtree.html
math
- [l, r)
-
ll pow_mod(ll x, ll n, int m)
- 快速幂
x^n \bmod m -
0 \le n, 1 \le m
- 快速幂
-
ll inv_mod(ll x, ll m)
-
\frac 1x \bmod m -
(x, m) = 1, 1 \le m -
O(\log m)
-
-
pair<ll, ll> crt(vector<ll> r, vector<ll> m)
\dag -
ll floor_sum(ll n, ll m, ll a, ll b)
\dag -
https://atcoder.github.io/ac-library/production/document_en/math.html
maxflow \dag
https://atcoder.github.io/ac-library/production/document_en/maxflow.html
mincostflow \dag
https://atcoder.github.io/ac-library/production/document_en/mincostflow.html
modint
就是 Mi. https://atcoder.github.io/ac-library/production/document_en/modint.html
scc \dag
https://atcoder.github.io/ac-library/production/document_en/scc.html
segtree \dag
https://atcoder.github.io/ac-library/production/document_en/segtree.html
string \dag
有 SA 和 Z 算法 https://atcoder.github.io/ac-library/production/document_en/string.html
twosat \dag
https://atcoder.github.io/ac-library/production/document_en/twosat.html