[POJ3416]Crossing

Problem

给你n个点,m个询问,每个询问有x, y
问以(x,y)为原点建立的平面直角坐标系分割的第一象限和第三象限的点数和减去第二象限和第四象限的点数和

Solution

用2个树状数组维护一条竖直的线的左右两边的点的分布情况
然后对x轴排序,把一个点从右边的树状数组中拿出,放到左边去(相当那条竖直的线在向右扫描)
然后查询处答案即可。

Notice

树状数组用0的坑

Code

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define reg register int
#define lowbit(x) (x & -x)
#define rep(i, a, b) for (reg i = a; i <= b; i++)
#define per(i, a, b) for (reg i = a; i >= b; i--)
#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
const int INF = 1e9, N = 500001;
const double eps = 1e-6, phi = acos(-1.0);
ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
struct node
{
	int x, y, flag, id;
}T[N + 5];
int ans[N + 5];

struct Node
{
	int val[3][N + 5];
	inline void build(int l, int r)
	{
		rep(i, l, r)
			val[1][i] = val[2][i] = 0;
	}
	
	inline void modify(int x, int y, int v)
	{
		while (y <= N)
		{
			val[x][y] += v;
			y += lowbit(y);
		}
	}
	
	int query(int x, int y)
	{
		int s = 0;
		while (y)
		{
			s += val[x][y];
			y -= lowbit(y);
		}
		return s;
	}
}BIT;

int cmp(node X, node Y)
{
	return X.x < Y.x;
}

int sqz() 
{
	int H_H = read();
	while(H_H--)
	{
		BIT.build(1, N);
		int n = read();
		int m = read();
		rep(i, 1, n) T[i].x = read(), T[i].y = read() + 1, T[i].flag = 0, T[i].id = i;
		rep(i, 1, m) T[i + n].x = read(), T[i + n].y = read() + 1, T[i + n].flag = 1, T[i + n].id = i;
		sort(T + 1, T + n + m + 1, cmp);
		rep(i, 1, n + m)
			if (!T[i].flag) BIT.modify(2, T[i].y, 1);
		rep(i, 1, n + m)
		{
			if (!T[i].flag)
			{
				BIT.modify(1, T[i].y, 1);
				BIT.modify(2, T[i].y, -1);
			}
			else
			{
				int sum = 2 * BIT.query(1, T[i].y) + BIT.query(2, N) - BIT.query(2, T[i].y) * 2 - BIT.query(1, N);
				ans[T[i].id] = abs(sum);
			}
		}
		rep(i, 1, m) printf("%d
", ans[i]);
		printf("
");
	}
}
原文地址:https://www.cnblogs.com/WizardCowboy/p/7608525.html