In a star-ravaged night, the king of garlic A number of the stars in the sky.
The king of garlic has cleverly drawn a Cartesian coordinate system in the sky, so that all the stars are distributed in the first image. There is an NN star in the sky, he can know the coordinates and brightness of each star.
Now, the garlic June asked himself QQ times, each time he asked himself each rectangular area of the star's brightness and how much (including the Stars on the border).
Input Format
The first line enters an integer n (1 \le n \le 50000) n (1≤n≤50000) that represents the number of stars.
Next to the NN line, enter three integers per line x,y,w (0 \le x, y, W\le) x,y,w (0≤x,y,w≤2000), indicating a star with a WW brightness in coordinates (x, y) (x, y). Note that a point may have more than one star.
The next line is to enter an integer q (1 \le q \le 50000) Q (1≤q≤50000) that represents the number of queries.
Next QQ line, each line input four integers x_1, y_1, X_2, y_2x1, y1, x2, Y2, where (x_1, y_1) (x1, y1) represents the coordinates of the lower-left corner of the query rectangle, (x_2, y_2) (x2, y2) represents the upper-right corner of the query rectangle Coordinates, 0 \le x_1 \le x_2 \le 20000≤x1≤x2≤2000,0 \le y_1 \le y_2 \le 20000≤y1≤y2≤2000. output Format
For each query, the output is an integer that represents the sum of the brightness of the stars within the rectangular area of the query. Sample Input
5
5 0 6 7 9 7 8 6 9 7 1 3 0 4 0 8
7 9
0 0 7
2 7 9 5 4 7 5
Sample Output
7
8
0
Because the data range is small, using prefixes and inferences to space and, so dp[i][j] represents (1,j), (i,1), (i,j) The sum of the stars ' brightness between four points, then (x1,y1) and (X2,y2) The sum of the Stars ' brightness is dp[x2][y2]-DP [X2] [Y1-1]-dp[x1-1][y2] + dp[x1-1][y1-1], (because there may be stars on the coordinate line, you need to consider the boundary), see the code.
#include <stdio.h> #include <string.h> int dp[2005][2005]; int main () {int n;
int x, y, W;
int q;
int x1, y1, x2, y2;
int mx = 2005;
memset (DP, 0, sizeof (DP));
scanf ("%d", &n);
while (n--) {scanf ("%d%d%d", &x, &y, &w); DP[X+1][Y+1] + = W; Because the stars can be repeated, that brightness can overlay} for (int i = 1; i < mx; i++) {for (int j = 1; j < MX; j + +) {Dp[i][j] + = Dp[i-1][j] + DP I
[J-1]-dp[i-1][j-1];//Note border}} scanf ("%d", &q);
while (q--) {scanf ("%d%d%d%d", &x1, &y1, &x2, &y2);
int ans = dp[x2+1][y2+1]-dp[x2+1][y1]-dp[x1][y2+1] + dp[x1][y1];//Note border printf ("%d\n", ans);
} return 0; }