5.L - Medium - Counting Rectangles

文章描述了一个编程问题,要求计算给定多个矩形的情况下,如何在满足条件(高度和宽度限制)下求出可以嵌套在另一组矩形内的总面积。解决方案涉及使用动态规划计算子矩形面积,提示使用64位整数类型处理可能的大数值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

You have n rectangles, the i-th rectangle has height  and width.

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++).

Input

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≤105; 1≤q≤105) — the number of rectangles you own and the number of queries.

Then n lines follow, each containing two integers hi,wiℎ(1≤hi,wi≤10001≤ℎ) — 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 105105, and the sum of n over all test cases does not exceed 105105.

Output

For each test case, output q lines, the i-th line containing the answer to the i-th query.

#include<bits/stdc++.h>
#define ll long long
#define int ll
#define endl '\n'
using namespace std;
const int N=1e5+7;
int n,q,a[1005][1005];
void solve()
{
	memset(a,0,sizeof(a));
	cin>>n>>q;
	for(int i=1;i<=n;i++)
	{
		int x,y;
		cin>>x>>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-1][j]+a[i][j-1]-a[i-1][j-1];
		}
	}
	while(q--)
	{
		int x1,x2,y1,y2;
		cin>>x1>>y1>>x2>>y2;
		x1++;x2--;y1++;y2--;
		cout<<a[x2][y2]-a[x1-1][y2]-a[x2][y1-1]+a[x1-1][y1-1]<<endl;
	}
}
signed main()
{
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t;
	cin>>t;
	while(t--)
	{
		solve();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值