关于 map
查看原帖
关于 map
399475
_XHY20180718_楼主2023/8/20 01:08

到底是swap()快还是clear()快,这道题实测swap被卡到飞起:

swap():

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,sum,cnt[N<<1],vis;
set<int>st;
map<int,int>mp[N<<1];
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>n;int op,k,b;
	while(n--){
		cin>>op>>k>>b;
		if(op==1){
		    ++mp[k+N][b];
		    ++cnt[k+N],++sum;
		    st.insert(k+N);
		}
		if(op==2)cout<<sum-cnt[k+N]<<'\n';
		if(op==3){
		    int x=0;
		    for(auto t=st.upper_bound(x);t!=st.end();t=st.upper_bound(x))
		    {x=*t;if(x!=k+N){map<int,int>t;mp[x].swap(t);cnt[x]=0;}}
			auto t=mp[k+N].find(b);
			if(t!=mp[k+N].end())cnt[k+N]-=t->second,mp[k+N].erase(b);
			sum=cnt[k+N];
		}
	}
	return 0;
}

clear():

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,sum,cnt[N<<1],vis;
set<int>st;
map<int,int>mp[N<<1];
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>n;int op,k,b;
	while(n--){
		cin>>op>>k>>b;
		if(op==1){
		    ++mp[k+N][b];
		    ++cnt[k+N],++sum;
		    st.insert(k+N);
		}
		if(op==2)cout<<sum-cnt[k+N]<<'\n';
		if(op==3){
		    int x=0;
		    for(auto t=st.upper_bound(x);t!=st.end();t=st.upper_bound(x))
		    {x=*t;if(x!=k+N)st.erase(t),mp[x].clear(),cnt[x]=0;}
			auto t=mp[k+N].find(b);
			if(t!=mp[k+N].end())cnt[k+N]-=t->second,mp[k+N].erase(b);
			sum=cnt[k+N];
		}
	}
	return 0;
}
2023/8/20 01:08
加载中...