传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5861
Little Sub and his Geometry Problem
Little Sub loves math very much, and has just come up with an interesting problem when he is working on his geometry homework.
It is very kind of him to share this problem with you. Please solve it with your coding and math skills. Little Sub says that even Mr.Potato can solve it easily, which means it won't be a big deal for you.
The problem goes as follows:
Given two integers and , and points with their Euclidean coordinates on a 2-dimensional plane. Different points may share the same coordinate.
Define the function
You are required to solve several queries.
In each query, one parameter is given and you are required to calculate the number of integer pairs such that and .
Input
There are multiple test cases. The first line of the input contains an integer (), indicating the number of test cases. For each test case:
The first line contains two positive integers and ().
For the following lines, the -th line contains two integers and (), indicating the coordinate of the -th point.
The next line contains an integer (), indicating the number of queries.
The following line contains integers (), indicating the parameters for each query.
It's guaranteed that at most 20 test cases has .
Output
For each test case, output the answers of queries respectively in one line separated by a space.
Please, DO NOT output extra spaces at the end of each line, or your answer may be considered incorrect!
Sample Input
2 4 2 1 1 2 3 5 1 2 3 4 5 15 5 1 1 7 3 5 10 8 6 7 15 3 25 12 31
Sample Output
2 3 4 1 2 5 11 5
题意概括:
N*N的大小的二维平面, M 个特殊点。
Q 次查询:查询 到达特殊点距离总和为 Ci 的点的数量。
只有在特殊点的右上方的点才会产生距离。
解题思路:
过特殊点作斜率为 -1 的直线,发现这些线上的点到特殊点距离相等,也就是说同一X,只有一个点满足距离和为 Ci。
选择最左下角的点作为参考点(假设虚拟一个(0,0))。
那么平面上坐标为 (x,y)的点所产生的距离和 C = (x+y)*cnt-sum;
其中 cnt 为 该点左下方的特殊点个数,sum为这些特殊点到参考点的距离总和。
(也就是相当于 (x, y)到参考点的距离 - 特殊点 i 到参考点的距离 = (x,y)到特殊点的距离。)
很显然x,y具有线性关系,随着 x 增大 y 减小,每次查询遍历一遍 x 而 y 的值会随着 x 的增加而相应减少,总负责度 O(Q*N)约为 1e6;
AC code:
1 #include <set> 2 #include <map> 3 #include <cstdio> 4 #include <cstring> 5 #include <iostream> 6 #include <algorithm> 7 #define INF 0x3f3f3f3f 8 #define LL long long 9 using namespace std; 10 const int MAXN = 1e5+10; 11 struct Node 12 { 13 LL x, y; 14 }node[MAXN]; 15 LL C[20]; 16 bool cmp(Node a, Node b) 17 { 18 if(a.x != b.x) return a.x < b.x; 19 else return a.y > b.y; 20 } 21 LL vis[MAXN]; 22 LL vis_sum[MAXN]; 23 int N, M, Q; 24 25 26 int main() 27 { 28 int T_case; 29 scanf("%d", &T_case); 30 while(T_case--){ 31 scanf("%d %d", &N, &M); 32 for(int i = 1; i <= M; i++){ 33 scanf("%lld %lld", &node[i].x, &node[i].y); 34 } 35 scanf("%d", &Q); 36 for(int i = 1; i <= Q; i++){ 37 scanf("%lld", &C[i]); 38 } 39 //sort(C+1, C+1+Q); 40 41 42 sort(node+1, node+M+1, cmp); 43 LL ty = N, tx = 1; 44 LL cnt = 0, sum = 0, ans = 0; 45 for(int i = 1; i <= Q; i++){ 46 cnt = 0; sum = 0; ans = 0; 47 int top = 0; 48 memset(vis, 0, sizeof(vis)); 49 memset(vis_sum, 0, sizeof(vis_sum)); 50 tx = 1;ty = N; 51 while(tx <= N){ 52 while(top+1 <= M && node[top+1].x <= tx){ 53 top++; 54 if(node[top].y <= ty){ 55 cnt++; 56 vis[node[top].y]++; 57 sum+=node[top].x+node[top].y; 58 vis_sum[node[top].y]+=tx; 59 //top++; 60 } 61 //else break; 62 //puts("zjy"); 63 } 64 65 while(cnt*(tx+ty)-sum > C[i]){ 66 cnt-=vis[ty]; 67 sum-=(vis_sum[ty]+vis[ty]*ty); 68 ty--; 69 } 70 if(cnt*(tx+ty)-sum == C[i]) ans++; 71 tx++; 72 } 73 74 printf("%lld", ans); 75 if(i < Q) printf(" "); 76 else puts(""); 77 } 78 } 79 return 0; 80 }