# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
Name |
---|
Auto comment: topic has been updated by hell_hacker (previous revision, new revision, compare).
Your code fails for this test:
EDIT: modified to a shorter one
I like to start debugging treaps by commenting srand and always printing a tree traversal to see if everything is ordered, etc..
Thank you for your help. I am new to this data structure. When running your provided input many times I am getting 4 sometimes and sometimes 6. I don't know why.
Have you disabled "srand"? If you are seeding the random numbers with a different seed everytime, and your treap is bugged, then things like that can happen.
Disabled as in srand(time(NULL)) or not using it at all? And I also read that one has to make sure all the priority values have to be distinct in a treap. How do we take care of that. Isn't it possible for rand() to return the same value twice over a large number of times.
Pseudo-random number generators work like this: from a seed number, they generate all the other numbers. If you seed it with "srand" with the same seed every time, it will always produce the same numbers. Doesn't mean it will produce repeated numbers, only the same sequence of numbers. However, if you don't comment that line, your program will always generate a different sequence of numbers, which in turn produces a different tree (as node priorities will be different across different executions). So, to produce always the same tree, you should comment that line, but it should work fine nevertheless.
EDIT: what you're doing in your code is seed rand with a different number everytime, as time(0) returns the current time.
Yep, I removed the srand line now it gives proper output for both the inputs you provided. output 1 : 0 4 2 invalid output 2: 4
But still WA on SPOJ.
Try this one:
Try producing other tests and using them to debug your code. Print the tree traversal, and the value of the nodes you are entering into when searching for an element. It should give enough input to debug.
I found a problem. The insert function. Could you tell me how to take care of duplicates. In insert function I had an if condition that if value of key of root is equal to value of key of the node to be inserted i just return. But if root key is not equal to key of node to be inserted and has lower priority it will be added.
You could keep both the key (value) and a counter (how many are represented on the node).
EDIT: sorry, you actually should check whether an element has already been added or not, as according to the problem statement, an element is only added if it is not in the tree yet. You should check if the element exists, then add it it doesn't.
EDIT2: Something like this: