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

来源:互联网 发布:管家婆会计软件 编辑:程序博客网 时间:2024/05/22 01:37
Crossing
Time Limit: 2000MS Memory Limit: 65536KTotal 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

210 329 2217 1418 233 156 2830 274 126 78 011 212 255 1019 2410 528 182 296 513 1220 2715 2611 923 2510 022 2416 3014 317 218 17 4

Sample Output

64422444
#include <stdio.h>#include <algorithm>#include <string.h>using namespace std;#define maxn 500005int 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象限的点数和。加加减减搞一搞就行了。然后按顺序输出答案。*/

原创粉丝点击