Problem Description
Matt has a company, Always Cook Mushroom (ACM), which produces high-quality mushrooms. 



ACM has a large field to grow their mushrooms. The field can be considered as a 1000 * 1000 grid where mushrooms are grown in grid points numbered from (1, 1) to (1000, 1000). Because of humidity and sunshine, the productions in different grid points are not
the same. Further, the production in the grid points (x, y) is (x + A)(y + B) where A, B are two constant. 



Matt,the owner of ACM has some queries where he wants to know the sum of the productions in a given scope(include the mushroom growing on the boundary). In each query, the scope Matt asks is a right angled triangle whose apexes are (0, 0), (p, 0), (p, q) 1<=p,
q<=1000. 



As the employee of ACM, can you answer Matt’s queries?

 
Input
The first line contains one integer T, indicating the number of test cases.



For each test case, the first line contains two integers:A, B(0<=A, B<=1000).



The second line contains one integer M(1<=M<=10^5), denoting the number of queries.



In the following M lines, the i-th line contains three integers a, b, x (1<=a, b<=10^6, 1<=x<=1000), denoting one apex of the given right angled triangle is (x, 0) and the slope of its base is (a, b). It is guaranteed that the gird points in the given right
angled triangle are all in valid area, numbered from (1, 1) to (1000, 1000).
 
Output
For each test case, output M + 1 lines.



The first line contains "Case #x:", where x is the case number (starting from 1) 



In the following M lines, the i-th line contains one integer, denoting the answer of the i-th query.
 
Sample Input
2
0 0
3
3 5 8
2 4 7
1 2 3
1 2
3
3 5 8
2 4 7
1 2 3
 
Sample Output
Case #1:
1842
1708
86
Case #2:
2901
2688
200
 
Source

题意:给定一个1000x1000的点阵。m组询问。每次询问一个由(0,0)、(x,0)点一以及从原点出发的方向向量(a,b)构成的直角三角形包围的点的权值和。

思路: 预处理出这1e6个点的极角关系序,离线。将询问也按(a,b)的极角排序。然后只需想象一根表针在逆时针的扫。把扫过的点的权值加到树状数组中,对于每个询问也不过一个前缀和。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
typedef long long ll;
using namespace std;
const int maxn = 1005;
const int inf = 1e5+5; struct Point {
ll a, b;
double s;
} p[maxn*maxn];
struct Query {
ll a, b, x, id;
double s;
} q[maxn*maxn];
ll bit[maxn];
ll ans[inf], Index[inf];
int cnt; void scan(ll &x) {
char c;
while ((c = getchar()) && (c < '0' || c > '9')) ;
x = c - '0';
while ((c = getchar()) && (c >= '0' && c <= '9'))
x = x * 10 + c - '0';
} void out(ll x) {
if (x > 9)
out(x/10);
putchar(x%10+'0');
} inline int lowbit(int x) {
return x & -x;
} inline void add(int x, int val) {
while (x <= 1000) {
bit[x] += val;
x += lowbit(x);
}
} inline ll sum(int x) {
ll tmp = 0;
while (x > 0) {
tmp += bit[x];
x -= lowbit(x);
}
return tmp;
} bool cmp1(Point x, Point y) {
return x.s < y.s;
} bool cmp2(Query x, Query y) {
if (x.s == y.s)
return x.x < y.x;
return x.s < y.s;
} void init() {
for (int i = 1; i <= 1000; i++)
for (int j = 1; j <= 1000; j++) {
p[cnt].a = i;
p[cnt].b = j;
p[cnt++].s = 1.0 * j / i;
}
sort(p, p+cnt, cmp1);
} int main() {
cnt = 0;
ll A, B, m;
init();
int t, cas = 1;
scanf("%d", &t);
while (t--) {
memset(bit, 0, sizeof(bit));
scan(A), scan(B), scan(m);
for (int i = 0; i < m; i++) {
scan(q[i].a), scan(q[i].b), scan(q[i].x);
q[i].s = 1.0 * q[i].b / q[i].a;
q[i].id = i;
} sort(q, q + m, cmp2);
for (int i = 0; i < m; i++) {
Index[q[i].id] = i;
}
cnt = 0;
printf("Case #%d:\n", cas++);
for (int i = 0; i < m; i++) {
while (p[cnt].s <= q[i].s) {
add(p[cnt].a, (p[cnt].a+A) * (p[cnt].b + B));
cnt++;
}
ans[i] = sum(q[i].x);
}
for (int i = 0; i < m; i++) {
out(ans[Index[i]]);
printf("\n");
}
}
return 0;
}

