JTREE- Unofficial Editorial

Or we can do a bit easier: to take a minimum with binary lifting. So 1 get instead of update and get of segment tree. (A bit less to write).

Or we can do the same with bin lift, which is for me a bit easier to implement.

I used Sparse Table for calculating the minimum. It supports:-
Get minimum for range l-r in O(1) time.
Update the table in O(logn) time.
Not for this question but for strict time limits, I would recommend sparse table.

Accepted solution:- CodeChef: Practical coding for everyone

What is wrong with my code? Getting WA on some cases…

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define NMAX 1000056
#define TMAX 5000056
using namespace std;

typedef long long ll;

ll n, m;
vector<ll> adj[NMAX];
vector< pair<ll,ll> > tick[NMAX];
ll seg[TMAX];
ll dp[NMAX];

ll max(ll x, ll y) {
	return x > y ? x : y;
}

void update(ll node, ll start, ll end, 
					ll idx, ll val) {
	if(start == end) {
		seg[node] = val;
		return ;
	}
	ll mid = start + (end - start) / 2;
	if(idx <= mid)
		update(2*node+1, start, mid, 
						idx, val);
	else
		update(2*node+2, mid+1,
					end, idx, val);
	seg[node] = min(seg[2*node+1], seg[2*node+2]);
}

ll query(ll node, ll start, ll end, 
						ll left, ll right) {
	if(start > right || end < left)
		return INF;
	if(left <= start && end <= right)
		return seg[node];
	ll mid = start + (end - start) / 2;
	ll q1 = query(2*node+1, start, mid, left, right);
	ll q2 = query(2*node+2, mid+1, end, left, right);
	return min(q1, q2);
}

void dfs(ll u, ll h) {
	for(const auto p : tick[u])
		dp[u] = min(dp[u], 
				p.second + query(0, 0, n, 
								max(0, h-p.first), h));
	update(0, 0, n, h, dp[u]);
	for(ll v : adj[u])
		dfs(v, h+1);
	update(0, 0, n, h, INF);
}

int main() {
	ios::sync_with_stdio(false);
	cin >> n >> m;
	for(ll i=0;i<n-1;i++) {
		ll u, v;
		cin >> u >> v;
		adj[v].push_back(u);
	}
	for(ll i=0;i<m;i++) {
		ll v, k, w;
		cin >> v >> k >> w;
		tick[v].push_back(make_pair(k, w));
	}
	memset(seg, INF, sizeof(seg));
	memset(dp, INF, sizeof(dp));
	dp[1] = 0;
	update(0, 0, n, 0, dp[1]);
	dfs(1, 0);
	ll q;
	cin >> q;
	while(q--) {
		ll s;
		cin >> s;
		cout << dp[s] << endl;
	}
	return 0;
}

SOLVED : INF was too small. Cost me 75 pts.

Mine is also similar to this one but,
Insted of doing a segment tree to find out minimum in among the k parents , i am using binary ascent,
i store the parents at level 1,2,4,8,16,32,64 like that and minimum cost till then,

https://www.codechef.com/viewsolution/11437877

@kqr45j

Can you please explain how to update sparse table in O(Log(n)) time?

Or
Any link through which we can learn the update part?

Thanks

Similar solution but used the segment tree storage for as the dp table instead of creating a separate one. :slight_smile:

Awesome solution bro.Respect _ / \ _ :slight_smile:

Great examples! @arunnsit

1 Like

@vishveshcoder
Hi,I will try to explain on how the updation works in case of sparse table.

If you don’t know about sparse table I would recommend you to go through topcoder tutorial first. It is quite easy to understand everything related to sparse table.

Still If you are having trouble in understanding then I will explain the main idea behind sparse table-

For each index ‘i’ in the array we store the minimum of all continuous subarray of length 2^k(k belongs to whole number {0,1,2,…} ) which start at index ‘i’ till (i+2^k) is less than n(size of array).

Representation is table[i][k] where i is the index and 2^k is the length of the continuous subarray.arr[] is the array containing the elements.

For eg: if we are index i = 5 and lets say the size of array is n = 22, then for index i = 5 we will store the following information(this information is only for index i=5, we have to store such type of information for every node) :-

