【树状数组】 HDOJ 5032 Always Cook Mushroom

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

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

也可以用法雷序列。。。。

#include <iostream>  
#include <queue>  
#include <stack>  
#include <map>  
#include <set>  
#include <bitset>  
#include <cstdio>  
#include <algorithm>  
#include <cstring>  
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 1000005
#define maxm 100005
#define eps 1e-10
#define mod 10000007
#define INF 1e9
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid  
#define rson o<<1 | 1, mid+1, R  
typedef long long LL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
void scanf(int &__x){__x=0;char __ch=getchar();while(__ch==' '||__ch=='\n')__ch=getchar();while(__ch>='0'&&__ch<='9')__x=__x*10+__ch-'0',__ch = getchar();}
LL gcd(LL _a, LL _b){if(!_b) return _a;else return gcd(_b, _a%_b);}
//head

struct point
{
	int x, y;
	LL v;
	double ang;
}p[maxn];
struct node
{
	int a, b, x, id;
	double ang;
}op[maxn];
LL res[maxn], tree[maxn];
int n, cnt;

int cmp1(point a, point b) {return a.ang < b.ang;}
int cmp2(node a, node b) {return a.ang < b.ang;}
int dcmp(double x) {return (x > eps) - (x < eps);}

void init(void)
{
	cnt = 0;
	for(int i = 1; i <= 1000; i++)
		for(int j = 1; j <= 1000; j++) {
			p[++cnt].x = i, p[cnt].y = j;
			p[cnt].ang = (double)j / i;
		}
	sort(p+1, p+cnt+1, cmp1);
}
void read(void)
{
	int a, b;
	scanf("%d%d", &a, &b);
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) {
		scanf("%d%d%d", &op[i].a, &op[i].b, &op[i].x);
		op[i].ang = (double)op[i].b / op[i].a, op[i].id = i;
	}
	for(int i = 1; i <= cnt; i++) p[i].v = (LL)(p[i].x + a) * (p[i].y + b);
	memset(tree, 0, sizeof tree);
}

void add(int x, int v)
{
	for(int i = x; i <= 1000; i += lowbit(i)) tree[i] += v;
}
LL sum(int x)
{
	LL ans = 0;
	for(int i = x; i > 0; i -= lowbit(i)) ans += tree[i];
	return ans;
}

void work(void)
{
	sort(op+1, op+n+1, cmp2);
	for(int i = 1, j = 1; i <= n; i++) {
		while(j <= cnt && dcmp(op[i].ang + 0.0000001 - p[j].ang) >= 0) add(p[j].x, p[j].v), j++;
		res[op[i].id] = sum(op[i].x);
	}
	for(int i = 1; i <= n; i++) printf("%I64d\n", res[i]);
}

int main(void)
{
	int _, __;
	init();
	while(scanf("%d", &_)!=EOF) {
		__ = 0;
		while(_--) {
			read();
			printf("Case #%d:\n", ++__);
			work();
		}
	}
	return 0;
}


你可能感兴趣的:(HDU)