Mixing fast IO, C-style input and C++-style input is a recipe for disaster.
Segmentation Fault occurs when your code tries to access invalid memory locations. I am new to CPP, but aren’t you supposed to free the memory you have dynamically created at run time using new operator?
If it is required, using it would probably fix that.
You may refer the following links
thanx bro, replacing scanf with cin worked