ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1767. The House of Doctor Dee

Why doesn't labs() work?
Posted by Avatar 12 Apr 2010 11:04
I got WA6 in this case:
if (inc1 == inc2) {
    cout << labs(cr2x - cr1x) + labs(cr2y - cr1y);
    return;
}
cout << max(labs(cr2x - cr1x), labs(cr2y - cr1y));

but I got AC in this case:
if (inc1 == inc2) {
    cout << (cr2x - cr1x) + labs(cr2y - cr1y);
    return;
}
cout << max(labs(cr2x - cr1x), labs(cr2y - cr1y));

and in this case:
if (inc1 == inc2) {
    cout << labs(cr2x - cr1x) + (cr2y - cr1y);
    return;
}
cout << max(labs(cr2x - cr1x), labs(cr2y - cr1y));

and in this:
if (inc1 == inc2) {
    cout << (cr2x - cr1x) + (cr2y - cr1y);
    return;
}
cout << max(labs(cr2x - cr1x), labs(cr2y - cr1y));

p.s. cr1x, cr1y, cr2x, cr2y is long long, (cr2x - cr1x)>=0 and (cr2y - cr1y)>=0 always, but why in this case does not labs() work correctly ?
Re: Why doesn't labs() work?
Posted by Alipov Vyacheslav [Tomsk PU] 12 Apr 2010 22:03
  Letter 'l' in "labs" stands for "long" = "int" (not "long long").
  So in the first case two summands are converted from "long long" to "int" and the summation causes overflow.
  In the second and the third cases one of the summands has type "long long" so the sum is in type "long long" too.

Edited by author 12.04.2010 22:11