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

Автор VasuOberoi, история, 3 года назад, По-английски

PLease explain why 2 same codes are not giving the same ans

code forces round 728 div2 Problem B :https://codeforces.net/contest/1541/problem/B

AC Submission : https://ide.codingblocks.com/s/579800

Wrong output Submission :https://ide.codingblocks.com/s/579801

Difference is using of macro (node) instead of pair<int,int>

Please help

If i am using #define node pair<int,int> it is getting accepted but when i am using typedef pair<int,int> node; it is giving wrong answer

Why this is happening ?? Is it a bug??

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

»
3 года назад, # |
  Проголосовать: нравится +34 Проголосовать: не нравится

First of all, you should better share your codes using numbers of submissions like that 120678373. It is more convenient for CF users; I examined your codes this way due to "compare" button. Also, it seems that you mixed up the submissions: the one you suggest as WA (https://ide.codingblocks.com/s/579801) is actually AC (my AC submission 120693832).

The actual reason of WA is that int * int == int, so a[i].first * a[j].first behaves not as you think it does. Why do you have AC and WA submissions both?

The compilation process in C++ consists of many stages. One of the earliest just scans the code and once it finds a #define, it will replace all the occurrences of the defined word below. typedef creates a synonym for the type; it behaves in more clever way.

So this code would get compilation error

#define

because preprocessor would replace node with pair<int, int> and there are many symbols that aren't allowed in variable names. On the other side, this code would work fine

typedef

The compiler understands that node is a variable name and doesn't replace it with something else.

So, in your AC submission the actual type of array a is array of pair<long long, long long>. You have these two defines in your code:

#define int long long
#define node pair<int, int>

and they do combine to the following (the first one changes the second one)

#define node pair<long long, long long>

And your WA submission contains pair<int, int> a[n], so that may cause WA or even TLE (in the case if (a[i].first * a[j].first > 2 * n) works too rare).