视频讲解:
二维前缀和详解 (Codeforces Round 817 E - Counting Rectangles)_哔哩哔哩_bilibili
#include<bits/stdc++.h>
using namespace std;
#define int long long
vector<vector<int>> prefix;
vector<vector<int>> a;
signed main()
{
int T;
cin >> T;
while (T--)
{
int n, q, dig1, dig2;
cin >> n >> q;
prefix.assign(1005, vector<int>(1005, 0));
a.assign(1005, vector<int>(1005, 0));
for (int i = 0; i < n; i++)
{
cin >> dig1 >> dig2;
a[dig1][dig2] += dig1 * dig2;
}
for (int i = 1; i <= 1000; i++)
for (int j = 1; j <= 1000; j++)
prefix[i][j] = a[i][j] + prefix[i - 1][j] + prefix[i][j - 1] - prefix[i - 1][j - 1];
while (q--)
{
int hs, ws, hb, wb;
cin >> hs >> ws >> hb >> wb;
cout << prefix[hb - 1][wb - 1] - prefix[hb - 1][ws] - prefix[hs][wb - 1] + prefix[hs][ws] << endl;
}
}
return 0;
}