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/ ...
随机推荐
- java基础语法运算符
1.1 算数运算符++.--的使用 在一般情况下,算数运算符不会改变参与计算的变量值.而是在原有变量值不变的情况下,计算出新的值.但是有些操作符会改变参与计算的变量的值, ...
- Dagger2不自动生成daggerXXXcomponent
在Fragment里面初始化dagger2创建对象时,不自动生成daggerXXXcomponent. 百思不得其解,后来发现是import android.app.Fragment;所以不自动生成. ...
- 锚点 , angular 锚点 vue锚点
因为最近在开发angular,自己有路由 用window.location跳到默认路由,查了半天用angular方式不好解决 ,so 原生走起 START scrollIntoView是一个与页面(容 ...
- SpringBoot添加webapp目录
一.文章简述 使用IDEA工具创建的SpringBoot项目本身是没有webapp目录的.如果我们想要添加webapp目录的话,可以手动添加. 二.操作步骤 1)点击IDEA右上角的Project S ...
- ubuntu16.10安装搜狗输入法
一.搜狗输入法安装 1.首先到搜狗输入法官网下载搜狗输入法,下载的是个deb文件. 搜狗输入法Linux版下载地址:http://pinyin.sogou.com/linux/?r=pinyin 2. ...
- 关于scratch导出的flash画质很差的问题解决方案
Scratch的分辨率是480*360,因此把scratch文件转变为flash时,因影像和画质很差,把flash插入到ppt幻灯片后,影像和画质仍然得不到保证.经过不断摸索,这个问题终于得到解决,关 ...
- java 的类型转换方式
隐式转换 byte等整型转int,最高位(符号位保留),中间补0 byte bt=-13; bt 源码:1000 1101 反码:1111 0010 补码:1111 0011 int it=bt; i ...
- SpringMVC中文乱码的解决办法
中文乱码分类: (1)按照请求分类: GET请求乱码 POST请求乱码 (2)按照乱码位置分类 从前台传到后台的数据乱码(存储到数据库中的数据乱码) 从后台传到前台的数据乱码(显示在页面的数据乱码) ...
- C++ string类insert用法总结
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- 《python》join、守护进程、锁/信号量/事件、进程队列
一.multiprocess.process模块 1.join方法 阻塞主进程,等待子进程执行完毕再放开阻塞 import time import random from multiprocessin ...