hdu5032 Always Cook Mushroom

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

点的权值是(x+A)(y+B),其中A,B是给定的常数

做法也很显然,将查询离线下来按照方向向量排序,之后的操作就相当于用一根端点在原点的线从x轴开始往y轴扫,不断地把扫到的点的权值加入到树状数组中。每次扫到某个查询的方向向量时,用这个三角形的底边求前缀和,记录下来就好了。

权值会爆int,注意下就好了

#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
ll bit[1010];
void update(int pos,ll val)
{
	for(int i=pos;i<=1000;i+=i&-i)
		bit[i]+=val;
}
ll psum(int pos)
{
	ll ans=0;
	for(int i=pos;i>0;i-=i&-i)
		ans+=bit[i];
	return ans;
}
struct Point
{
	int x,y;
	bool operator <(Point one)const
	{
		return y*one.x<=one.y*x;
	}
}point[1000010];
struct Qu
{
	int x,y,len,no;
	bool operator <(Qu one)const
	{
		return ll(y*one.x)<ll(one.y*x);
	}
}qu[1000010];
ll ans[1000010];
int main()
{
	int n=0;
	for(int i=1;i<=1000;i++)
		for(int j=1;j<=1000;j++)
		{
			point[n].x=i;
			point[n++].y=j;
		}
	sort(point,point+n);
	int T;
	scanf("%d",&T);
	for(int cs=1;cs<=T;cs++)
	{
		int a,b,m;
		scanf("%d%d%d",&a,&b,&m);
		for(int i=0;i<m;i++)
		{
			scanf("%d%d%d",&qu[i].x,&qu[i].y,&qu[i].len);
			qu[i].no=i;
		}
		sort(qu,qu+m);
		memset(bit,0,sizeof(bit));
		for(int i=0,j=0;i<m;i++)
		{
			Point t;
			t.x=qu[i].x;
			t.y=qu[i].y;
			while(j<n&&point[j]<t)
			{
				update(point[j].x,ll(point[j].x+a)*(point[j].y+b));
				j++;
			}
			ans[qu[i].no]=psum(qu[i].len);
		}
		printf("Case #%d:\n",cs);
		for(int i=0;i<m;i++)
			cout<<ans[i]<<endl;
	}
}

Time
Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 518    Accepted Submission(s): 157

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

Recommend

hujie   |   We have carefully selected several similar problems for you:  5287 5286 5285 5284 5283

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-31 14:32:59

hdu5032 Always Cook Mushroom的相关文章

HDU5032 Always Cook Mushroom(树状数组&amp;&amp;离线)

树状数组+询问离线.一个优化是需要的,就是先对1000*1000个点先排序,而不是每次都生成这1000*1000个点然后和询问一起排序,那样会tle. #include <iostream> #include <cstring> #include <string> #include <vector> #include <cstdio> #include <algorithm> #include <cmath> using

HDU5032 -- Always Cook Mushroom 树状数组

题意:1000*1000的格子, 坐标为(1, 1) ~ (1000, 1000), 常数 A, B, 点(x,  y)权值为 (x + A) * (y + B), q次询问, 每次询问(0, 0) (p,  0), (p, q)的直角三角形内的权值和. 作法: 离线处理, 把所有点和询问放到一起, 按斜率从小到大排序, 考虑每个询问, 那么对当前询问有贡献的点的斜率肯定小于等于该直角三角形斜边的斜率, 所有遇到一个点加入到树状数组, 然后每次询问就是取 区间和了.

HDU Always Cook Mushroom (极角排序+树状数组)

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 numbe

[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-q

Hdu 5032 Always Cook Mushroom (树状数组)

题目大意: 在一个1000*1000的二维平面上,每一个整点都有一个权值,权值大小是 the production in the grid points (x, y) is (x + A)(y + B) where A, B are two constant. 思路分析: 先离线处理出所有的询问,对于每一个询问都有一个极角,按照极角排序. 然后对于平面上每一个点,都依次的加入到BIT中,当当前的这个询问的极角比这个点到原点的对应极角要大的时候,就将这个点加进去. 可以理解成有一根线,按照逆时针的

HDU 5032 Always Cook Mushroom

题意: 一块田地坐标从(1,1)到(1000,1000)  每块田地能种(x+A)*(y+B)的蘑菇  问  形似(0,0)(p,0)(p,q)这样的三角形区域能种的蘑菇的数量 思路: 其实很简单  枚举x  根据输入的向量  我们可以求出每个x对应最高的y  然后对于y可以用等差数列求和  再加上y个B  最后乘(x+A)就好了  但是这题时间卡得挺恶心的- 一开始写完T了  输入开挂还T  看了别人的代码发现y那个部分可以提出来打表做(就是代码中的f数组)  本以为稳A了  还是T-  仔细

hdu 5038 Grade

Grade Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 360    Accepted Submission(s): 203 Problem Description Ted is a employee of Always Cook Mushroom (ACM). His boss Matt gives him a pack of

HDU 5038 Grade(数学)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5038 Problem Description Ted is a employee of Always Cook Mushroom (ACM). His boss Matt gives him a pack of mushrooms and ask him to grade each mushroom according to its weight. Suppose the weight of a m

acm_icpc网络赛第五站:北京赛区

北京嘛,没什么好说的..实验室貌似没出线的..哎 就一道水题还被我英语给坑了.. 哈希一水: Grade Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 344    Accepted Submission(s): 195 Problem Description Ted is a employee of Always Cook Mu