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;
}

最新文章

  1. python文件读写的学习
  2. BizTalk开发系列(十一) 在Orchestration中执行Pipeline
  3. R开发环境(Eclipse+StatET)
  4. Java 中的反射机制
  5. 使用Maven将Hadoop2.2.0源码编译成Eclipse项目
  6. Swift 网络请求数据与解析
  7. string can not be resolved
  8. LNMP1.4环境中安装fileinfo插件
  9. CentOS7快速配置nginx node mysql8.0
  10. 一个iOS6系统bug+一个iOS7系统bug
  11. ANDROID 中设计模式的采用--创建型模式
  12. 【web渗透技术】渗透攻防Web篇-SQL注入攻击初级
  13. webpack介绍 安装 常用命令
  14. mysql 中sql的执行顺序
  15. linux中top命令
  16. less开发指南(一)- 小牛试刀
  17. 【BZOJ】1823: [JSOI2010]满汉全席(2-sat)
  18. Spring Data MongoDB 三:基本文档查询(Query、BasicQuery
  19. Kettle-1-安装配置
  20. laravel中的事件处理

热门文章

  1. Make a DAC with a microcontroller&#39;s PWM timer
  2. oracle systemtap tracing
  3. 关于deselectRowAtIndexPath
  4. java中方法drawImage()的参数详细解释
  5. Struts2 无后缀action请求
  6. java程序的运行方式
  7. python3 urllib.request 网络请求操作
  8. oracle和mysql功能相同的函数
  9. 转】未指定 INSTANCESHAREDWOWDIR 命令行值。如果指定INSTANCESHAREDDIR 值,则必须指定该值 .
  10. springboot securyt 默认的用户名和密码