E. Counting Rectangles(二维前缀和)
原题链接
You have nn rectangles, the ii-th rectangle has height hihi and width wiwi.
You are asked qq queries of the form hs ws hb wbhs ws hb wb.
For each query output, the total area of rectangles you own that can fit a rectangle of height hshs and width wsws while also fitting in a rectangle of height hbhb and width wbwb. In other words, print ∑hi⋅wi∑hi⋅wi for ii such that hs<hi<hbhs<hi<hb and ws<wi<wbws<wi<wb.
Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.
Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
Input
The first line of the input contains an integer tt (1≤t≤1001≤t≤100) — the number of test cases.
The first line of each test case two integers n,qn,q (1≤n≤1051≤n≤105; 1≤q≤1051≤q≤105) — the number of rectangles you own and the number of queries.
Then nn lines follow, each containing two integers hi,wihi,wi (1≤hi,wi≤10001≤hi,wi≤1000) — the height and width of the ii-th rectangle.
Then qq lines follow, each containing four integers hs,ws,hb,wbhs,ws,hb,wb (1≤hs<hb, ws<wb≤10001≤hs<hb, ws<wb≤1000) — the description of each query.
The sum of qq over all test cases does not exceed 105105, and the sum of nn over all test cases does not exceed 105105.
Output
For each test case, output qq lines, the ii-th line containing the answer to the ii-th query.
Example
input
Copy
3
2 1
2 3
3 2
1 1 3 4
5 5
1 1
2 2
3 3
4 4
5 5
3 3 6 6
2 1 4 5
1 1 2 10
1 1 100 100
1 1 3 3
3 1
999 999
999 999
999 998
1 1 1000 1000
output
Copy
6 41 9 0 54 4 2993004
Note
In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a 1×11×1 rectangle inside of it and fit into a 3×43×4 rectangle.
Only the 2×32×3 rectangle works, because 1<21<2 (comparing heights) and 1<31<3 (comparing widths), so the 1×11×1 rectangle fits inside, and 2<32<3 (comparing heights) and 3<43<4 (comparing widths), so it fits inside the 3×43×4 rectangle. The 3×23×2 rectangle is too tall to fit in a 3×43×4 rectangle. The total area is 2⋅3=62⋅3=6.
思路:
1,根据范围,不可能暴力,
2,只看输入的h,w,联想到二维前缀和,注意规范操作
代码:
int a[1020][1020];
void solve(){//二维前缀和
// int x;read(x);cout<<x<<'\n';
int n,q;
memset(a,0,sizeof(a));
read(n);read(q);
int ans=0;
for(int i=1;i<=n;++i){
int x,y;
read(x);read(y);
a[x][y]+=x*y;
}
for(int i=1;i<=1000;++i){
for(int j=1;j<=1000;++j){
a[i][j]=a[i][j]+a[i-1][j]+a[i][j-1]-a[i-1][j-1];
}
}
while(q--){
int x1,y1,x2,y2;
read(x1);read(y1);read(x2);read(y2);
cout<<a[x2-1][y2-1]-a[x1][y2-1]-a[x2-1][y1]+a[x1][y1]<<'\n';
}
}