首页 > 其他分享 >Counting Rectangles(二维数组前缀和)

Counting Rectangles(二维数组前缀和)

时间:2022-11-16 15:56:54浏览次数:80  
标签:前缀 fit Rectangles height each test Counting rectangles rectangle

题目链接

题目描述:

You have \(n\) rectangles, the \(i\)-th rectangle has height \(h_i\) and width \(w_i\).

You are asked \(q\) queries of the form \(h_s w_s h_b w_b\).

For each query output, the total area of rectangles you own that can fit a rectangle of height \(h_s\) and width \(w_s\) while also fitting in a rectangle of height \(h_b\) and width \(w_b\). In other words, print \(\sum{h_i⋅w_i}\) 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 \(h_i,w_i (1≤h_i,w_i≤1000)\) — the height and width of the \(i\)-th rectangle.

Then \(q\) lines follow, each containing four integers $h_s,w_s,h_b,w_b (1≤h_s<h_b, w_s<w_b≤1000) $— the description of each query.

The sum of \(q\) over all test cases does not exceed \(10^5\), 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\).


题解:

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const int N = 1010;

int T = 1;
int n, q;
LL a[N][N]; // 利用二维数组来存储, a[i][j] 代表长 i ,宽为 j 的矩阵的面积

void solve()
{
    scanf("%d%d", &n, &q);

    memset(a, 0, sizeof a); // 每次都将矩阵初始化为0

    for (int i = 1; i <= n; i++)
    {
        int x, y;
        scanf("%d%d", &x, &y);

        a[x][y] += x * y; // 将矩阵的值变为面积(注意要加)
    }

    for (int i = 1; i <= 1001; i++)
    {
        for (int j = 1; j <= 1001; j++)
        {
            a[i][j] = a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1] + a[i][j]; // 将矩阵做前缀和
        }
    }

    while (q--)
    {
        int h1, w1, h2, w2, l, r;
        scanf("%d%d%d%d", &h1, &w1, &h2, &w2);

        // 题目要求长和宽相等的矩阵也不符合要求,所以一定要严格要求长和宽都小于或大于所给的两个矩阵
        h1 = h1 + 1;
        w1 = w1 + 1;
        h2 = h2 - 1;
        w2 = w2 - 1;

        printf("%lld\n", a[h2][w2] - a[h1 - 1][w2] - a[h2][w1 - 1] + a[h1 - 1][w1 - 1]); // 求前缀和
    }
}

int main()
{
    // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

    scanf("%d", &T);

    while (T--)
    {
        solve();
    }

    return 0;
}

标签:前缀,fit,Rectangles,height,each,test,Counting,rectangles,rectangle
From: https://www.cnblogs.com/KSzsh/p/16896167.html

相关文章

  • 什么是索引的最左前缀法则
    最左前缀法则:如果索引有多列,如:联合索引,要遵守最左前缀法则。指的是查询从索引的最左前列开始并且不跳过索引中的列,否则将用不到索引。EXPLAINSELECT*FROMemployeesWH......
  • 二维前缀和
    ```#include<bits/stdc++.h>usingnamespacestd;intn,m,K,cnt;inta[510][510],f[510][510];intmain(){ cin>>n>>m>>K; for(inti=1;i<=n;i++) { for(intj=1;j<=m;j+......
  • CF1650G 『Counting Shortcuts』 题解
    从洛谷博客那里搬过来的(图论专题本来打算先挑最简单的做,结果做了两个多小时(题意就是让你找从起点\(s\)到终点\(t\)的最短路以及次短路个数,本题次短路长度指的是最短......
  • DTOJ 5932 Counting 题解
    题目链接portal题解认识到了生成函数很好用,于是摆了一篇题解10分直接dp,\(f_{i,j}\)表示走了\(i\)步之后,当前位置在\(j\)的方案数然后就有状态转移方程\(f_{i,......
  • CF1748E Yet Another Array Counting Problem 题解
    可能更好的阅读体验题目传送门题目大意给定一个长度为\(n\)的序列\(a\)和\(m\),定义\([l,r]\)的最左边的最大值为最小的\(l\lei\ler\)满足\(x_i=\max\{a_......
  • #yyds干货盘点# 动态规划专题:二维前缀和
    1.简述:描述给你一个n行m列的矩阵A,下标从1开始。接下来有q次查询,每次查询输入4个参数x1,y1,x2,y2请输出以(x1,y1)为左上角,(x2,y2)为右下角的子矩阵......
  • 能力提升综合题单-模拟,前缀和差分 题解
    好久没有写题解了,今天回来了!!A-铺地毯在luogu,享受coding的快乐见到题以后,一道水题直接模拟二位数组。。。《真·ACcode》:点击查看代码#include<bits/stdc++.h>u......
  • 十六进制和八进制的前缀
    1、八进制数是一种逢八进一的计数体制,基数是8,用0~7表示,如077。2、八进制数以数字0开头。3、十六进制数是一种逢十六进一的计数体制,基数是16,用09,AF表示,如0xFF或0XFF。4、十......
  • #yyds干货盘点# 动态规划专题:前缀和
    1.简述:描述给定一个长度为n的数组.接下来有q次查询,每次查询有两个参数l,r.对于每个询问,请输出输入描述:第一行包含两个整数n和q.第二行包含n个整数,表示.接下来q行,每......
  • 208.实现Trie(前缀树)
    Trie(发音类似"try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。这一数据结构有相当多的应用情景,例如自动补完和拼写检查。请你实现Tri......