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. vue 父组件通过props向子组件传递数据/方法的方式

    参考网址:https://segmentfault.com/a/1190000010507616 下面栗子中, callback是传递父组件的方法, mutationName是传递父组件的数据, Ap ...

  2. weblogic控制台用户名密码修改

    1.记得用户名密码但想修改密码修改方法 保存后立即生效,即你退出后即能以新密码登录:但由于启动的用户名密码和登录的用户名密码是同一个,所以我们需要去修改DOMAIN_HOME/servers/serv ...

  3. commonJS 和 ES6 模块化的不同

    commonjs 导出 module.exports={ add:function(){ console.log('add测试') } } 导入 var add=require('./add.js') ...

  4. LY.JAVA面向对象编程.工具类中使用静态、说明书的制作过程、API文档的使用过程

    2018-07-08 获取数组中的最大值 某个数字在数组中第一次出现时的索引 制作说明书的过程 对工具类的使用 获取数组中的最大值 获取数字在数组中第一次出现的索引值 API的使用过程 Math

  5. API服务网关(Zuul)

    技术背景 前面我们通过Ribbon或Feign实现了微服务之间的调用和负载均衡,那我们的各种微服务又要如何提供给外部应用调用呢. 当然,因为是REST API接口,外部客户端直接调用各个微服务是没有问 ...

  6. jsp下载excel文件

    jsp下载excel文件的的实现方法很多,今天也遇到这个问题,乱敲了一阵,终于搞定了,记下来和朋友们分享吧. 假设需要下载excel文件的jsp页面名为:down.jsp 对应的后台action名为: ...

  7. Spring注入,Ioc的具体配置

    Spring框架的IOC注入: 一.Java部分代码: Person实体类: package com.ioc; import java.util.List; import java.util.Map; ...

  8. bzoj1968

    题解: 显然每一个数对答案的贡献为n/i 代码: #include<bits/stdc++.h> using namespace std; int n; int main() { scan ...

  9. java⑩

    1.for循环: for循环语法 for(表达式1;表达式2;表达式3){ 循环体4} 表达式1:初始化变量 只执行一次!表达式2:循环条件 满足条件进入循环体4表达式3:迭代变量 如果循环体 中只有 ...

  10. Linux3.10.0块IO子系统流程(3)-- SCSI策略例程

    很长时间以来,Linux块设备使用了一种称为“蓄流/泄流”(plugging/unplugging)的技术来改进吞吐率.简单而言,这种工作方式类似浴盆排水系统的塞子.当IO被提交时,它被储存在一个队列 ...