STL
WUYONGWEI2011 · · 算法·理论
概念
Standard Template Library,标准模板库,是C++自带的一套模板库。
迭代器
在STL中,迭代器(Iterator)用来访问和检查STL容器中元素的对象。
迭代器本身可以看作一个数据指针.主要支持自增(++)和解引用(*),其中自增用来移动迭代器,解引用可以获取或修改它指向的元素。
指向某个STL容器container中元素的迭代器的类型一般为container::iterator(比赛中支持使用auto)。
相关函数
C++11后可使用:
next(it)获得前向迭代器it的后继,next(it, n)获得前向迭代器it的第n个后继。
prev(it)获得双向迭代器it的前驱,prev(it, n)获得双向迭代器it的第n个前驱。
STL容器
分类
序列式容器
- 向量(vector):后端可高效增加元素的顺序表。
- 数组(array):C++11,定长的顺序表。
- 双端队列(deque) :双端都可高效增加元素的顺序表。
- 列表(list):可以沿双向遍历的链表。
- 单向列表(forward_list):只能沿一个方向遍历的链表。
关联式容器
- 集合(set):用以有序地存储互异元素的容器,红黑树实现。
- 多重集合(multiset):用以有序地存储元素的容器,允许存在相等的元素。
- 映射(map):由{键,值}对组成的集合。
- 多重映射(multimap):由{键,值}对组成的多重集合,亦即允许键有相等情况的映射。
无序(关联式)容器
此类容器有常数但较大的时间复杂度。
- 无序集合(unordered_set/unordered_multiset):C++11,与set/multiset的区别在于元素无序,只关心「元素是否存在」,使用哈希实现。
- 无序映射(unordered_map/unordered_multimap):C++11,与map/multimap的区别在于键无序,使用哈希实现。
容器适配器
- 栈(stack):后进先出(LIFO)的容器。
- 队列(queue):先进先出(FIFO)的容器。
- 优先队列(priority_queue):元素的次序是由作用于所存储的值对上的某种谓词决定的一种队列。
共有函数
- begin():返回指向开头元素的迭代器。
- end():返回指向末尾的下一个元素的迭代器。
- size():返回容器内的元素个数。
- max_size():返回容器理论上能存储的最大元素个数。
- empty():返回容器是否为空。
- swap():交换两个容器,如
swap(a,b),时间O(1)。 - clear():清空容器。
其他容器函数
deque:
front()返回首元素的引用。back()返回末尾元素的引用。push_front()在头部插入一个元素,常数复杂度。pop_front()删除头部元素,常数复杂度。push_back()在末尾插入一个元素,常数复杂度。pop_back()删除末尾元素,常数复杂度。
set/muiltset:
insert(x)将元素x插入到set/multiset中。erase(x)删除值为x的所有元素,返回删除元素的个数。erase(pos)删除迭代器为pos的元素。erase(first,last)删除迭代器在[first,last)范围内的所有元素。begin()返回指向首元素的迭代器。end()返回指向数组尾端占位符的迭代器,注意是没有元素的。lower_bound(x)/upper_bound(x)返回指向首个不小于/大于x的元素的迭代器。如果不存在这样的元素,返回end()。count(x)返回set/multiset内键为 x 的元素数量.find(x)在set/multiset内存在键为x的元素时会返回该元素的迭代器,否则返回end()。
map/multimap:
erase(key):函数会删除键为key的所有元素。返回值为删除元素的数量。erase(pos):删除迭代器为pos的元素。erase(first,last):删除迭代器在[first,last)范围内的所有元素。count(x):返回容器内键为x的元素数量。复杂度为O(\log(size) +ans)。find(x):若容器内存在键为x的元素,会返回该元素的迭代器;否则返回end()。lower_bound(x):返回指向首个不小于给定键的元素的迭代器。upper_bound(x): 返回指向首个大于给定键的元素的迭代器.若容器内所有元素均小于或等于给定键,返回 end()。
stack:
-
top()访问栈顶元素。STL算法
-
find:顺序查找,如
find(v.begin(),v.end(),value)。 -
reverse:翻转数组、字符串,如
reverse(v.begin(), v.end())或reverse(a + begin, a + end)。 -
unique:去除容器中相邻的重复元素,如
unique(ForwardIterator first, ForwardIterator last),返回值为指向去重后容器结尾的迭代器,原容器大小不变。 -
shuffle:随机地打乱数组,如
shuffle(v.begin(),v.end(),rand())。 -
sort:排序,如
sort(v.begin(),v.end(),cmp)或sort(a+begin,a+end,cmp)。 -
nth_element:按指定范围进行分类,即找出序列中第n大的元素,如
nth_element(v.begin(),v.begin()+n,v.end(),cmp)或nth_element(a+begin,a+begin+n,a+end,cmp)。 -
binary_search:二分查找,如
binary_search(v.begin(), v.end(), value)。 -
merge:将两个(已排序的)序列有序合并到第三个序列的插入迭代器上,如
merge(v1.begin(), v1.end(), v2.begin(), v2.end() ,back_inserter(v3))。 -
inplace_merge:将两个(已按小于运算符排序的)[first,middle), [middle,last) 范围 原地合并为一个有序序列,如
inplace_merge(v.begin(), v.begin() + middle, v.end())。 -
lower_bound/upper_bound:在一个有序序列中进行二分查找,返回指向第一个大于等于/大于x的元素的位置的迭代器。如果不存在这样的元素,则返回尾迭代器,如
lower_bound(v.begin(),v.end(),x)。 -
next_permutation:将当前排列更改为 全排列中的下一个排列.如果当前排列已经是 全排列中的最后一个排列(元素完全从大到小排列),函数返回false并将排列更改为全排列中的第一个排列(元素完全从小到大排列);否则,函数返回 true,如
next_permutation(v.begin(), v.end())或next_permutation(v + begin, v + end)。 -
prev_permutation:将当前排列更改为全排列中的上一个排列,用法同next_permutation。
-
partial_sum:求前缀和,如
partial_sum(src.begin(), src.end(), back_inserter(dst))。题单
2026.7.7 ::::info[B]
#include<bits/stdc++.h> #define int long long const int N=2e5+5; using namespace std; int t,n,a[N],b[N],maxn,sum; set<pair<int,int> >s; signed main(){ ios::sync_with_stdio(false); //cin.tie(0); cin>>t; set<pair<int,int> >::iterator p; pair<int,int>k; while(t--){ cin>>n; for(int i=1;i<=n;i++)cin>>a[i]; for(int i=1;i<=n;i++)cin>>b[i]; s.clear(),maxn=sum=0; for(int i=1;i<=n;i++){ sum+=a[i]-b[i]; p=s.lower_bound({b[i],a[i]}); while(p!=s.end()){ k=*p; if(k.first>a[i])break; a[i]=max(a[i],k.second); s.erase(p); p=s.lower_bound({b[i],a[i]}); } p=s.lower_bound({b[i],a[i]}); cout<<k.first<<" "<<k.second<<"\n"; while(1){ p--,k=*p; cout<<k.first<<" "<<k.second<<"\n"; if(k.first==0||k.second<b[i])break; b[i]=min(b[i],k.first); if(p==s.begin()){s.erase(p);break;} s.erase(p); p=s.lower_bound({b[i],a[i]}); } maxn=max(maxn,a[i]-b[i]); s.insert({b[i],a[i]}); cout<<sum-maxn<<"\n"; } cout<<"\n"; } return 0; }:::: ::::info[C]
#include<bits/stdc++.h> #define int long long const int N=1e6+5; using namespace std; int n,q,a[N],x,h,cnt1,cnt2,ans; set<int>s1,s2; signed main(){ ios::sync_with_stdio(false); cin.tie(0); cin>>n>>q; ans=n*(n-1)/2; for(int i=1;i<=n;i++){ cin>>a[i]; if(a[i]==1){ if(cnt1!=0)ans-=(cnt1*(cnt1-1))/2; cnt1=0,cnt2++;s1.insert(i); }else cnt1++; if(a[i]==2){ if(cnt2!=0)ans-=(cnt2*(cnt2-1))/2; cnt2=0;s2.insert(i); } } if(cnt1!=0)ans-=(cnt1*(cnt1-1))/2; if(cnt2!=0)ans-=(cnt2*(cnt2-1))/2; set<int>::iterator k; for(int i=1;i<=q;i++){ cin>>x>>h; if(a[x]==1){ if(h==2){ k=s2.lower_bound(x); }else{ } }else if(a[x]==2){ if(h==1){ }else{ } } cout<<ans<<"\n"; } return 0; }:::: ::::info[D]
#include<bits/stdc++.h> using namespace std; int t,n,k,op,x,ans,cnt1,cnt2; int main(){ ios::sync_with_stdio(false); cin.tie(0); cin>>t; while(t--){ cin>>n>>k; for(int i=1;i<=n;i++){ cin>>op>>x; if(op==1)cnt1+=x; else if(op==2)cnt2+=x; else{ //反悔贪心 } } } return 0; }:::: ::::info[E]
#include<bits/stdc++.h> #define int long long const int N=1e5+5; using namespace std; int n,k,b[N*2],c[N],d[N],ans,cnt,anss; struct home{ int s,t,sum1,sum2,num1,num2; operator<(const home &A)const{ return s+t<A.s+A.t; } }a[N]; char ch1,ch2; priority_queue<int>p; priority_queue<int,vector<int>,greater<int> >q; signed main(){ ios::sync_with_stdio(false); cin.tie(0); cin>>k>>n; if(k==1){ cnt=n*2; for(int i=1;i<=cnt;i+=2){ cin>>ch1>>b[i]>>ch2>>b[i+1]; if(ch1==ch2)ans+=abs(b[i+1]-b[i]),i--,cnt-=2; } sort(b+1,b+cnt+1); int k=(b[cnt/2]+b[cnt/2+1])/2; for(int i=1;i<=cnt;i++)ans+=abs(b[i]-k); cout<<ans+cnt/2<<"\n"; }else{ cnt=n; for(int i=1;i<=cnt;i++){ cin>>ch1>>a[i].s>>ch2>>a[i].t; if(ch1==ch2)ans+=abs(a[i].t-a[i].s),i--,cnt--; } sort(a+1,a+cnt+1); for(int i=1;i<=cnt;i++){ a[i].sum1=a[i-1].sum1+a[i].s+a[i].t; p.push(a[i].s),p.push(a[i].t); a[i].sum2=a[i-1].sum2+p.top(),a[i].sum1-=p.top(); q.push(p.top()),p.pop(); cout<<a[i].s<<" "<<a[i].t<<" "<<p.top()<<" "<<q.top()<<"\n"; c[i]=(p.top()+q.top())/2; } while(!p.empty())p.pop(); while(!q.empty())q.pop(); for(int i=cnt;i>0;i--){ a[i].num1=a[i+1].num1+a[i].s+a[i].t; p.push(a[i].s),p.push(a[i].t); a[i].num2=a[i-1].num2+p.top(),a[i].sum1-=p.top(); q.push(p.top()),p.pop(); d[i]=(p.top()+q.top())/2; } anss=a[1].num2-a[1].num1; for(int i=1;i<=cnt;i++){ anss=min(anss,a[i].sum2-a[i].sum1+a[i+1].num2-a[i+1].num1); } cout<<anss+ans+cnt<<"\n"; } return 0; }:::: ::::info[F]
#include<bists/stdc++.h> #define int long long const int N=1e5+5; using namespace std; int n,a[N],x,y,ans,c[N],d[N]; signed main(){ ios::sync_with_stdio(false); cin.tie(0); cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; if(i%2==1)addc(i,a[i]); else addd(i,a[i]); //记极大极小值 } cin>>q for(int i=1;i<=q;i++){ cin>>x>>y; //更新相邻极大极小值 //利用其奇偶性算要减的值的和 cout<<ans; } return 0; }:::: ::::info[G]
#include<bits/stdc++.h> #define int long long const int N=2e5+5; using namespace std; int t,n,Q,a[N],p,x,b[1005][N]; int d[N],num,cnt; priority_queue<int>q1,q2; signed main(){ ios::sync_with_stdio(false); cin.tie(0); cin>>t; while(t--){ cin>>n>>Q; for(int i=1;i<=n;i++)cin>>a[i]; num=n,cnt=0; for(int i=2;i<=num;i++){ if(num%i==0){ d[++cnt]=num/i; while(num%i==0)num/=i; } } for(int i=1;i<=cnt;i++){ for(int j=1;j<=d[i];j++){ b[i][j%d[i]]=0; for(int z=j;z<=n;z+=d[i])b[i][j%d[i]]+=a[z]; q.push(b[i][j%d[i]]*(n/d[i])); } } cout<<q.top()<<"\n"; for(int i=1;i<=Q;i++){ cin>>p>>x; for(int j=1;j<=cnt;j++){ int k=p%d[j]; q.erase(b[j][k]*(n/d[j])); b[j][k]+=x-a[p]; q.push(b[j][k]*(n/d[j])); } a[p]=x; cout<<q.top()<<"\n"; } } return 0; }::::
Ps: 2026.7.7初写。
Ps: 2026.11.3补全。