您的位置:首页 > 其它

2017 ACM Arabella Collegiate Programming Contest Snake Rana

2018-01-09 20:23 791 查看
Snake Rana

time limit per test
4.0 s

memory limit per test
256 MB

input
standard input

output
standard output

Old Macdonald wants to build a new hen house for his hens. He buys a new rectangular area of size N by M.
The night before he builds the hen house, snake Rana devises an evil plan to plant bombs in K distinct cells in the area to kill the
hens and eat them for dinner later.

The morning of, Old Macdonald notices that each of the K cells, where snake Rana planted a bomb, have a marking on them. That won’t
stop him though, all he must do is build the hen house in an area with no bombs.

Assume that rows are numbered from top to bottom, and columns are numbered from left to right. Old Macdonald now wants to know the number of ways he can choose sub-rectangles of top left coordinates (x1, y1) and
bottom right coordinates (x2, y2) (x1 ≤ x2) (y1 ≤ y2)such
that there are no bombs in the sub rectangle.

Input

The first line of input is T – the number of test cases.

The first line of each test case is three integers N, M,
and K (1 ≤ N, M ≤ 104) (1 ≤ K ≤ 20).

The next K lines each contains distinct pair of integers x, y (1 ≤ x ≤ N) (1 ≤ y ≤ M) -
where (x, y) is the coordinate of the bomb.

Output

For each test case, output a line containing a single integer - the number of sub-rectangles that don’t contain any bombs.

Example

input
3
2 2 1
2 2
6 6 2
5 2
2 5
10000 10000 1
1 1


output
5
257
2500499925000000


题意:n*m大小的矩阵中,有k个地雷,求不含有地雷的所有矩形的总数

解题思路:先算出矩阵的总个数,将k个地雷先按x轴排序,再按y轴排序,然后分别统计包含前1,2……k个点的矩阵,总个数减去这些即是答案

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

int n, m, k;
struct node
{
int x, y;
bool operator < (const node &a)const
{
if (a.x != x) return x < a.x;
return y < a.y;
}
}a[50];

int main()
{
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d%d%d", &n, &m, &k);
LL ans = 1LL * (1LL + n)*n / 2 * 1LL * (1LL + m)*m / 2;
for (int i = 1; i <= k; i++) scanf("%d %d", &a[i].x, &a[i].y);
sort(a + 1, a + 1 + k);
for (int i = 1; i <= k; i++)
{
int lm = 1, rm = m, row = a[i].x;
for (int j = i + 1; j <= k; j++)
{
if (a[j].x != a[j - 1].x) ans -= 1LL * a[i].x*(a[j].x - row)*(a[i].y - lm + 1)*(rm - a[i].y + 1), row = a[j].x;
if (a[j].y > a[i].y) rm = min(rm, a[j].y - 1);
else lm = max(lm, a[j].y + 1);
}
ans -= 1LL * a[i].x*(n - row + 1)*(a[i].y - lm + 1)*(rm - a[i].y + 1);
}
printf("%lld\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: