传送门: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. 告别Flash——那些年我们追过的FusionCharts

    随着FusionCharts最终放弃Flash这块蛋糕,不.或者已经不能叫做蛋糕了,现在Flash图表控件就只剩下AnyChart这一个独苗了,到底Flash还能走多远?这是Flash的末路吗? 众说 ...

  2. JMS - ActiveMQ的简单使用

    首先需要下载ActiveMQ,下面的链接给我们列出了所有版本:http://activemq.apache.org/download-archives.html每个版本为不同的OS提供了链接: 公司电 ...

  3. Spring 基础入门(一)

    本文代码部分来自于<spring in action>,本文讲的是使用!! Spring 是为了解决什么 一个框架的存在是为了解决某个问题的,那么Spring这个框架是为了解决什么问题呢? ...

  4. 2017年11月30日 C#TreeNode递归&邮箱验证&新用户窗体

    TreeNode递归 递归:自己调用自己一层一层的把数据找出来 TreeNode:可以创建多个节点 private void button1_Click(object sender, EventArg ...

  5. Redis实现主从复制(Master&Slave)

    由于前段时间公司项目比较赶,一直抽不出时间写博客,今天偷空写一篇吧.前面给大家讲解了单机版redis的基本操作,现在继续给大家讲解一下Redis的进阶部分,主从复制和读写分离. 一.Master&am ...

  6. Maven打包时,不包含jar包

    在给Maven项目打war包时,如果不想把依赖中的jar包也包含进去,可以在plugins中加入 <span style="white-space:pre"> < ...

  7. oracle 基础笔记

    sqlplus / as sysbda;-- 运行命令conn scott/tiger [as sysdba];-- 登录show user;查看当前用户alter user scott accoun ...

  8. AngularJS之控制器

    控制器在Angularjs中的作用是增强视图,它实际就是一个函数,用来向视图中的作用域添加额外的功能,我们用它来给作用域对象设置初始状态,并添加自定义行为. 当我们在页面上创建一个控制器时,Angul ...

  9. visual studio code断点调试react

    在项目配置文件   .vscode\launch.json 中添加:   "sourceMaps": true,   "skipFiles": [   &quo ...

  10. 使用cocostudio 需要在Android.mk文件的配置

    直接贴上Android.mk文件吧. 对了,是cocos2d3.0的,不知道2.x是否一样. LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LO ...