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

 

where

 

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次询问,每次询问给出C,问平面上有多少个点满足:左下方的点到它的距离和为C。

思路:发现符合条件的点在每个X线上最多一个一个点满足,而且这些点具有单调性,即X递增,Y递减。假设当前点(i,j),左下方的点个数为tot,

那么距离和=(i+j)*tot-sum;      我们维护一些轴上的学习,然后双指针去搞就好了。

#include<bits/stdc++.h>
#define ll long long
#define rep(i,w,v) for(int i=w;i<=v;i++)
using namespace std;
const int maxn=;
struct in{
int x,y;
bool friend operator<(in w,in v){
if(w.x==v.x) return w.y<v.y; return w.x<v.x;
}
}s[maxn];
int num[maxn],tot,ans; ll sum,C,d[maxn];
int main()
{
int T,N,M,Q;
scanf("%d",&T);
while(T--){
scanf("%d%d",&N,&M);
rep(i,,M) {
scanf("%d%d",&s[i].x,&s[i].y);
}
sort(s+,s+M+); scanf("%d",&Q);
rep(kk,,Q){
scanf("%lld",&C);
int p=,q=N; rep(i,,N) num[i]=,d[i]=;
ans=tot=; sum=;
rep(i,,N){
while(p+<=M&&s[p+].x<=i) {
p++; if(s[p].y<=q){
tot++; num[s[p].y]++;
sum+=s[p].x+s[p].y;
d[s[p].y]+=i;
}
}
while((ll)tot*(i+q)-sum>C){
tot-=num[q];
sum-=(d[q]+(ll)num[q]*q);
q--;
}
if((ll)tot*(i+q)-sum==C) ans++;
}
if(kk!=) putchar(' ');
printf("%d",ans);
}
puts("");
}
return ;
}

ZOJ - 4082:Little Sub and his Geometry Problem (双指针)的更多相关文章

  1. ZOJ 4082 Little Sub and his Geometry Problem题解

    题意 f(u,v):x小于等于u且y小于等于v的点才对f有贡献,每个这样的点贡献(u-x)+() 思路 =f(u_2,v_2)" class="mathcode" src ...

  2. ZOJ Monthly, January 2019 Little Sub and his Geometry Problem 【推导 + 双指针】

    传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5861 Little Sub and his Geometry Prob ...

  3. HDU1086You can Solve a Geometry Problem too(判断线段相交)

    You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3 ...

  4. codeforces 361 E - Mike and Geometry Problem

    原题: Description Mike wants to prepare for IMO but he doesn't know geometry, so his teacher gave him ...

  5. hdu 1086 You can Solve a Geometry Problem too

    You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3 ...

  6. CodeForces 689E Mike and Geometry Problem (离散化+组合数)

    Mike and Geometry Problem 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/I Description M ...

  7. Codeforces Gym 100338B Geometry Problem 计算几何

    Problem B. Geometry ProblemTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudg ...

  8. you can Solve a Geometry Problem too(hdoj1086)

    Problem Description Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare ...

  9. (hdu step 7.1.2)You can Solve a Geometry Problem too(乞讨n条线段,相交两者之间的段数)

    称号: You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/ ...

随机推荐

  1. InnoDB支持的最大事务数量

    Innodb存储引擎有rollback segment,每个rollback segment中记录了1024个undo log segment,在每个undo log segment中进行undo页的 ...

  2. 关于TCP长连接和发送心跳的一些理解

    原因 TCP是一种有连接的协议,但是这个连接并不是指有一条实际的电路,而是一种虚拟的电路.TCP的建立连接和断开连接都是通过发送数据实现的,也就是我们常说的三次握手.四次挥手.TCP两端保存了一种数据 ...

  3. python字典和列表的高级应用

    1.将序列分解为单独的变量 1.1问题 包含n个元素的元组或列表.字符串.文件.迭代器.生成器,将它分解为n个变量 1.2方案 直接通过赋值操作 要求:变量个数要等于元素个数 当执行分解操作时,有时需 ...

  4. 小程序使用wxParse解析html

    转载至:http://blog.csdn.net/tang05709/article/details/54924675 这个是需要循环出显示的https://github.com/tonyzhan/c ...

  5. Oracle如何解决日期格式“01-3月 -18”的显示问题(3种处理方式)

    今天查询表数据还是出现上次那种问题,但是每次都要去调用转化函数,比较麻烦,所以找一下资料,得到几种方式解决oralce的日期数据显示格式 问题描述: 解决方法 1)方法1:调用Oracle函数转化成日 ...

  6. checkbox多选按钮变成单选

    <input id="a" type="checkbox"/><input id="b" type="check ...

  7. linux下iostat命令详解

    iostat用于输出CPU和磁盘I/O相关的统计信息 iostat语法 用法:iostat [ 选项 ] [ <时间间隔> [ <次数> ]] 常用选项说明: -c:只显示系统 ...

  8. HDU 1005 Number Sequence(数论)

    HDU 1005 Number Sequence(数论) Problem Description: A number sequence is defined as follows:f(1) = 1, ...

  9. js 操作dom

    childNodes 返回当前元素所有子元素的数组 parentNode 返回元素的父节点 document.createElement(tagName) 文档对象上的createElement方法可 ...

  10. C++ Templates 关于程序库的概念和通用工具

    using namespace std所谓的命名空间,就是一种将程序库名称封装起来的方法,它就像在程序库中竖立了一道围墙 标准程序库中有一部分,比如string classes,支持具体的错误处理,它 ...