白科技!!
建议配合 Ctrl + F 食用。
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
。
partial_sum / adjacent_difference
好像都是左闭右闭的,不牛。前缀和 / 差分
个人:,,这样就都是左开右闭,很统一,美中不足的是,要多开 , 没有用,可以用来记录 。
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:返回 的个数。 -
__builtin_parity:返回 的个数奇偶性。 -
__builtin_ffs:最后一个 1 是从低往高第几位,是lg(lowbit)+1。 -
__builtin_sqrt:开平方,没有取整。 -
__builtin_ctz:后导 个数,要求 。 -
__builtin_clz:前导 个数,要求 。 -
__builtin_clrsb:最长同色前缀长度 。
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) 添加 的元素(强制转成 char)
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
中最左的相邻相同位置,即查找 2 个连续相等
remove / remove_if / replace / replace_if
删除 / 替换。
注意替换是先传 old 再传 new
swap_ranges
swap,(l1,r1,l2)
binary_search
是否存在 。
要求升序,复杂度 。
boolalpha
islower, isupper, isdigit, isalpha, isalnum, toupper, tolower
stringstream
结合 ofstream 批量造数据。
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 的排名(是小于 的值的个数) find_by_order(k): 第 k 大的指针 join(x): 把 x 合并进来,并删除 x split(val,t): 保留 val 的部分, val 的放在 b 里
pair_heap
__gnu_pbds::priority_queue<int>q;
attribute
__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 为单位。
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
改 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
https://atcoder.github.io/ac-library/production/document_en/lazysegtree.html
math
- ll pow_mod(ll x, ll n, int m)
- 快速幂
- ll inv_mod(ll x, ll m)
- pair<ll, ll> crt(vector<ll> r, vector<ll> m)
- ll floor_sum(ll n, ll m, ll a, ll b)
- https://atcoder.github.io/ac-library/production/document_en/math.html
maxflow
https://atcoder.github.io/ac-library/production/document_en/maxflow.html
mincostflow
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
https://atcoder.github.io/ac-library/production/document_en/scc.html
segtree
https://atcoder.github.io/ac-library/production/document_en/segtree.html
string
有 SA 和 Z 算法 https://atcoder.github.io/ac-library/production/document_en/string.html
twosat
https://atcoder.github.io/ac-library/production/document_en/twosat.html