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
  1. 2
  2. 0 0
  3. 3
  4. 3 5 8
  5. 2 4 7
  6. 1 2 3
  7. 1 2
  8. 3
  9. 3 5 8
  10. 2 4 7
  11. 1 2 3
 
Sample Output
  1. Case #1:
  2. 1842
  3. 1708
  4. 86
  5. Case #2:
  6. 2901
  7. 2688
  8. 200
 
Source

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

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

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. typedef long long ll;
  7. using namespace std;
  8. const int maxn = 1005;
  9. const int inf = 1e5+5;
  10.  
  11. struct Point {
  12. ll a, b;
  13. double s;
  14. } p[maxn*maxn];
  15. struct Query {
  16. ll a, b, x, id;
  17. double s;
  18. } q[maxn*maxn];
  19. ll bit[maxn];
  20. ll ans[inf], Index[inf];
  21. int cnt;
  22.  
  23. void scan(ll &x) {
  24. char c;
  25. while ((c = getchar()) && (c < '0' || c > '9')) ;
  26. x = c - '0';
  27. while ((c = getchar()) && (c >= '0' && c <= '9'))
  28. x = x * 10 + c - '0';
  29. }
  30.  
  31. void out(ll x) {
  32. if (x > 9)
  33. out(x/10);
  34. putchar(x%10+'0');
  35. }
  36.  
  37. inline int lowbit(int x) {
  38. return x & -x;
  39. }
  40.  
  41. inline void add(int x, int val) {
  42. while (x <= 1000) {
  43. bit[x] += val;
  44. x += lowbit(x);
  45. }
  46. }
  47.  
  48. inline ll sum(int x) {
  49. ll tmp = 0;
  50. while (x > 0) {
  51. tmp += bit[x];
  52. x -= lowbit(x);
  53. }
  54. return tmp;
  55. }
  56.  
  57. bool cmp1(Point x, Point y) {
  58. return x.s < y.s;
  59. }
  60.  
  61. bool cmp2(Query x, Query y) {
  62. if (x.s == y.s)
  63. return x.x < y.x;
  64. return x.s < y.s;
  65. }
  66.  
  67. void init() {
  68. for (int i = 1; i <= 1000; i++)
  69. for (int j = 1; j <= 1000; j++) {
  70. p[cnt].a = i;
  71. p[cnt].b = j;
  72. p[cnt++].s = 1.0 * j / i;
  73. }
  74. sort(p, p+cnt, cmp1);
  75. }
  76.  
  77. int main() {
  78. cnt = 0;
  79. ll A, B, m;
  80. init();
  81. int t, cas = 1;
  82. scanf("%d", &t);
  83. while (t--) {
  84. memset(bit, 0, sizeof(bit));
  85. scan(A), scan(B), scan(m);
  86. for (int i = 0; i < m; i++) {
  87. scan(q[i].a), scan(q[i].b), scan(q[i].x);
  88. q[i].s = 1.0 * q[i].b / q[i].a;
  89. q[i].id = i;
  90. }
  91.  
  92. sort(q, q + m, cmp2);
  93. for (int i = 0; i < m; i++) {
  94. Index[q[i].id] = i;
  95. }
  96. cnt = 0;
  97. printf("Case #%d:\n", cas++);
  98. for (int i = 0; i < m; i++) {
  99. while (p[cnt].s <= q[i].s) {
  100. add(p[cnt].a, (p[cnt].a+A) * (p[cnt].b + B));
  101. cnt++;
  102. }
  103. ans[i] = sum(q[i].x);
  104. }
  105. for (int i = 0; i < m; i++) {
  106. out(ans[Index[i]]);
  107. printf("\n");
  108. }
  109. }
  110. return 0;
  111. }

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. spring mvc 如何传递集合参数(list,数组)

    spring mvc 可以自动的帮你封装参数成为对象,不用自己手动的通过request一个一个的获取参数,但是这样自动的参数封装碰碰到了集合参数可能就需要点小技巧才可以了. 一.基础类型和引用类型有什 ...

  2. AutoCAD二次开发——AutoCAD.NET API开发环境搭建

    AutoCAD二次开发工具:1986年AutoLisp,1989年ADS,1990年DCL,1993年ADS-RX,1995年ObjectARX,1996年Active X Automation(CO ...

  3. 浅用block 转

    block是一门有用的大后期学问.现在我只是列出一点基本用法. 1.快速枚举(Enumeration) 通常是和NSArray, NSDictionary, NSSet, NSIndexSet放在一起 ...

  4. 转 如何在IOS设备中去掉屏幕上的status bar

    引入如何在IOS设备中去掉屏幕上的status bar,即:不显示设备上方的[网络.时间.电池??]条?操作方法一:在-info.list项目文件中,加上“Status bar is initiall ...

  5. copy and paste ,做到这样也很牛逼了

    db笔记本 mysql资源 mysql5.1中文参考手册 mysql管理 基于linux使用mysql二进制包安装mysql5.5 mysql client命令行选项 mysqld服务器系统变量和状态 ...

  6. DruidDataSource配置

    DruidDataSource大部分属性都是参考DBCP的,如果你原来就是使用DBCP,迁移是十分方便的. 参考配置 <bean id="dataSource" class= ...

  7. 表单提交的3种方式,http post的contentType

    application/x-www-form-urlencoded:窗体数据被编码为名称/值对.这是标准的编码格式.这是默认的方式 multipart/form-data:窗体数据被编码为一条消息,页 ...

  8. TR069协议小结

        也称为CWMP,是在Internet网上通过wan口控制通信终端设备的协议.其协议流程如下图所示: 具体网上有很多资料.其主要的两个内容是:HTTP Client模型.DATA模型.      ...

  9. Android之Android软键盘的隐藏显示研究

    转自:http://blog.csdn.net/lilu_leo/article/details/6587578 看了很多这类型的文章,这篇文章最有价值,解决了我的烦恼,必须转. Android是一个 ...

  10. UI_UITabBarController

    建立控制器 // 普通控制器 GroupViewController *groupVC = [[GroupViewController alloc] init]; SecondViewControll ...