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 ...
随机推荐
- html元素两种分类。替换元素和不可替换元素;块级元素和行内元素
根据元素本身特点来分类: 替换元素替换元素根据其标签和属性来决定元素的具体显示内容.有<img><input><textarea><select>< ...
- asfda
从事前端的朋友应该对"字体图标"这个词汇不陌生,为了适应越来越挑剔的屏幕,网页图标和简单图案使用.png来搭建已经基本上被淘汰了.取而代之的是使用css3和svg来绘制,而对于网页 ...
- struts2随笔
1.struts.properties配置常量等同于struts.xml中配置(置于类加载路径下面)struts.multipart.maxSize文件上传最大大小struts.action.exte ...
- MySql 双实例安装
1.官网下载压缩包 https://dev.mysql.com/downloads/mysql/ 2.解压到D盘 3.修改my.ini 文件 [mysql] # 设置mysql客户端默认字符集 def ...
- GeneratedKeyHolder的作用:获得新建主键值
Spring利用GeneratedKeyHolder,提供了一个可以返回新增记录对应主键值的方法: int update(PreparedStatementCreator psc, KeyHolder ...
- 数据上下文中的AddOrUpdate方法
AddOrUpdate是扩展方法,需要添加引用 using System.Data.Entity.Migrations;
- Redis学习笔记1 -- 单机环境时分布式锁的使用
使用第三方开源组件Jedis实现Redis客户端,且只考虑Redis服务端单机部署的场景. 前言 分布式锁一般有三种实现方式:1. 数据库乐观锁:2. 基于Redis的分布式锁:3. 基于ZooKee ...
- git基本命令集合
以下内容不适合初学者 括号中表示需要自己填写 v1.0 git add git commit -m git commit -a -m git commit -amend git clone git l ...
- 从Pc转向H5开发遇到的适配问题思考
1.首先说滚动条 移动端开发在不设置任何适配和viewport宽度的情况下,以iphone5为例:屏幕界面的逻辑分辨率是320x568,在谷歌浏览器的界面下屏幕的可视宽度是980px(谷歌设置的,每个 ...
- javascript多浏览器的兼容
一.document.formName.item(”itemName”) 问题 问题说明:IE下,可以使用 document.formName.item(”itemName”) 或 document. ...