Блог пользователя samurai123

Автор samurai123, история, 6 лет назад, По-английски

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.

  • Проголосовать: нравится
  • -13
  • Проголосовать: не нравится

»
6 лет назад, # |
Rev. 2   Проголосовать: нравится +16 Проголосовать: не нравится

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.