samurai123's blog

By samurai123, history, 5 years ago, In English

Hello all, I am solving Problem and it is running fine in my computer. I also checked on ideone.com and there also it is giving expected output but it is giving runtime error on codeforces(Exit code is -1073741819) Submission. Can anyone help me and tell reason for this weird behavior? Thanks in advance.

  • Vote: I like it
  • -13
  • Vote: I do not like it

»
5 years ago, # |
Rev. 2   Vote: I like it +16 Vote: I do not like it

In line 52, the statement a[i].erase(it); erases from the dynamic memory the item in a[i] whose address is stored in the iterator it. In line 45, the post statement of the for loop included in the macro tr(a[i],it) attempts to execute it++ after erasing the item. This generates a segmentation fault.

You should not increment it after erasing the item. Instead, store it in another iterator variable iv, increment iv, erase the item whose address is stored in it, and then store the address in iv back to it. Replace lines 45-54 with the following lines:

for( auto it = a[i].begin(), iu = a[i].end(), iv = it; it != iu; it = iv )
{
    iv++;

   if(present(b[i],*it))
   {
	auto del=b[i].find(*it);
	b[i].erase(del);
	ans++,ansb++;
	a[i].erase(it);
   }
} 

This should fix the problem.