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 Problem
Time Limit: 4 Seconds Memory Limit: 65536 KB
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 次查询:查询 到达特殊点距离总和为 Ci 的点的数量。
只有在特殊点的右上方的点才会产生距离。
解题思路:
过特殊点作斜率为 -1 的直线,发现这些线上的点到特殊点距离相等,也就是说同一X,只有一个点满足距离和为 Ci。
选择最左下角的点作为参考点(假设虚拟一个(0,0))。
那么平面上坐标为 (x,y)的点所产生的距离和 C = (x+y)*cnt-sum;
其中 cnt 为 该点左下方的特殊点个数,sum为这些特殊点到参考点的距离总和。
(也就是相当于 (x, y)到参考点的距离 - 特殊点 i 到参考点的距离 = (x,y)到特殊点的距离。)
很显然x,y具有线性关系,随着 x 增大 y 减小,每次查询遍历一遍 x 而 y 的值会随着 x 的增加而相应减少,总负责度 O(Q*N)约为 1e6;
AC code:
#include <set>
#include <map>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 1e5+;
struct Node
{
LL x, y;
}node[MAXN];
LL C[];
bool cmp(Node a, Node b)
{
if(a.x != b.x) return a.x < b.x;
else return a.y > b.y;
}
LL vis[MAXN];
LL vis_sum[MAXN];
int N, M, Q; int main()
{
int T_case;
scanf("%d", &T_case);
while(T_case--){
scanf("%d %d", &N, &M);
for(int i = ; i <= M; i++){
scanf("%lld %lld", &node[i].x, &node[i].y);
}
scanf("%d", &Q);
for(int i = ; i <= Q; i++){
scanf("%lld", &C[i]);
}
//sort(C+1, C+1+Q); sort(node+, node+M+, cmp);
LL ty = N, tx = ;
LL cnt = , sum = , ans = ;
for(int i = ; i <= Q; i++){
cnt = ; sum = ; ans = ;
int top = ;
memset(vis, , sizeof(vis));
memset(vis_sum, , sizeof(vis_sum));
tx = ;ty = N;
while(tx <= N){
while(top+ <= M && node[top+].x <= tx){
top++;
if(node[top].y <= ty){
cnt++;
vis[node[top].y]++;
sum+=node[top].x+node[top].y;
vis_sum[node[top].y]+=tx;
//top++;
}
//else break;
//puts("zjy");
} while(cnt*(tx+ty)-sum > C[i]){
cnt-=vis[ty];
sum-=(vis_sum[ty]+vis[ty]*ty);
ty--;
}
if(cnt*(tx+ty)-sum == C[i]) ans++;
tx++;
} printf("%lld", ans);
if(i < Q) printf(" ");
else puts("");
}
}
return ;
}
ZOJ Monthly, January 2019 Little Sub and his Geometry Problem 【推导 + 双指针】的更多相关文章
- ZOJ Monthly, January 2019 Little Sub and his Geometry Problem ZOJ4082(模拟 乱搞)
在一次被自己秀死... 飞机 题目: 给出N,K, Q; 给出一个N*N的矩阵 , 与K个特殊点 , 与Q次查询 , 每次查询给出一个C , 问 在这个N*N矩阵中 , 有多少的点是满足这样的一个关 ...
- ZOJ Monthly, January 2019
A: Little Sub and Pascal's Triangle Solved. 题意: 求杨辉三角第n行奇数个数 思路: 薛聚聚说找规律,16说Lucas 答案是 $2^p \;\;p 为 n ...
- ZOJ Monthly, January 2019 Little Sub and Isomorphism Sequences 【离线离散化 + set + multiset】
传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5868 Little Sub and Isomorphism Seque ...
- ZOJ Monthly, January 2019 I Little Sub and Isomorphism Sequences(set 妙用) ZOJ4089
写这篇博客来证明自己的愚蠢 ...Orz 飞机 题意:给定你个数组,以及一些单点修改,以及询问,每次询问需要求得,最长的字串长度,它在其他位置存在同构 题解:经过一些奇思妙想后 ,你可以发现问题是传 ...
- ZOJ Monthly, January 2018 训练部分解题报告
A是水题,此处略去题解 B - PreSuffix ZOJ - 3995 (fail树+LCA) 给定多个字符串,每次询问查询两个字符串的一个后缀,该后缀必须是所有字符串中某个字符串的前缀,问该后缀最 ...
- matrix_2015_1 138 - ZOJ Monthly, January 2015
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3844 第一个,n个数,每次操作最大数和最小数都变成他们的差值,最后n个数相 ...
- ZOJ Monthly, January 2018
A 易知最优的方法是一次只拿一颗,石头数谁多谁赢,一样多后手赢 #include <map> #include <set> #include <ctime> #in ...
- ZOJ Monthly, January 2018 Solution
A - Candy Game 水. #include <bits/stdc++.h> using namespace std; #define N 1010 int t, n; int a ...
- ZOJ Monthly, January 2019-Little Sub and Pascal's Triangle
这个题的话,它每行奇数的个数等于该行行号,如果是0开始的,就该数的二进制中的1的个数,设为k,以它作为次数,2k就是了. #include <stdio.h> int main() { i ...
随机推荐
- SQL 工具系列二
1.RedGate 工具 SQL Prompt 脚步智能提示工具 脚步提示工具,轻松写入,编辑和探索SQL: SQL Prompt能根据数据库的对象名称,语法和用户编写的代码片段自动进行检索,智能的为 ...
- golang的xml、json解析
xml golang的xml处理主要应用Unmarshal.Marshal方法实现,解析一个xml到struct如下,首先是xml文件: <?xml version="1.0" ...
- 基于Node.js的ghost开源博客平台
Ghost 是一套基于Node.js 构建的开源博客平台(Open source blogging platform),具有易用的书写界面和体验. 1.安装node windows 下安装npm:ht ...
- apache和tomcat搭建集群
最近在学习简单的apache服务器和两个tomcat一起搭建集群,这里简单记录一下 1.准备工作 ①搭建一个可以运行的web项目 用maven搭建springmvc项目 ,只要将这里面的web.xml ...
- struts2 国际化语言转换
学习struts2,了解了使用struts2的配置文件可以走向国际化,实现页面的语言转换.我已中文和英文为例,简单的实现登录页面的国际化 废话不多说,上代码 一,login.jsp页面 使用s标签&l ...
- SQL Server UDF to pad a string
http://www.mssqltips.com/sqlservertip/1738/sql-server-udf-to-pad-a-string/ declare @l varchar(50) se ...
- Java基础知识错误分析
答案:A,C 解析: 题目2: 答案:B 解析: 题目3: 答案:A 解析: 题目4: 答案:D 解析: 题目5: 答案:C 题目六: 答案:C 解析:
- 汉诺塔matlab实现
Matlab的递归调用,好久不用matlab了,练练手. global handCount; handCount = 1; huuotsun(1, 2, 3, 3) function hu ...
- Ubuntu 下 /etc/resolv.conf文件总是自动清除问题的解决方案
最近学习Linux,在虚拟机中安装的是Ubuntu操作系统,用了几天发现Ubuntu无法上网,打开命令终端,输入命令: ping www.baidu.com -c2 结果显示名称无法识别,而直接与宿主 ...
- JBOSS参数调优
阅读目录 JBOSS参数调优 jvm调优讲解1 JVM调优讲解2 JVM常见配置汇总 JBOSS生产环境下JVM调优 JBOSS瘦身 JBoss性能优化:内存紧张的问题终于解决了(转载)--- ...