【树状数组】 ACdream 1127 Base Station

压根没有想到用树状数组可以做。。。。一开始用KD树做,结果超时了。。。KD树查询的时候太慢了。。而且对于这道题目,貌似可以人工构造出一组数据把KD树卡成一条链。。。。。跪掉了。。

用树状数组就离线查询呗。。。把查询和点都按一维从小到大排好序,然后按另一维单点插入就好嘞。。。


#include <iostream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <climits>
#define maxn 200005
#define eps 1e-6
#define mod 10007
#define INF 99999999
#define lowbit(x) (x&(-x))
#define lson o<<1, L, mid
#define rson o<<1 | 1, mid+1, R
typedef long long LL;
using namespace std;

LL xx1, yy1, xx2, yy2;
struct point
{
	LL x, y;
}po[maxn];
struct query
{
	LL r1, r2;
	int res;
	int number;
}q[maxn];
int tree[maxn];
LL yy[maxn];
int ycnt, n, m;

int cmp1(LL a, LL b)
{
	return a<b;
}
int cmp2(point a, point b)
{
	return a.x>b.x;
}
int cmp3(query a, query b)
{
	if(a.r1 == b.r1) return a.r2<b.r2;
	return a.r1>b.r1;
}
int cmp4(query a, query b)
{
	return a.number<b.number;
}
void init(void)
{
	memset(tree, 0, sizeof tree);
}
void add(int x)
{
	for(int i = x; i > 0; i-=lowbit(i)) tree[i]++;
}
int sum(int x)
{
	int ans = 0;
	for(int i = x; i <= ycnt; i+=lowbit(i)) ans += tree[i];
	return ans;
}
void read(void)
{
	int i;
	LL tmpx, tmpy;
	ycnt = 0;
	scanf("%d", &n);
	for(i = 1; i <= n; i++) {
		scanf("%lld%lld", &tmpx, &tmpy);
		po[i].x = (tmpx-xx1)*(tmpx-xx1) + (tmpy-yy1)*(tmpy-yy1);
		po[i].y = (tmpx-xx2)*(tmpx-xx2) + (tmpy-yy2)*(tmpy-yy2);
		yy[ycnt++] = po[i].y;
	}
	scanf("%d", &m);
	for(i = 1; i <= m; i++) {
		scanf("%lld%lld", &q[i].r1, &q[i].r2);
		q[i].number = i;
		q[i].r1*=q[i].r1;
		q[i].r2*=q[i].r2;
		yy[ycnt++] = q[i].r2;
	}
}
void work(void)
{
	int i, j;
	sort(yy+1, yy+ycnt+1, cmp1);
	for(i = 2, j = 2; i <= ycnt; i++)
		if(yy[i] != yy[i-1])
			yy[j++] = yy[i];
	ycnt = j-1;
	sort(po+1, po+n+1, cmp2);
	sort(q+1, q+m+1, cmp3);
}
int search(LL tmp)
{
	int bot = 1, top = ycnt, mid;
	while(top>=bot) {
		mid = (top+bot)>>1;
		if(yy[mid] == tmp) break;
		if(yy[mid] > tmp) top = mid-1;
		else bot = mid+1;
	}
	return mid;
}
void debug(void)
{
	int i;
	printf("BBBB\n");
	for(i = 1; i <= ycnt; i++)
		printf("%d\n", search(yy[i]));
}
void solve(void)
{
	int i, j;
	for(i = 1, j = 1; i <= m; i++) {
		while(j <= n && po[j].x >= q[i].r1) add(search(po[j++].y));
		q[i].res = sum(search(q[i].r2));
	}
	sort(q+1, q+m+1, cmp4);
	for(i = 1; i <= m; i++)
		printf("%d\n", q[i].res);
}
int main(void)
{
	while(scanf("%lld%lld%lld%lld", &xx1, &yy1, &xx2, &yy2)!=EOF) {
		init();
		read();
		work();
		solve();
	}
	return 0;
}


你可能感兴趣的:(树状数组,ACdream)