POJ-3416 Crossing (树状数组 离线处理)

Crossing
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 1753 Accepted: 821

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
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
#define maxn 500005
int c1[maxn], c2[maxn];
int res[50001];
struct point{
	int x, y;
	bool operator < (const point& v){
		return y < v.y;
	}
}a[50001];
struct query{
	int x, y, id;
	bool operator < (const query& v){
		return y < v.y;
	}
}q[50001];
inline int lowbit(int x){
    return x & (-x);
}
void add(int c[], int x){
	while(x < maxn){
		c[x]++;
		x += lowbit(x);
	}
}
int getsum(int c[], int x){
	int ans = 0;
	while(x){
		ans += c[x];
		x -= lowbit(x);
	}
	return ans;
}
int main(){
    int T, n, m;
    scanf("%d", &T);
    while(T--){
    	memset(c1, 0, sizeof(c1));
    	memset(c2, 0, sizeof(c2));
    	scanf("%d %d", &n, &m);
	    for(int i = 1; i <= n; ++i){
	    	scanf("%d %d", &a[i].x, &a[i].y);
	    	a[i].x++; a[i].y++;
	    	add(c1, a[i].x);
	    }
	    for(int i = 1; i <= m; ++i){
	    	scanf("%d %d", &q[i].x, &q[i].y);
	    	q[i].x++; q[i].y++;
	    	q[i].id = i;
	    }
	    sort(a + 1, a + 1 + n);
	    sort(q + 1, q + 1 + m);
	    int pos = 1;
	    for(int i = 1; i <= m; ++i){
	    	while(a[pos].y < q[i].y && pos <= n){
	    		add(c2, a[pos].x);
	    		pos++;
	    	}
	    	res[q[i].id] = getsum(c2, q[i].x) + n - getsum(c1, q[i].x) - (getsum(c2, maxn - 1) - getsum(c2, q[i].x));
	    }
	    for(int i = 1; i <= m; ++i){
	    	printf("%d\n", abs(2 * res[i] - n));
	    }
	    if(T){
	    	printf("\n");
	    }
	}
}

/*
题意:5e4个点,5e5次询问,每次询问给出两条分别平行于X,Y的直线,问1,3象限和2,4象限点数的差。

思路:如果点数不多,应该可以二维数组搞一搞,然而5e4的范围只能一维搞了。那么我们离线处理
先把所有点和询问读进来,然后按y从小到大排序。开两个树状数组,c1把所有点的x加进去。然后由于询问
已按Y排序,对于每次询问,我们先将y小于该询问的点的x加到c2中,那么getsum(c2, q[i].x)就是该询问
第3象限的点数(假设原坐标系原点在左下角),getsum(c1, q[i].x)就是该询问第2,3象限的点数和,n - getsum(c1, q[i].x)
就是该询问第1,4象限的点数和,getsum(c2, maxn - 1)就是该询问第3,4象限的点数和。加加减减搞一搞就行了。
然后按顺序输出答案。
*/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POJ 2182是一道使用树状数组解决的题目,题目要求对给定的n个数进行排序,并且输出每个数在排序后的相对位置。树状数组是一种用来高效处理前缀和问题的数据结构。 根据引用中的描述,我们可以通过遍历数组a,对于每个元素a[i],可以使用二分查找找到a到a[i-1]中小于a[i]的数的个数。这个个数就是它在排序后的相对位置。 代码中的query函数用来求前缀和,add函数用来更新树状数组。在主函数中,我们从后往前遍历数组a,通过二分查找找到每个元素在排序后的相对位置,并将结果存入ans数组中。 最后,我们按顺序输出ans数组的元素即可得到排序后的相对位置。 参考代码如下: ```C++ #include <iostream> #include <cstdio> using namespace std; int n, a += y; } } int main() { scanf("%d", &n); f = 1; for (int i = 2; i <= n; i++) { scanf("%d", &a[i]); f[i = i & -i; } for (int i = n; i >= 1; i--) { int l = 1, r = n; while (l <= r) { int mid = (l + r) / 2; int k = query(mid - 1); if (a[i > k) { l = mid + 1; } else if (a[i < k) { r = mid - 1; } else { while (b[mid]) { mid++; } ans[i = mid; b[mid = true; add(mid, -1); break; } } } for (int i = 1; i <= n; i++) { printf("%d\n", ans[i]); } return 0; } ``` 这段代码使用了树状数组来完成题目要求的排序功能,其中query函数用来求前缀和,add函数用来更新树状数组。在主函数中,我们从后往前遍历数组a,通过二分查找找到每个元素在排序后的相对位置,并将结果存入ans数组中。最后,我们按顺序输出ans数组的元素即可得到排序后的相对位置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [poj2182Lost Cows——树状数组快速查找](https://blog.csdn.net/aodan5477/article/details/102045839)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [poj_2182 线段树/树状数组](https://blog.csdn.net/weixin_34138139/article/details/86389799)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值