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. IOS快速入门

    http://www.cnblogs.com/wellsoho/p/4313312.html

  2. nginx配置location总结

    location匹配顺序 "="前缀指令匹配,如果匹配成功,则停止其他匹配 普通字符串指令匹配,顺序是从长到短,匹配成功的location如果使用^~,则停止其他匹配(正则匹配) ...

  3. Objective-C:深复制(拷贝)

    深复制:复制对象时,如果对象中包含对象类型的实例变量,要对对象类型的实例变量也要做对象复制.新对象中的对象类型实例变量和旧对象中的对象类型实例变量指的是不同的对象.不管任何一方实例变量对对象做修改,都 ...

  4. Informatica 常用组件Filter之三 创建FIL

    在 Designer 中,切换到 Mapping Designer 并打开映射. 选择"转换-创建". 选择"过滤器转换",然后输入新的转换名称.过滤器转换的命 ...

  5. poj 3131 Cubic Eight-Puzzle 双向广搜 Hash判重

    挺不错的题目,很锻炼代码能力和调试能力~ 题意:初始格子状态固定,给你移动后格子的状态,问最少需要多少步能到达,如果步数大于30,输出-1. 由于单向搜索状态太多,搜到二十几就会爆了,所以应该想到双向 ...

  6. iOS开发-UITableView表格优化

    之前的一篇文章大概讲述了一下UITableView的使用,UITableView在iOS的地位和ListView在Android中的地位基本上算是不相上下,关于ListView的优化网上的也有很多文章 ...

  7. android.net.Uri 简介 API

    android.net.Uri 简介 public abstract class android.net.Uri extends Object implements Parcelable, Compa ...

  8. AngularJS driective 封装 自动滚动插件

    1.ui-smooth-scroll.js文件内容 angular.module('app') .directive('uiSmoothScroll', ['$location', '$anchorS ...

  9. 6个原则、50条秘技提高HTML5应用及网站性能

    Jatinder Mann是微软Internet Explorer产品的一名项目经理,在BUILD 2012大会上,他做了题为“提高HTML5应用和网站性能的50条秘技(50 performance ...

  10. 如何解决SPD的缓存问题

      SPD有时候文件被缓存住了,表现为文件的最后更改时间不对,或者本来文件已经被check in了,但是显示check out状态,而此时如果选择check in, 就会提示文件没有被check ou ...