Time Limit:3000MS | Memory Limit:Unknown | 64bit IO Format:%lld & %llu |
Description


Mr. Kim knows that the residents of the two apartments frequently have a meeting. So, he thinks that the best location of a new restaurant is halfway between two apartments. Considering lease expenses and existing restaurants, however, he can't select the optimal location unconditionally. Hence he decides to regard a location satisfying the following condition as a good place. Let dist(p, q) be the distance between p and q.
A location p is a good place if for each existing restaurant's location q, dist(p, A) < dist(q, A) or dist(p, B) < dist(q, B). In other words, p is not a good place if there exists an existing restaurant's location q such that dist(p, A)dist(q, A) and dist(p, B)
dist(q, B).
In the above figure, the location (7, 4) is a good place. But the location p = (4, 6) is not good because there is no apartment which is closer to p than the restaurant at q = (3, 5), i.e., dist(p, A) = 5dist(q, A) = 3 and dist(p, B) = 7
dist(q, B) = 7. Also, the location (0, 0) is not good due to the restaurant at (0, 5). Notice that the existing restaurants are positioned regardless of Mr. Kim's condition.
Given n locations of existing restaurants, write a program to compute the number of good places for a new restaurant.
Input
Your program is to read the input from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case starts with a line containing two integers M and n ( 2M
60, 000 and 2
n
50, 000), which represent the size of a city map and the number of existing restaurants, respectively. The (i + 1)-th line of a test case contains two integers xi and yi ( i = 1, 2,..., n and 0
xi, yi < M), which represents the coordinate of the i-th existing restaurant. Assume that all restaurants have distinct coordinates and that the two apartments A and B are positioned at the locations of 1-st restaurant and 2-nd restaurant. Notice that A and B are placed on the same horizontal line.
Output
Your program is to write to standard output. Print exactly one line for each test case. Print the number of good places which can be found in a given city map.
The following shows sample input and output for two test cases.
Sample Input
2 6 3 1 3 4 3 0 2 11 11 0 5 10 5 4 9 2 8 7 8 5 6 3 5 5 3 3 2 7 2 9 1
Sample Output
2 16
Source
题意:
有一个M*M的网格,坐标[0...M-1,0...M-1] 网格里面有两个y坐标相同的宾馆A和B,以及n个餐厅,宾馆AB里面各有一个餐厅,编号1,2,其他餐厅编号3-n,现在你打算新开一家餐厅,需要考察一下可能的位置,一个位置p是“好位置”的条件是:当且仅当对于已有的每个餐厅q,要么p比q离A近,要么p比q离B近,即dist(p,A) < dist(q,A) 或者 dist(p,B) < dist(q,B) 问“好位置”的个数
思路:
首先能够肯定的是好位置在AB之间,所以就是考虑AB中间的每一列有多少位置是好位置
设A,B坐标为 (x1, y0) (x2, y0) (y坐标相同)
对于AB连线之间的一个位置 (xi, y0), 设他所在列的好位置个数为h[i]
则h[i] = min(abs(yj-y0), h[i-1]+1, h[i+1]+1) - 1; yj为i列上的所有餐厅的y坐标
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>
using namespace std;
//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif
#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)
const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));
const int maxm = 60000 + 20;
int h[maxm];
int main() {
int T;
scanf("%d", &T);
while(T--) {
int m, n;
scanf("%d%d", &m, &n);
int x1, y1, x2, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
if(x1 > x2) swap(x1, x2);
for(int i=x1; i<=x2; i++) h[i] = m;
for(int i=3; i<=n; i++) {
int x, y;
scanf("%d%d", &x, &y);
h[x] = min(h[x], abs(y-y1));
}
h[x1] = 0;
for(int i=x1+1; i<x2; i++) {
h[i] = min(h[i], h[i-1]+1);
}
h[x2] = 0;
for(int i=x2-1; i>x1; i--) {
h[i] = min(h[i], h[i+1]+1);
}
LL ans = 0;
for(int i=x1+1; i<x2; i++) if(h[i] > 0) {
ans++;
ans += min(y1, h[i]-1);
ans += min(m-y1, h[i]-1);
}
P64I(ans);
}
return 0;
}