Wrong answer submission and Accepted submission
You can compare them, the only difference is that the Wrong answer submission has an extra cin.tie(0)->sync_with_stdio(0);
I didn't use cin
and cout
, just fread
and fwrite
.
Can someone tell me if there is something wrong with my usage or a compiler bug.
https://cplusplus.com/reference/ios/ios/tie/
In other words,
cin.tie
does not return&cin
for->sync_with_stdio(0)
to operate on. Instead, it returns the C stdin. You can confirm this by running the programFor me, it output
EDIT: This was not the error, see below.
sync_with_stdio
is a static function of classios
, socin.tie(0)->sync_with_stdio(0)
is equivalent tocin.tie(0), ios::sync_with_stdio(0)
.I see, so that wasn't the issue.
I've played around with your code a bit. Haven't found a solution yet, but there might be some ordering issue with the commas. See this submission where I just changed a comma and got it to error at the 94684 number instead of the 47312 number. This might be a situation where C++'s optimizer might be screwing things by re-ordering things around.
94684 is about twice 47312, I think the reason for this is that you changed inSZ from
1 << 17
to1 << 18
, so I still think it'scin.tie(0)->sync_with_stdio(0)
that's causing thefread
error.OK, I found your error. Using
cin.tie
andsync_with_stdio
afterfread
is undefined behavior, and you usedfread
within yourIO
constructor.See this submission. the only change is that I used
new
to allocate your code so the constructor happens AFTER thecin
functions.Thanks.
One way of fixing this is by making dummy methods in your IO struct, and defining cin and cout as macros to your IO struct. For example, my IO struct in this submission uses the same idea and functions as a drop-in replacement for my template that has
cin.tie(nullptr)->sync_with_stdio(false)
for all practical purposes (other than interactive problems, of course).