HDU Always Cook Mushroom (极角排序+树状数组)的更多相关文章

  1. hdu 6203 ping ping ping(LCA+树状数组)

    hdu 6203 ping ping ping(LCA+树状数组) 题意:给一棵树,有m条路径,问至少删除多少个点使得这些路径都不连通 \(1 <= n <= 1e4\) \(1 < ...

  2. hdu 5869 区间不同GCD个数(树状数组)

    Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

  3. 【 HDU - 4456 】Crowd (二维树状数组、cdq分治)

    BUPT2017 wintertraining(15) #5A HDU 4456 题意 给你一个n行n列的格子,一开始每个格子值都是0.有M个操作,p=1为第一种操作,给格子(x,y)增加z.p=2为 ...

  4. hdu 4911 求逆序对数+树状数组

    http://acm.hdu.edu.cn/showproblem.php?pid=4911 给定一个序列,有k次机会交换相邻两个位置的数,问说最后序列的逆序对数最少为多少. 实际上每交换一次能且只能 ...

  5. HDU 6318 - Swaps and Inversions - [离散化+树状数组求逆序数][杭电2018多校赛2]

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=6318 Problem Description Long long ago, there was an ...

  6. HDU 5869 Different GCD Subarray Query 树状数组 + 一些数学背景

    http://acm.hdu.edu.cn/showproblem.php?pid=5869 题意:给定一个数组,然后给出若干个询问,询问[L, R]中,有多少个子数组的gcd是不同的. 就是[L, ...

  7. hdu 1394 Minimum Inversion Number(树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给你一个0 — n-1的排列,对于这个排列你可以将第一个元素放到最后一个,问你可能得到的最 ...

  8. HDU 4630 No Pain No Game 树状数组+离线操作

    题意:给一串数字,每次查询[L,R]中两个数的gcd的最大值. 解法:容易知道,要使取两个数让gcd最大,这两个数最好是倍数关系,所以处理出每个数的所有倍数,两两间根据倍数关系形成一条线段,值为该数. ...

  9. HDU 4746 莫比乌斯反演+离线查询+树状数组

    题目大意: 一个数字组成一堆素因子的乘积,如果一个数字的素因子个数(同样的素因子也要多次计数)小于等于P,那么就称这个数是P的幸运数 多次询问1<=x<=n,1<=y<=m,P ...

随机推荐

  1. 再论C++之垃圾回收(GC)

    本文已经迁移到:http://cpp.winxgui.com/cn:garbage-collection-in-cplusplus 使用智能指针(smart pointers) http://www. ...

  2. Nginx 作为反向Proxy 的优化要点

    原文地址:http://my.oschina.net/hyperichq/blog/405421 常用优化要点 当nginx用于反向代理时,每个客户端将使用两个连接: 一个用于响应客户端的请求,另一个 ...

  3. 为免费app嵌入Admob广告

    为免费app嵌入Admob广告,进而获得广告收入. 1.http://www.admob.com/注册一个帐号, 添加Add Mobile Site/app,输入相关信息后,提交完成, 下载Andro ...

  4. 【JVM】调优笔记3-----JVM参数配置 JDK1.8

    一.关于JVM参数配置,有多种途径. 1.在tomcat中直接配置的 打开tomcat的安装目录, 在bin下修改catalina.bat文件 添加如下: set "JAVA_OPTS=-X ...

  5. appium+python自动化61-中文输入乱码问题解决

    前言 在夜神模拟器上输入中文,发现是乱码,将unicodeKeyboard和resetKeyboard参数设置为True了,发现还是没法解决. 打开手机设置语言和输入法,发现找不到Appium And ...

  6. PHP 7.0 5.6 下安裝 phpLDAPadmin 发生错误的修正方法

    在稍具規模的網路環境中, 網管時常選用 LDAP 來進行帳號的統整管理, 一方面提供管理便利度, 另一方面使用者也不必因為不同系統而記憶不同帳號, phpLDAPadmin 是一套常見的 LDAP 管 ...

  7. git打pach包

    在开发中,我们发出的基线版本号常常会有一些bug须要修复,假设採用本地上库,然后再给用户新的版本号,可能会费时费力,而假设给用户我们改动后的代码让用户一行一行合入本地,也显的比較落后,假设用户那边也使 ...

  8. Action过滤器使用实例(一)

    1.实例一 /// <summary> /// 需要用户登陆的 action,执行提前验证 /// </summary> public class LoginFilterAtt ...

  9. 理解Python命名机制

    理解Python命名机制 本文最初发表于恋花蝶的博客(http://blog.csdn.net/lanphaday),欢迎转载,但必须保留此声明且不得用于商业目的.谢谢. 引子 我热情地邀请大家猜测下 ...

  10. 5.synchronized锁重入

    package demo1; /** * synchronized锁重入 * Created by liudan on 2017/6/5. */ public class MyThread5_sync ...