CCOST - Calculate The Cost

no tags 

In a small Village near the Himalayas, there is a rich Land-Owner, in Possession of a vast, rectangular tract of land. Unknown to him, a Major Oil Corporation has verified the existence of a vast Oil Resource beneath the land owned by him.

The Oil Company sends a Man to negotiate the purchase of a rectangular field from within the landowner's land, with sides parallel to those of his area. The Landowner, valuing his land according to the trees growing in it and the area to be purchased, gives the company man a Map of his Land, marking the location of trees of different types, and a list of the worth of each type of tree.

To ensure the most economic purchase of land with the required dimensions, the Company Man provides you with the data in his possession, and alongwith that, a list of the land areas that he considers good by his judgement.

You must provide, for each land area that he has listed, the Sum Total of the values of the Trees that lie Within or On the Boundary of that land area.

Input

The first line of the input contains an integer T, which is the number of test cases. For each test case, the first line contains an integer n, equal to the number of trees in the area. This line is followed by n lines each containing 3 integers separated by spaces which are coordinate of the tree ( x, y ) and value of that tree. Following this is an integer R, equal to the number of proposols of land areas given by the Company Man. Next R lines contain 4 integers each (x1, y1, x2, y2) which are the coordinates of lower left ( x1,y1 ) and upper right ( x2, y2 ) corner of the rectangular area.

Output

For each test case, your program should output R lines containing the sum of values of the Trees which lie inside or on the corresponding rectangular plot. There should NOT BE any blank lines between output of different test cases.

Example

Input:
1
3
1 1 2
2 2 3
3 3 4
2
1 1 1 2
0 0 5 5

Output:
2
9

Constraints and Limits

T ≤ 10, n ≤ 10^5, r ≤ 50000, 0 ≤ x,y ≤ 10^7, value of any tree ≤ 10^4.
For any rectangular area 0 ≤ x1 ≤ x2 ≤ 10^7, 0 ≤ y1 ≤ y2 ≤ 10^7
Note 1: There can be more than one trees at the same point.
Note 2: The input data is large ( about 15 MB ), Be careful about your input output routines.
.


hide comments
Phạm Bãng Ðãng: 2016-08-18 16:00:37

NICE problem :))))

Himanshu: 2016-08-09 17:44:29

can be solved with just 1D-BIT.

anando_du: 2015-07-27 11:30:04

nice problem ^_^ inclusion exclusion caused me some WA .. and Array Limit caused RTE for 7 times -_- finally AC ^_^

Rishav Goyal: 2014-01-09 22:01:36

i think nlogn+rlog^n should pass the time limit. NO?

aristofanis: 2013-05-23 14:46:50

int => WA
long long => TLE
^_^

Last edit: 2013-05-23 14:47:12

Added by:Ajay Somani
Date:2008-02-05
Time limit:0.915s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:CodeCraft 08, Problem Setter: Ajay Somani,Anshuman Singh