Codeforces 1722E Counting Rectangles(二维前缀和)

发布于:2023-01-04 ⋅ 阅读:(322) ⋅ 点赞:(0)

题目链接:Problem - E - Codeforces

Codeforces Round #817 (Div. 4) E题

题目

                                                    E. Counting Rectangles

time limit per test 4 seconds
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 whb 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 hiwi for ii 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≤1e51≤q≤1e5) — the number of rectangles you own and the number of queries.

Then n lines follow, each containing two integers hi (1≤hi,wi≤1000) — the height and width of the i-th rectangle.

Then q lines follow, each containing four integers hs,ws,hb,wbhs,ws,hb,wb (1≤hs<hbws<wb≤1000) — the description of each query.

The sum of q over all test cases does not exceed 1e5, and the sum of n over all test cases does not exceed 1e5.

Output

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

Example

input 

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

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×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.

题意:之后再说

完整代码(C++)(下方附TLE代码 蠢.jpg):

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define INF 0x3f3f3f3f
typedef long long ll;
const int N = 1e3 + 3;
ll s[N][N];
int main()
{
    std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int T = 1;
    cin >> T;
    while (T--)
    {
        memset(s, 0, sizeof s);
        int n, q;
        cin >> n >> q;
        for (int i = 1; i <= n; i++)
        {
            int x, y;
            cin >> x >> y;
            s[x][y] += x * y;
        }
        for (int i = 1; i <= 1000; i++)
            for (int j = 1; j <= 1000; j++)
                s[i][j] += s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];
        for (int i = 1; i <= q; i++)
        {
            int h, w, hh, ww;
            cin >> h >> w >> hh >> ww;
            cout << s[hh - 1][ww - 1] + s[h][w] - s[h][ww - 1] - s[hh - 1][w] << endl;
        }
    }
    return 0;
}

完整TLE代码(C++)

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define INF 0x3f3f3f3f
typedef long long ll;
const int N = 1e5 + 3;
int a[N], b[N], c[N];
int main()
{
    std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int T = 1;
    cin >> T;
    while (T--)
    {
        int n, q;
        cin >> n >> q;
        for (int i = 1; i <= n; i++)
        {
            cin >> a[i] >> b[i];
            c[i] = a[i] * b[i];
        }
        for (int i = 1; i <= q; i++)
        {
            int h, w, hh, ww;
            ll sum = 0;
            cin >> h >> w >> hh >> ww;
            for (int j = 1; j <= n; j++)
                if (a[j] > h && a[j] < hh && b[j] > w && b[j] < ww)
                    sum += c[j];
            cout << sum << endl;
        }
    }
    return 0;
}

本文含有隐藏内容,请 开通VIP 后查看