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. Qt一步一步实现插件通信(附源码)

      前一章已经实现了主程序调用加载插件功能,这一章描述主程序和插件间通信功能 说道Qt的通信必须要了解信号和槽的机制原理,这里不做论述,不清楚的同学去看看信号和槽机制 不废话直接上步骤,在上一章的基础 ...

  2. C++解析四-友员函数、内联函数、静态成员

    友元函数 类的友元函数是定义在类外部,但有权访问类的所有私有(private)成员和保护(protected)成员.尽管友元函数的原型有在类的定义中出现过,但是友元函数并不是成员函数.友元可以是一个函 ...

  3. net资源1

    .net core 例子 https://github.com/aspnet/Docs/tree/master/aspnetcore/fundamentals C#中使用Spire.docx操作Wor ...

  4. useful links about machine learning

    机器学习(Machine Learning)&深度学习(Deep Learning)资料(Chapter 1) 机器学习(Machine Learning)&深度学习(Deep Lea ...

  5. js onclick函数中传字符串参数的问题

    规则: 外变是“”,里面就是‘’外边是‘’,里边就是“”   示例: var a="111"; var html="<a onclick='selecthoods( ...

  6. springMVC的工作流程图

  7. requests(第三方模块) 请求、登录、下载网页

    import requests  #http://docs.python-requests.org/en/latest/api/ 说明文档 ''' requests.request(method,ur ...

  8. node(2) EventEmitter类 事件队列 事件和error事件方法

    事件队列的核心:事件触发与事件监听器功能的封装. // 引入 events 模块 var events = require('events'); // 创建 eventEmitter 对象 var e ...

  9. eclipse搭建ssm框架

    新建数据库ssm 建立数据库表user CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT , `sex` varchar(255) ...

  10. EMIF接口与FPGA的互联(转)

    reference: https://blog.csdn.net/ruby97/article/details/7539151 DSP6455的EMIFA模块 之前介绍了DSP6455的GPIO和中断 ...