I want to know How swap function works

Revision en2, by TaeHo0o00o0N, 2020-05-21 08:04:08

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 ( problem accept 4 second )

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 ( runnging time is 200ms )

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

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English TaeHo0o00o0N 2020-05-21 08:04:08 55
en1 English TaeHo0o00o0N 2020-05-21 08:01:14 762 Initial revision (published)