Sorry for the delay, here it is:
Commented code: CodeChef: Practical coding for everyone
“Queries for number of distinct elements in a subarray”:
Queries for number of distinct elements in a subarray - GeeksforGeeks
Stack algorithm for “next greater element”:
Next Greater Element (NGE) for every element in given Array - GeeksforGeeks
Persistent segment tree:
Persistent Segment Tree | Set 1 (Introduction) - GeeksforGeeks
Comments, questions, requests, criticism, are all welcome!