I want to know How swap function works

Правка en1, от TaeHo0o00o0N, 2020-05-21 08:01:14

when i wrote the code using small to larger techinque,

if i write code like

	if((int)s.size()<(int)s1.size()){
		for(set<pair<lld,int> >::iterator it = s.begin();it!=s.end();it++) s1.insert(*it);
		s = s1;
	}else{
		for(set<pair<lld,int> >::iterator it = s1.begin();it!=s1.end();it++) s.insert(*it);
	}

it gets TLE

but if i write code like

	if((int)s.size()<(int)s1.size()){
		for(set<pair<lld,int> >::iterator it = s.begin();it!=s.end();it++) s1.insert(*it);
	        swap(s,s1);
	}else{
		for(set<pair<lld,int> >::iterator it = s1.begin();it!=s1.end();it++) s.insert(*it);
	}

it gets AC and run so fast

but i don't know why difference of time is so big.. help..

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en2 Английский TaeHo0o00o0N 2020-05-21 08:04:08 55
en1 Английский TaeHo0o00o0N 2020-05-21 08:01:14 762 Initial revision (published)