ZOJ - 4082: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
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 .
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 (双指针)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- CodeForces 689E Mike and Geometry Problem (离散化+组合数)
Mike and Geometry Problem 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/I Description M ...
- Codeforces Gym 100338B Geometry Problem 计算几何
Problem B. Geometry ProblemTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudg ...
- you can Solve a Geometry Problem too(hdoj1086)
Problem Description Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare ...
- (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/ ...
随机推荐
- python3 get/post/使用代理/自定义header/自定义Cookie
说明:urllib发送http请求并不是很人性化,更推荐使用在urllib基础上封装的.python2和python3都兼容的requests模块,移步查看. 一.get请求 get请求就是在构造Re ...
- ubuntu默认启动方式修改 psensor命令
Check UUID sudo blkid Then sudo gedit /etc/default/grub & to pull up the boot loader configurati ...
- 补交第二周作业:学习ka li
在老师给的虚拟机上安装,试了n次都没有安装成功,百度上的说法也是众说纷纭. 之后重新安装了另一个版本的虚拟机,按照教程成功装上了ka li. 一. 安装VMtools:是为了方便宿主机与虚拟机间的文件 ...
- unity中让摄像机移动到鼠标点击的位置和鼠标控制平移视角
private Vector3 targetVector3; private float movespeed=0.5f; private bool IsOver = true; private Gam ...
- 笨办法06字符串(string)和文本
代码如下: # coding : utf-8 x = "There are %d types of people." % 10 binary = "binary" ...
- Spring Boot + Druid 监控数据库(三)
Druid可以做什么? 1) 可以监控数据库访问性能,Druid内置提供了一个功能强大的StatFilter插件,能够详细统计SQL的执行性能,这对于线上分析数据库访问性能有帮助. 2) 替换DBC ...
- Linux系统管理常用命令用法总结(1)
1.usermod可用来修改用户帐号的各项设定. usermod [-LU][-c <备注>][-d <登入目录>][-e <有效期限>][-f <缓冲天数& ...
- c++的读入txt文件(转)
因为学姐的项目需要,要用到excel的读入读出,百度过后发现txt的读入读出比较简单,于是,我采用了先把excel转成txt,然后再读入. 方法是csdn上的天使的原地址: https://blo ...
- 利用SMB jcifs实现对windows中的共享文件夹的操作
需求是在本地上传文件到服务器上,服务器是windows的,使用共享文件夹提供权限给你的. 利用第三方: CIFS (Common Internet File System) SMB(Server Me ...
- kubernetes 环境搭建(ubuntu16.04)
通过kubeadm安装kubernetes的教程:1: 首先在每台机器上安装: docker(1.12), kubeadm(1.6), kubectl, kubelet, kubernetes-cni ...