№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
Название |
---|
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: