#include <iostream>
#include <unordered_map>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define oset tree<pair<int, int>, null_type,less<pair<int, int>>, rb_tree_tag,tree_order_statistics_node_update>
using
namespace
std;
using
namespace
__gnu_pbds;
class
Node {
public
:
int
value;
Node* next;
Node(
int
value) {
this
->value = value;
next = NULL;
}
};
void
solve(Node* head, oset& os, unordered_map<
int
,
int
>& mp,
int
& count) {
if
(head == NULL)
return
;
solve(head->next, os, mp, count);
count++;
os.insert({head->value, count});
mp[head->value]++;
int
numberOfElements = count - mp[head->value] - os.order_of_key({head->value, -1});
head->value = numberOfElements;
}
void
printList(Node* head) {
while
(head) {
cout << head->value << (head->next ?
"->"
:
""
);
head = head->next;
}
cout <<
"\n"
;
}
int
main() {
Node* head =
new
Node(43);
head->next =
new
Node(65);
head->next->next =
new
Node(12);
head->next->next->next =
new
Node(46);
head->next->next->next->next =
new
Node(68);
head->next->next->next->next->next =
new
Node(85);
head->next->next->next->next->next->next =
new
Node(59);
head->next->next->next->next->next->next->next =
new
Node(85);
head->next->next->next->next->next->next->next->next =
new
Node(37);
oset os;
unordered_map<
int
,
int
> mp;
int
count = 0;
printList(head);
solve(head, os, mp, count);
printList(head);
return
0;
}