Hun Gui Wei Company

Time Limit: 4000ms
Memory Limit: 131072KB
This problem will be judged on HDU. Original ID: 5140
64-bit integer IO format: %I64d      Java class name: Main

Description

My friend Hun Gui Wei (HGW) has started a company which is named Hun Gui Wei Company (HGWC). In HGWC, there are many staffs. Every staff has a certain salary, level and working age. Sometimes, HGW wants to do some queries. He wants to know the sum of salary of the staffs fit some conditions. For the large amount of staffs, artificial query is time-consuming. So he wants to hire you to write a program to help him. If you do a good job, he will pay you a generous remuneration.

Input

Multi test cases (about 10).
The first line contains an integer n indicates there are n staffs in HGWC.
In the next n lines, each line describes the attributes of one staff.
Each line contains S, L, A indicate the salary, level and working age of one staff.
Next line an integer m comes which indicates there will be m queries.
Then next m lines, query dates will come
$\begin{matrix}
LL_0 & HL_0 & LA_0 & HA_0 \\
LL_1 & HL_1 & LA_1 & HA_1 \\
\vdots & \vdots & \vdots & \vdots \\
LL_{m-1} & HL_{m-1} & LA_{m-1} & HA_{m-1}
\end{matrix}$
HGW sets a variable k. In the beginning of each test case, k = 0. For the i-th query, he sets
$LL_i = LL_i + k, HL_i = HL_i - k, LA_i = LA_i + k, HA_i = HA_i - k$, then make k the answer of this query.

[Technical Specification]
All numbers in the input are integers.
$1 \leq n, m \leq 10^5$
$0 \leq S, L, A \leq 10^9$
$-10^{17} \leq LL_i, HL_i, LA_i, HA_i \leq 10^{17}$

Output

For i-th query,output the sum of salary of the staffs whose level are between $LL_i$ and $HL_i$ while working age are between $LA_i$ and $HA_i$ in a single line.

Sample Input

2
1 2 3
4 5 6
2
3 2 3 3
2 6 2 7

Sample Output

1
4

Source

Language: 
Theme: 
Share Code? 

Powered by NB231 | Current Style: .