语法 / STL 热知识

· · 个人记录

Mod.

白科技!!

建议配合 Ctrl + F 食用。

踩坑

123

lower_bound/upper_bound

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)

找到区间 [l,r) 内第一个等于 x 的数。

复杂度线性。

__lg(x)

返回 \lfloor \log_2 n \rfloor 。

复杂度 \mathcal{O(1)}。

minmax(a,b)

返回 pair,first 是 min,second 是 max。

min_element / max_element / minmax_element

返回对应极值的位置。

for_each

顾名思义。

nth_element(l,mid,r)

区间第 k 小:k-=1,nth_element(begin(a),begin(a)+k,end(a)),cout<<a[k]<<'\n';

将 [l,r) 重排序,使得 [l,mid) 中不超过 mid,(mid,r) 不少于 mid。

复杂度 \mathcal{O(n)}。

stable_sort

稳定的 sort。

is_sorted

是否不降。

rotate

循环轮转。

rotate(l,mid,r) 将 [l,r) 转到 mid 指向 l。

复杂度 \mathcal{O(n)}。

fill

区间赋值。

merge / inplace_merge

end=merge(l1,r1,l2,r2,begin)

inplace_merge(l,mid,r)

归并排序。

count / count_if

计数器。

transform

# partial_sum / adjacent_difference 好像都是左闭右闭的,不牛。前缀和 $s_i = s_{i - 1} + a_i$ / 差分 $d_i = a_i - a_{i - 1}

个人:s_{i + 1} = s_i + a_i,d_i + a_i = a_{i + 1},这样就都是左开右闭,很统一,美中不足的是,要多开 s_n,d_{n-1} 没有用,可以用来记录 a_0。

hypot

hypot(x,y)=\sqrt{x^2+y^2}。

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

iota

生成连续的数。

partition

将一个范围内的元素划分为两个部分,给定一元谓词为 True 的元素在前面,其他在后面。

is_partitioned

检查一个范围内的元素是否已经被划分为两个连续部分,第一部分满足特定的一元谓词,第二部分不满足一元谓词。

partition_point

在 [l,r) 中二分出第一个不满足该谓词的元素,要保证单调性。

string.append

string.append(str) 等价于 +=str

string.append(str,ind,len) 添加 str 从 ind 开始的 len 个元素

string.append(x,c) 添加 x 个 c

string.append(l,r) 添加 [l,r) 的元素(强制转成 char)

string.replace

string.replace(ind,len,str,Ind=0,Len=str.size()),将 [ind,ind+len) 替换为 str[Ind,Ind+Len)。

str,Ind,Len 亦可以替换为 c,x。

string.find/rfind

str,ind.

find 从前往后匹配,返回第一个匹配串左端点,找不到返回 -1。
rfind 从后往前匹配,返回第一个匹配串右端点,找不到返回 -1。

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

要求升序。复杂度 $\log

adjacent_find

# remove / remove_if / replace / replace_if 删除 / 替换。 注意替换是先传 old 再传 new # swap_ranges swap,(l1,r1,l2) # binary_search $[l,r)$ 是否存在 $x$。 要求升序,复杂度 $\log$。 # boolalpha islower, isupper, isdigit, isalpha, isalnum, toupper, tolower # stringstream 结合 ofstream 批量造数据。 ```cpp ofstream cout; void File(int T,string s) { stringstream is; is<<s<<"_"<<setw(3)<<setfill('0')<<T<<".in"; cout.close(); cerr<<is.str()<<'\n'; cout.open(is.str()); } void Solve() { return; } void main() { ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int l=7,r=17; for(int i=l;i<r;++i) { File(i,"BK0717"),Solve(); } } ``` # STL pbds #include<bits/extc++.h> using namespace __gnu_pbds; ## 红黑树 tree<Info1,Info2,less<>,rb_tree_tag,tree_order_statistics_node_update>t; // Info1 -> Info2(null_type) 成员函数: find, insert(返回 pair<it,bool>), erase, lower_bound, upper_bound 同 set; order_of_key(w): 找到键值 w 的排名(是小于 $w$ 的值的个数) find_by_order(k): 第 k 大的指针 join(x): 把 x 合并进来,并删除 x split(val,t): 保留 $\le$ val 的部分,$>$ val 的放在 b 里 ## pair_heap __gnu_pbds::priority_queue<int>q; # attribute ```cpp __attribute__((constructor)) void before_main() { clg("--- ",__func__,'\n'); } __attribute__((destructor)) void after_main() { clg("--- ",__func__,'\n'); } struct __attribute__((packed)) Info { int x; LL y; }; ``` # ws / noskipws / skipws 从输入流舍弃前导空白符。 noskipws:保留前导字符的输入。 # setbase setbase(8/10/16) 按对应的进制输入。 # showpos / noshowpos 启用 / 关闭 输出非负整数的 `+`。 # exchange exchange(a,b):以 b 替换 a 的值,并返回 a 的旧值。 # chrono 更好的测时工具。 chrono::high_resolution_clock::now():返回一个 timepoint。 std::chrono::duration<double>.count():输出以 s 为单位。 ```cpp struct { chrono::time_point<chrono::high_resolution_clock>S,T; void operator~() { S=chrono::high_resolution_clock::now(); } void operator-() { T=chrono::high_resolution_clock::now(); } void operator!() { cerr<<std::chrono::duration<double>{T-S}.count()<<"s\n"; } }Time; ~Time,-Time,!Time; ``` # quoted `quoted(s,c='"')`:输出 c+s+c # gprof gcc 编译的时候加个参数 -pg,然后运行程序,正常的话会产生一个gprof.out,然后 gprof ./xx。 待验证。 # ac library 拉伸二象性 中文:luogu.com.cn/article/xxa80t4t 英文:https://atcoder.github.io/ac-library/production/document_en/appendix.html 默认都是 0-index,左闭右开 功能、要求、复杂度 ## convolution $\dag

改 appendix.html 为 convolution.html 即可看到英文版 https://atcoder.github.io/ac-library/production/document_en/convolution.html

dsu

fenwicktree

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

QwQ。