hey @hikarico I’m trying to understand your approach(CodeChef: Practical coding for everyone) in June17 CLONEME question. You have used persistent hash tree. but I’m not getting your approach from line 133. If the left or right any of them are not equal then we are searching for leftmost or rightmost, but it could be in between also…? did I misunderstand something here?
I dont have enough karma points to reply to your comment on original post.
Could somebody give me the implimentation of this problem with this code of persistent seg tree please.I don’t know why it gives me wrong answer. This problem
Just a basic knowledge of segment trees is enough? I have just started wth this thing (solved Q on minimum in range [l,r] and other basic ones). Will that be enough to understand it? Or do i need to see/practice more before starting this?
It’s the same as segment tree. We’re not really creating new nodes during query (except when lazy), so it’s the same code. Except that instead of passing 2p and 2p + 1, we pass l[p] and r[p] during recursion:
// Example RSQ, call sum_query(a, b, root)
int sum_query(int a, int b, int p, int L=0, int R=n-1) {
if (b < L || R < a) return 0; // outside range
if (a <= L && R <= b) return st[p]; // inside range
return sum_query(a, b, l[p], L, M)
+ sum_query(a, b, r[p], M + 1, R);
}
Let’s say I want to compare if array [1, 2, 2, 3, 4] is similar to [1, 2, 3, 3, 4]. My trees represent hashing of frequency tables {1:1, 2:2, 3:1, 4:1} and {1:1, 2:1, 3:2, 4:1}
Look at second layer, there’s a special case where both left and right have mismatch, but the result is still “similar”. This can be solved by comparing rightmost and leftmost in both trees.