传送门: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. wcf datetime json format

    wcf 内置的json序列化工具,有时需要替换,或者特殊情况的处理,需要修改. 我也遇到了Dto属性类型是datetime,json的反序列化 和 序列号不友好. 这是国外网站的一个方案:Replac ...

  2. jQuery 整体架构

    不同于 jQuery 代码各个模块细节实现的晦涩难懂,jQuery 整体框架的结构十分清晰,按代码行文大致分为如上图所示的模块. 初看 jQuery 源码可能很容易一头雾水,因为 9000 行的代码感 ...

  3. (原创).Net将EF运用于Oralce一 准备工作

    网上有很多EF运用于Oracle的博文,但是找了半天发现大多数博文大都语焉不详,于是决定自己折腾. 首先我的开发工具为vs2010,那么最适用于VS2010的EF版本为多少呢?答案是EF5.我在Sta ...

  4. Charles破解(转)

    NB的Charles是一款付费软件.但…本文将讲解如何破解Charles.注:虽然与文章内容相悖,但还是希望大家能购买正版软件,毕竟都是做软件开发的,何必自断生路,要有版权意识. 环境信息: Mac ...

  5. 第1章 初识CSS3

    什么是CSS3? CSS3是CSS2的升级版本,3只是版本号,它在CSS2.1的基础上增加了很多强大的新功能. 目前主流浏览器chrome.safari.firefox.opera.甚至360都已经支 ...

  6. Hibernate 注解(Annotations 二)一对一双向注解

    注解(Annotation),也叫元数据.一种代码级别的说明.它是JDK1.5及以后版本引入的一个特性,与类.接口.枚举是在同一个层次.它可以声明在包.类.字段.方法.局部变量.方法参数等的前面,用来 ...

  7. <Android 应用 之路> 百度地图API使用(2)

    简介 上一篇只是大致的提一下百度地图API的Android SDK的基本内容,然后抄袭一个官网上的Demo,今天看一下百度地图的第一部分,地图类型和基本的显示. 简单实战 不同类型地图的显示 //设置 ...

  8. CentOS7 下安装 SublimeText3

    原文地址:http://www.sundabao.com/centos-%E5%AE%89%E8%A3%85sublime-text-3/ Centos 安装Sublime text 3 Centos ...

  9. CentOS 7运维管理笔记(12)----GUI配置工具Webmin的安装

    早期的Linux系统管理员或是Web管理员在修改服务器配置时使用最多的就是vi编辑器,但是现在越来越多的基于GUI界面的配置工具出现了,毕竟人们还是喜欢以直接的可视化的方式来修改服务器的配置,而不是再 ...

  10. vs2010开发activex(MFC)控件/ie插件(一)

    原文:http://blog.csdn.net/yhhyhhyhhyhh/article/details/50782904  vs2010开发activex(MFC)控件:      第一步:生成ac ...