首页 > 代码库 > [hdu 5032]2014北京网络赛Always Cook Mushroom 离散化+离线线段树/树状数组

[hdu 5032]2014北京网络赛Always Cook Mushroom 离散化+离线线段树/树状数组

Always Cook Mushroom

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 196    Accepted Submission(s): 54


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
2014 ACM/ICPC Asia Regional Beijing Online
 


题目大意

给定一片蘑菇田,只有从(1,1)至(1000,1000)的整数点能产蘑菇,点(x,y)的蘑菇产量为(x+A)(y+B)

给定直角三角形的两个顶点和斜边的斜率,用(a,b)的形式给出,求该三角形内的蘑菇产量和。


解题思路

因为点最多有1000*1000=100W个,可以先求出他们斜率并排序,找到每个斜率的rank,并将询问按照x排序,将斜率离散化,从上到下,从左到右将每个点加入线段树。询问实际上就是求一个前缀和了。一边处理询问,一边处理这些点。

每次区间更新该斜率至最大的斜率(相当于添加一条权值为(x+A)(y+B)的线段),单点查询该点值。


线段树的习惯不是很好……

应该树状数组也可以过……


#include <cstdio>
#include <algorithm>
#include <cstring>
#define LL long long
using namespace std;
inline int ReadInt()
{
    int flag=0;
    char ch=getchar();
    int data=http://www.mamicode.com/0;>

[hdu 5032]2014北京网络赛Always Cook Mushroom 离散化+离线线段树/树状数组