2018 Blue Bridge Simulation Competition Star Violence in the Sky | 2D Tree Array

On a starry night, Mr. Garantou counted the stars in the sky one by one.

Mr. Garantou ingeniously drew a rectangular coordinate system for the sky, so that all the stars are distributed in the first image. there is n in the sky n  stars, he can know the coordinates and brightness of each star.

Now, Mr. Garantou asks himself  qq  times, each time he asks himself what is the sum of the brightness of the stars in each rectangular area (including the stars on the border).

input format

Enter an integer  n on the first line (1 \le n \le 50000)n ( 1 n 5 0 0 0 0 )  represents the number of stars.

next  _n  lines, each line input three integers x,y,w(0 \le x, y, w\le 2000)x , y , w ( 0 x , y , w 2 0 0 0 ) , expressed at coordinates (x,y)( x , y )  has a brightness ww  stars. Note that a point may have multiple stars.

Enter an integer  q on the next line (1 \le q \le 50000)q ( ​​1 q 5 0 0 0 0 ) , indicating the number of queries.

next  qq  lines, each line input four integers x_1, y_1, x_2, y_2x1, and1,x2, and2, where  (x_1, y_1)(x1, and1)  represents the coordinates of the lower left corner of the query rectangle,(x_2, y_2)(x2, and2)  represents the coordinates of the upper right corner of the query rectangle,0 \le x_1 \le x_2 \le 20000x1x22 0 0 00 \le y_1 \le y_2 \le 20000y1y22000

output format

For each query, output a line with an integer representing the sum of the brightnesses of the stars within the query rectangle.

sample input

5
5 0 6
7 9 7
8 6 13
9 7 1
3 0 19
4
0 8 7 9
0 0 7 10
2 7 10 9
5 4 7 5

Sample output

7
32
8
0

Give the coordinates under the first quadrant and give the values ​​under this coordinates Give us the range of multiple rectangles Find this

Although violence can

But tree-like arrays can also be ~ and faster~

Complexity O(q*log(x)*log(y))

x,y<=2000

Note that the boundaries are counted and the data cannot exist on 0,0

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll tre[2010][2010];
void add(int x,int y,int w)
{
	for(int i=x;i<=2001;i+=i&(-i)){
		for(int j=y;j<=2001;j+=j&(-j)){
			tre[i][j]+=w;
	}
}
ll query(int x,int y){
	ll sum=0;
	for(int i=x;i>0;i-=i&(-i)){
		for(int j=y;j>0;j-=j&(-j)){
			sum+=tre[i][j];			
	}
	return sum;
}
intmain()
{
	int n;
	ios::sync_with_stdio(0);
	cin>>n;
	while(n--){
		int x,y,w;
		cin>>x>>y>>w;
		add(x+1,y+1,w);
	}
	int q;
	cin>>q;
	while(q--){
		int lx,ly,rx,ry;
		cin>>lx>>ly>>rx>>ry;
		
		ll ans = query(rx+1,ry+1)-query(rx+1,ly)-query(lx,ry+1)+query(lx,ly); //Note that the side should be counted in the rectangle
		cout<<ans<<endl;			
	}
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325763171&siteId=291194637