Problem - 1722E - Codeforces
codeforces.com
이차원 누적합
- 제목
Counting Rectangles
- 조건
time limit per test : 6 second
memory limit per test : 256 megabytes
input : standard input
output : standard output
- 문제
You have n rectangles, the i-th rectangle has height hi and width wi.
You are asked q queries of the form hs ws hb wb.
For each query output, the total area of rectangles you own that can fit a rectangle of height hs and width ws while also fitting in a rectangle of height hb and width wb. In other words, print ∑hi⋅wi for i such that hs<hi<hb and ws<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++).
- 입력
The first line of the input contains an integer t (1≤t≤100) — the number of test cases.
The first line of each test case two integers n,q (1≤n≤10^5; 1≤q≤10^5) — the number of rectangles you own and the number of queries.
Then n lines follow, each containing two integers hi,wi (1≤hi,wi≤1000) — the height and width of the i-th rectangle.
Then q lines follow, each containing four integers hs,ws,hb,wb (1≤hs<hb, ws<wb≤1000) — the description of each query.
The sum of q over all test cases does not exceed 105, and the sum of n over all test cases does not exceed 10^5.
- 출력
For each test case, output q lines, the i-th line containing the answer to the i-th query.
- 힌트
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×1 rectangle inside of it and fit into a 3×4 rectangle.
Only the 2×3 rectangle works, because 1<2 (comparing heights) and 1<3 (comparing widths), so the 1×1 rectangle fits inside, and 2<3 (comparing heights) and 3<4 (comparing widths), so it fits inside the 3×4 rectangle. The 3×2 rectangle is too tall to fit in a 3×4 rectangle. The total area is 2⋅3=6.
예제 입력1 | 예제 출력1 |
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 |
6 41 9 0 54 4 2993004 |
#include <iostream>
#include <algorithm>
using namespace std;
#define fastio ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define endl '\n'
long long table[1001][1001];
void init(){
for(int n = 0 ; n < 1001 ; n++){
for(int m = 0 ; m < 1001 ; m++){
table[n][m] = 0;
}
}
}
int main() {
fastio;
int tc, r, c, r1, r2, c1, c2;
cin >> tc;
while(tc--){
init();
int sz_sample, sz_std;
cin >> sz_sample >> sz_std;
for(int k = 0 ; k < sz_sample ; k++){
cin >> r >> c;
table[r][c] += r * c;
}
for(int n = 1 ; n < 1001 ; n++){
for(int m = 1 ; m < 1001 ; m++){
table[n][m] += table[n - 1][m] + table[n][m - 1] - table[n - 1][m - 1];
}
}
for(int k = 0 ; k < sz_std ; k++){
cin >> r1 >> c1 >> r2 >> c2;
cout << table[r2 - 1][c2 - 1] - (table[r1][c2 - 1] + table[r2 - 1][c1] - table[r1][c1]) << endl;
}
}
return 0;
}