POJ 3416 Crossing

Crossing

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 1564   Accepted: 726

Description

Wintokk has collected a huge amount of coins at THU. One day he had all his coins fallen on to the ground. Unfortunately, WangDong came by and decided to rob Wintokk of the coins.

They agreed to distribute the coins according to the following rules:

Consider the ground as a plane. Wintokk draws a horizontal line on the plane and then WangDong draws a vertical one so that the plane is divided into 4 parts, as shown below.

Wintokk will save the coins in I and III while those fit in II and IV will be taken away by the robber WangDong.

For fixed locations of the coins owned by Wintokk, they drew several pairs of lines. For each pair, Wintokk wants to know the difference between the number of the saved coins and that of the lost coins.

It‘s guaranteed that all the coins will lie on neither of the lines drew by that two guys.

Input

The first line contains an integer T, indicating the number of test cases. Then T blocks of test cases follow. For each case, the first line contains two integers N and M, where N is the number of coins on the
ground and M indicates how many times they are going to draw the lines. The 2nd to (N+1)-th lines contain the co-ordinates of the coins and the last M lines consist of the M pairs integers (xy) which
means that the two splitting lines intersect at point (xy).

(N,≤ 50000, 0 ≤x,≤ 500000)

Output

For each query, output a non-negative integer, the difference described above. Output a blank line between cases.

Sample Input

2
10 3
29 22
17 14
18 23
3 15
6 28
30 27
4 1
26 7
8 0
11 21
2 25
5 10
19 24
10 5
28 18
2 29
6 5
13 12
20 27
15 26
11 9
23 25
10 0
22 24
16 30
14 3
17 21
8 1
7 4

Sample Output

6
4
4

2
2
4
4
4

Source

POJ Monthly--2007.10.06, Wang Dong

解题思路:用两个树状数组维护原点左右两边硬币的个数,先把硬币及原点排序,把所有点插入到右边的树状数组中,取所有在原点左边的点加入到左边树状数组中,同时从右边的树状数组中删除,分别统计下一三象限和二四象限的值相减求绝对值即可

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int l[500005],r[500005];
struct P
{
	int x;
	int y;
	int id;
}point[50005],coin[50005];
int cmp(P a,P b)
{
	if(a.x!=b.x)
		return a.x<b.x;
	else
		return a.y<b.y;
}
int lowbit(int x)
{return x&(-x);}
void add(int *s,int pos,int n,int val)
{
	int i;
	for(i=pos;i<=n;i+=lowbit(i))
		s[i]+=val;
}
int sum(int *s,int pos)
{
	int i,sum=0;
	for(i=pos;i>0;i-=lowbit(i))
		sum+=s[i];
	return sum;
}
int main()
{
	int i,t,n,m,ans[50005],y;
	scanf("%d",&t);
	while(t--)
	{
		y=-1;
		memset(l,0,sizeof(l));
		memset(r,0,sizeof(r));
		memset(ans,0,sizeof(ans));
		scanf("%d%d",&n,&m);
		for(i=1;i<=n;i++)
		{
			scanf("%d%d",&coin[i].x,&coin[i].y);
			coin[i].x++;coin[i].y++;
			if(y<coin[i].y)
				y=coin[i].y;
		}
		sort(coin+1,coin+1+n,cmp);
		for(i=1;i<=m;i++)
		{
			scanf("%d%d",&point[i].x,&point[i].y);
			point[i].x++;point[i].y++;
			point[i].id=i;
			if(y<point[i].y)
				y=point[i].y;
		}
		sort(point+1,point+1+m,cmp);
		for(i=1;i<=n;i++)
			add(r,coin[i].y,y,1);
		int op=1;
		for(i=1;i<=m;i++)
		{
			while(point[i].x>=coin[op].x&&op<=n)
			{
				add(l,coin[op].y,y,1);
				add(r,coin[op].y,y,-1);
				op++;
			}
			int a=sum(r,y)-sum(r,point[i].y)+sum(l,point[i].y);
			int b=sum(l,y)-sum(l,point[i].y)+sum(r,point[i].y);
			ans[point[i].id]=abs(a-b);
		}
		for(i=1;i<=m;i++)
			printf("%d\n",ans[i]);
		printf("\n");
	}
	return 0;
}
时间: 2024-10-07 10:44:21

POJ 3416 Crossing的相关文章

POJ 3416 Crossing --离线+树状数组

题意: 给一些平面上的点,然后给一些查询(x,y),即以(x,y)为原点建立坐标系,一个人拿走第I,III象限的点,另一个人拿II,IV象限的,点不会在任何一个查询的坐标轴上,问每次两人的点数差为多少. 解法:离线树状数组.点不在坐标轴上,即点不共线使这题简单了不少,可以离散化点,也可以不离散化,因为x,y <= 500000,直接就可以搞.我这里是离散的,其实也没比直接搞快. 见两个树状数组,一个先把所有点都modify进去,一个等待以后加元素. 然后将查询和给出的点都按y坐标排序,然后离线对

BFS POJ 2150 Crossing Prisms

题目传送门 1 /* 2 BFS:这题我的做法是先找出所有草地,然后枚举所有两兄弟烧的地点进行BFS,然后去最小值 3 */ 4 /************************************************ 5 Author :Running_Time 6 Created Time :2015-8-3 19:23:42 7 File Name :FZU_2150.cpp 8 *************************************************/

POJ 1700 Crossing River(贪心)

V - Crossing River Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1700 Description A group of N people wishes to go across a river with only one boat, which can at most carry two persons. There

[ACM] poj 1700 Crossing River (经典过河问题)

Crossing River Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10212   Accepted: 3855 Description A group of N people wishes to go across a river with only one boat, which can at most carry two persons. Therefore some sort of shuttle arr

poj 1700 Crossing River C++/Java

http://poj.org/problem?id=1700 题目大意: 有n个人要过坐船过河,每一个人划船有个时间a[i],每次最多两个人坐一条船过河.且过河时间为两个人中速度慢的,求n个人过河的最短时间. 思路: 贪心. 对于每次过河的,有两种情况: //最快和最慢过去,然后最快回来.在和次慢过去.最快回来 int action1=a[i-1] + a[0] + a[i-2] +a[0]; //最快和次慢过去,然后最快回来,在次慢和最慢过去,次慢回来 int action2=a[1] +a[

poj 1700 Crossing River

Crossing River Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12585   Accepted: 4787 Description A group of N people wishes to go across a river with only one boat, which can at most carry two persons. Therefore some sort of shuttle arr

poj 1700 Crossing River(贪心)

分析:题意 源岸数量<=3  很好判断 3:num[0]+num[1]+num[2] 2:num[1] ,1:num[0] 源岸数量>3 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int main(void) { int n,t ; int i,j; int num[1001]; cin>>

ACM学习历程——POJ 1700 Crossing River(贪心)

Description A group of N people wishes to go across a river with only one boat, which can at most carry two persons. Therefore some sort of shuttle arrangement must be arranged in order to row the boat back and forth so that all people may cross. Eac

Crossing River POJ过河问题

A group of N people wishes to go across a river with only one boat, which can at most carry two persons. Therefore some sort of shuttle arrangement must be arranged in order to row the boat back and forth so that all people may cross. Each person has