HDU Always Cook Mushroom (极角排序+树状数组)
Problem Description
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?
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).
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.
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
Case #1:
1842
1708
86
Case #2:
2901
2688
200
题意:给定一个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 (极角排序+树状数组)的更多相关文章
- hdu 6203 ping ping ping(LCA+树状数组)
hdu 6203 ping ping ping(LCA+树状数组) 题意:给一棵树,有m条路径,问至少删除多少个点使得这些路径都不连通 \(1 <= n <= 1e4\) \(1 < ...
- hdu 5869 区间不同GCD个数(树状数组)
Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K ( ...
- 【 HDU - 4456 】Crowd (二维树状数组、cdq分治)
BUPT2017 wintertraining(15) #5A HDU 4456 题意 给你一个n行n列的格子,一开始每个格子值都是0.有M个操作,p=1为第一种操作,给格子(x,y)增加z.p=2为 ...
- hdu 4911 求逆序对数+树状数组
http://acm.hdu.edu.cn/showproblem.php?pid=4911 给定一个序列,有k次机会交换相邻两个位置的数,问说最后序列的逆序对数最少为多少. 实际上每交换一次能且只能 ...
- HDU 6318 - Swaps and Inversions - [离散化+树状数组求逆序数][杭电2018多校赛2]
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=6318 Problem Description Long long ago, there was an ...
- HDU 5869 Different GCD Subarray Query 树状数组 + 一些数学背景
http://acm.hdu.edu.cn/showproblem.php?pid=5869 题意:给定一个数组,然后给出若干个询问,询问[L, R]中,有多少个子数组的gcd是不同的. 就是[L, ...
- hdu 1394 Minimum Inversion Number(树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给你一个0 — n-1的排列,对于这个排列你可以将第一个元素放到最后一个,问你可能得到的最 ...
- HDU 4630 No Pain No Game 树状数组+离线操作
题意:给一串数字,每次查询[L,R]中两个数的gcd的最大值. 解法:容易知道,要使取两个数让gcd最大,这两个数最好是倍数关系,所以处理出每个数的所有倍数,两两间根据倍数关系形成一条线段,值为该数. ...
- HDU 4746 莫比乌斯反演+离线查询+树状数组
题目大意: 一个数字组成一堆素因子的乘积,如果一个数字的素因子个数(同样的素因子也要多次计数)小于等于P,那么就称这个数是P的幸运数 多次询问1<=x<=n,1<=y<=m,P ...
随机推荐
- 老生常谈ajax
一,js中的ajax ajax(Asynchronous Javascript And XML)即为异步的JavaScript和XML,顾名思义,这个技术是和我们当前页面刷新无关的,因为它是异步的,在 ...
- java基础学习总结——方法的重载(overload)
一.方法的重载 方法名一样,但参数不一样,这就是重载(overload). 所谓的参数不一样,主要有两点:第一是参数的个数不一样,第二是参数的类型不一样.只要这两方面有其中的一方面不一样就可以构成方法 ...
- Android Binder分析二:Natvie Service的注冊
这一章我们通过MediaPlayerService的注冊来说明怎样在Native层通过binder向ServiceManager注冊一个service,以及client怎样通过binder向Servi ...
- 去除ArcMap连接空间数据库中多余的属性表
这个操作目前可能不具有可行性,但是为了完整性还是在下面讲一下吧.如有兴趣的小伙伴,可以按照下面的操作方式去尝试. 一.需求 去除ArcMap连接空间数据库中多余的属性表. PL/SQL中查询得到的内容 ...
- 【docker-compose】使用docker-compose部署运行spring boot+mysql 【处理容器的时区问题】【详解】【福利:使用docker-compose构建 wordpress+mysql】
==================================================================================================== ...
- Entityframework:“System.Data.Entity.Internal.AppConfig”的类型初始值设定项引发异常。
<configSections> <!-- For more information on Entity Framework configuration, visit http:// ...
- 如何利用启明星Portal门户系统的Page模块构建文档库
利用启明星门户系统的Page模块构架可以搭建企业内部的文档管理系统. (一)应用背景 企业内部通常都会使用共享网盘的方式来存放不同部门之间的文档,例如管理员在服务器上对人事部门增加人事部文档文件夹. ...
- Objective-C:在类中设置不同协议
在下面的代码中,设置了两种不同的协议规则:一种是老师对学生设置的协议:即老师发出命令后,学生站起来.回答问题.坐下; 另一种是我对学生设置的协议:即学生按照我的协议中的初始化函数去初始化一个整数. / ...
- OpenCV学习(27) 直方图(4)
我们可以利用OpenCV的直方图,backproject直方图和meanshift算法来跟踪物体.下面通过简单的例子来说明如何实现跟踪算法,我们有两幅狒狒的图片,如下图所示:我们首先在左图中框选狒狒的 ...
- PHP语言基础之MySql 05 By ACReaper
PHP的基本语法学完后,我们马上学下PHP如何和MySql进行交互.PHP和MySql进行交互的API可以分为两类,一类是面向过程的,一类是面向对象的,面向对象的我们等复习完面向对象再介绍,现在先介绍 ...