For k = 0, length = 2^k => length = 1, we store the minimum of subarray of length = 1 starting at index i = 5, which means minimum of index i = 5 only. So we have only one element and minimum of it will be itself only. => (table[5][0] = arr[5])

For k=1, length = 2^k -> length = 2, we store the minimum of subarray of length = 2 starting at index i = 5, which means minimum of index i = 5 and i = 6. => (table[5][1] = min(arr[5],arr[6]) )

For k=2, length = 2^k -> length = 4, we store the minimum of subarray of length = 4 starting at index i = 5, which means minimum of index i = 5,i = 6,i = 7 and i = 8.=> (table[5][2] = min(arr[5],arr[6],arr[7],arr[8]) )

For k=3, length = 2^k -> length = 8, we store the minimum of subarray of length = 8 starting at index i = 5, which means minimum of index i = 5 till i = 12 (from index 5 to 12 we have 8 elements). => (table[5][3] = min(arr[5], … ,arr[12]) )

For k=4, length = 2^k -> length = 16, we store the minimum of subarray of length = 16 starting at index i = 5, which means minimum of index i = 5 till i = 20 (from index 5 to 20 we have 16 elements). => (table[5][4] = min(arr[5], … ,arr[20]) )

For k=5, length = 2^k -> length = 32, we store the minimum of subarray of length = 32 starting at index i = 5, which means minimum of index i = 5 till i = 36 => (from index 5 to 36 we have 32 elements).

But for the last case k = 5, we can’t go till index 36 since size of array is n = 22 only, so we will store the minimum till 22 only for k = 5. => (table[5][5] = min(arr[5], … ,arr[22]) )

Now must be wondering on how to store these values, right?
Before that, Please understand that for each value of k, we will store answer for each element in the array and then we will increase our K, meaning, for k = 0 I will store the answer for each element and then only I will move on to k = 1. Similarly after I have stored the value(for k = 1) for each element, then only I will increment my k. So with that in mind, now you can move on to an example which will do 90% of the work itself.

Let’s say you are at index i = 5 and you need to calculate minimum for k = 4(length = 16, from index 5 till 20).
Also, we know the answer for k=3 for each element in the array (How? please read the above paragraph once again).
So now I will use the value stored for k = 3 to calculate the value for k = 4(which is a basic DP too).

For index i=5 and for k=4, the value will be MINIMUM of table[5][3] and table[13][3] . Because table[5][3] stores the minimum for elements from arr[5] till arr[12] and table[13][3] will store the minimum of arr[13] till arr[20] (since from index 13 to 20 we have 8 elements or length = 2^3). So for minimum of arr[5] till arr[20] we can directly use the previous values quite easily and update our value of table[5][4] in O(1) time .

In other words we divide our current target subarray (5 - 20) in two parts and use the previously computed values, since if we are calculating for length = 16 then, we must have computed for length = 8 before.

Now for this problem, as we move down the tree from root to some node, I already have the minimum stored for the nodes which come in the path from root to the current node. So for current node we repeat the same procedure as we did above and update the minimum values for each length for current node.

If you still have any doubt, then feel free to ask.

Great logic! (y)

@arunnsit Great explanation. Thanks for that.

But I had one doubt which I hope you will be able to clear. The complexity you told is O(Nlog(n) + M). What I think is, since log(n) time is spent for each ticket and there are M tickets, it should have been O(N + Mlog(n)).
Correct me if I am wrong.

I didn’t understand the calculating the vector V.

what we are doing over here is pushing a node u to the vector V when we haven't discovered the subtree of u and pop it when the whole subtree is discovered .

And what is p in dfs(u,p)? Could you elaborate?
Thanks.

I solved it using heavy light decomposition.

Link to my solution : CodeChef: Practical coding for everyone

I would like to say that this is one of the most well written editorials I have ever seen.
It was really clever to build the segment Tree on the basis of height and not node.
No need for topological sort now.

@dushsingh1995 , we don’t need the array this time , we are handling this with the segment tree . See for calculating solution for a node x , we have already updated our tree with DP[all nodes between the path from 1 to x] and thats all what we require . If you will be more specific , i can explain it in more details !

Give me a few mins , i will update it with an example :slight_smile:

@nidzulandz , updated :slight_smile:

@arunnsit Got it immidietly with examples! Amazing solution :smiley:

Aaah. That was harsh!