传送门: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 【推导 + 双指针】的更多相关文章

  1. ZOJ Monthly, January 2019 Little Sub and his Geometry Problem ZOJ4082(模拟 乱搞)

    在一次被自己秀死... 飞机 题目: 给出N,K, Q; 给出一个N*N的矩阵  , 与K个特殊点 , 与Q次查询 , 每次查询给出一个C , 问 在这个N*N矩阵中 , 有多少的点是满足这样的一个关 ...

  2. ZOJ Monthly, January 2019

    A: Little Sub and Pascal's Triangle Solved. 题意: 求杨辉三角第n行奇数个数 思路: 薛聚聚说找规律,16说Lucas 答案是 $2^p \;\;p 为 n ...

  3. 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 ...

  4. ZOJ Monthly, January 2019 I Little Sub and Isomorphism Sequences(set 妙用) ZOJ4089

    写这篇博客来证明自己的愚蠢 ...Orz  飞机 题意:给定你个数组,以及一些单点修改,以及询问,每次询问需要求得,最长的字串长度,它在其他位置存在同构 题解:经过一些奇思妙想后 ,你可以发现问题是传 ...

  5. ZOJ Monthly, January 2018 训练部分解题报告

    A是水题,此处略去题解 B - PreSuffix ZOJ - 3995 (fail树+LCA) 给定多个字符串,每次询问查询两个字符串的一个后缀,该后缀必须是所有字符串中某个字符串的前缀,问该后缀最 ...

  6. matrix_2015_1 138 - ZOJ Monthly, January 2015

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3844 第一个,n个数,每次操作最大数和最小数都变成他们的差值,最后n个数相 ...

  7. ZOJ Monthly, January 2018

    A 易知最优的方法是一次只拿一颗,石头数谁多谁赢,一样多后手赢 #include <map> #include <set> #include <ctime> #in ...

  8. ZOJ Monthly, January 2018 Solution

    A - Candy Game 水. #include <bits/stdc++.h> using namespace std; #define N 1010 int t, n; int a ...

  9. ZOJ Monthly, January 2019-Little Sub and Pascal's Triangle

    这个题的话,它每行奇数的个数等于该行行号,如果是0开始的,就该数的二进制中的1的个数,设为k,以它作为次数,2k就是了. #include <stdio.h> int main() { i ...

随机推荐

  1. 【tomcat资源映射本地路径配置】

    需要注意的是:eclipse默认的服务器配置是其内置的. 如果使用自定义的tomcate的配置文件需要在服务器中配置: 1:进入到tomcat根目录下,然后vim conf/server.xml,在& ...

  2. 分布式事务-Sharding 数据库分库分表

      Sharding (转)大型互联网站解决海量数据的常见策略 - - ITeye技术网站 阿里巴巴Cobar架构设计与实践 - 机械机电 - 道客巴巴 阿里分布式数据库服务原理与实践:沈询_文档下载 ...

  3. A space or line break was encountered after the "@" character. Only valid identifiers, keywords, comments, "(" and "{" are valid at the start of a code block and they must occur immediately following

    mvc 控制器调用分布视图出错,("A space or line break was encountered after the "@" character. Only ...

  4. Windbg 脚本命令简介 一

    Windbg  脚本命令简介 一 Windbg command r: registers的简写,可以显示或修改寄存器的值.浮点寄存器的值.定义别名变量. 可以显示当前线程下的寄存器值. The r c ...

  5. 关于利用MQ实现分布式事务的想法【转】

    转自:https://www.jianshu.com/p/bafb09954f18 假设:消息服务不丢消息 场景 服务A 服务B 服务C 消息服务Q 伪代码 服务A中 transaction{ A本地 ...

  6. IIS6服务器的请求流程(图文&源码)

    1.IIS 7开发与管理完全参考手册  http://book.51cto.com/art/200908/146040.htm 2.Web服务IIS 6   https://technet.micro ...

  7. asp.net 日期转换为大写汉字

    //年份转换为大写汉字 public static string numtoUpper(int num) { return "零壹贰叁肆伍陆柒捌玖"[num].ToString() ...

  8. js对象之间的继承

    js的对象之间的继承抛弃了原型与构造器的概念,而转为字面量对象之间进行属性拷贝的方式进行继承. 首先我们来写一个封装好的继承函数: function extend(parent){ var child ...

  9. org.apache.ibatis.binding.BindingException

    1.异常提示: org.apache.ibatis.binding.BindingException: Mapper method 'com.artup.dao.WorksDao.selectWork ...

  10. 纯js轮播图

    <div id="wrapper"> <div id="container"> <img src="http://ima ...