YJJ is a salesman who has traveled through western country. YJJ is always on journey. Either is he at the destination, or on the way to destination.
One day, he is going to travel from city A to southeastern city B. Let us assume that A is (0,0)(0,0) on the rectangle map and B (109,109)(109,109). YJJ is so busy so he never turn back or go twice the same way, he will only move to east, south or southeast, which means, if YJJ is at (x,y)(x,y) now (0≤x≤109,0≤y≤109)(0≤x≤109,0≤y≤109), he will only forward to (x+1,y)(x+1,y), (x,y+1)(x,y+1) or (x+1,y+1)(x+1,y+1).
On the rectangle map from (0,0)(0,0) to (109,109)(109,109), there are several villages scattering on the map. Villagers will do business deals with salesmen from northwestern, but not northern or western. In mathematical language, this means when there is a village kk on (xk,yk)(xk,yk) (1≤xk≤109,1≤yk≤109)(1≤xk≤109,1≤yk≤109), only the one who was from (xk−1,yk−1)(xk−1,yk−1) to (xk,yk)(xk,yk) will be able to earn vkvk dollars.(YJJ may get different number of dollars from different village.)
YJJ has no time to plan the path, can you help him to find maximum of dollars YJJ can get.
Input
The first line of the input contains an integer TT (1≤T≤10)(1≤T≤10),which is the number of test cases.
In each case, the first line of the input contains an integer NN (1≤N≤105)(1≤N≤105).The following NN lines, the kk-th line contains 3 integers, xk,yk,vkxk,yk,vk (0≤vk≤103)(0≤vk≤103), which indicate that there is a village on (xk,yk)(xk,yk) and he can get vkvk dollars in that village.
The positions of each village is distinct.
Output
The maximum of dollars YJJ can get.
Sample Input
1 3 1 1 1 1 2 2 3 3 1
Sample Output
3
思路:首先可以联想到在矩阵里移动的dp,dp[i][j]=max(dp[i-1][j],dp[i][j-1],dp[i-1][j-1]+v[i][j]),首先离散化是必然的。
其次这样的二维数组是开不了的(因为1e5*1e5爆空间了),所以先考虑滚动一维,dp[j]表示第j列能得到的最大值,有一点可以肯定,在同一列上的点,只能选择一个进行交易。所以按横坐标顺序一次选择是最优策略,所以滚动掉i这一维。
接着,第i列的最大值,可能来自前i行的某一列,所以可以考虑用线段树维护前j-1列的最值。(据说不用线段树,直接做也可以水过去)。
离散化后对x按从小到大排序,y按从大到小排序(类似01背包的滚动数组原理,需要逆序求解).然后边更新,边查询即可。
代码:
#include
#include
#include
#include
#include
#include
#include
#include