Crossing 树状数组

poj 3416

Crossing

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 1883 Accepted: 858

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

看了网上题解才做出了的QAQ

题意:平面上有n个硬币,有m个对横竖坐标轴,保证硬币不在坐标轴上,输出每对坐标轴1,3象限硬币个数 和2,4象限硬币个数的差值(取绝对值)

题解:因为增加坐标轴不影响硬币的坐标,所以该题可以用树状数组离线处理,用两个数组维护(lower和upper)分别维护在y坐标轴下面的硬币个数和在y坐标轴上面的硬币,那怎么维护呢?首先对point(坐标原点的位置)和coin(硬币的位置)按照y值的大小排序,开始从下到上(y的大小)遍历point的位置(所以要初始所有coin在upper数组内),当存在coin的y小于point的y,移除upper中的该coin,在lower中增加该coin,当不存在时,便询问而记录答案,然后point转移到下一个更高的位置。最后按照id顺序输出答案。反复理解一下,如果还有不懂,下面代码有清楚的注释 。

下面是 G++ AC代码

//#include<bits/stdc++.h>
//#include <unordered_map>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<set>
#include<climits>
#include<queue>
#include<cmath>
#include<stack>
#include<map>
using namespace std;
#define LL long long
#define MT(a,b) memset(a,b,sizeof(a))
const int INF  =  0x3f3f3f3f;
const int ONF  = -0x3f3f3f3f;
const int mod  =  998244353;
const int maxn =  5e4+5;
const int N    =  5e5+5;
const double PI  =  3.141592653589;
const double E   =  2.718281828459;

struct dd
{
    int x, y, id, ans;               //坐标(x,y),point的id和对应的答案ans
    bool friend operator < (dd a, dd b){
        return a.y!=b.y ? a.y<b.y : a.x<b.x;
    }
}coin[maxn],point[maxn];

int n,m;
int lower[maxn<<4], upper[maxn<<4];//树状数组lower维护y坐标轴下面的coin个数,upper维护y坐标轴上面的coin个数

int lowbit(int x){
    return x&(-x);
}

void add(int c[] , int pos , int v ){
    while(pos<=N) c[pos] += v , pos += lowbit(pos);
}

int query(int c[] , int pos ){
    int ans = 0;
    while(pos>0) ans += c[pos] , pos -= lowbit(pos);
    return ans ;
}

bool com(dd a, dd b){
    return a.id<b.id;
}

int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
        MT(coin,0); MT(point,0); MT(lower,0); MT(upper,0);
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
        {
            int x, y ;scanf("%d%d",&x,&y);
            coin[i] = {++x,++y};
        }
        for(int i=0;i<m;i++)
        {
            int x, y ;scanf("%d%d",&x,&y);
            point[i] = {++x,++y,i,0};
        }
        sort(coin, coin+n);
        sort(point, point+m);                              //point和coin按照y的大小排序
        for(int i=0 ;i<n; i++) add(upper,coin[i].x,1);     //因为我们要从下到上遍历,所有首先把所有coin全部压入upper数组
        int l = 0 , k = 0;                                 //l表示point的下标, k表示coin的下标
        while(l<m)                                         //每个point对应一个答案,所以遍历point以便得到对应的ans
        {
            while(k<n&&coin[k].y<point[l].y)               //当存在coin的y小于point的y,移除upper中的coin[k],在lower中增加coin[k]
            {
                add(lower,coin[k].x,1);
                add(upper,coin[k].x,-1);
                k++;
            }
            int a = query(upper,point[l].x) + query(lower,N-1) - query(lower,point[l].x-1); //a为1,3象限coin的个数
            int b = query(lower,point[l].x) + query(upper,N-1) - query(upper,point[l].x-1); //b为2,4象限coin的个数
            point[l++].ans = abs(a-b);                                                      //记录答案
        }
        sort(point,point+m,com);                    // 因为是离线处理,所以要按照原来的id顺序输出,排序
        for(int i=0;i<m;i++)  printf("%d\n",point[i].ans);
        if(T) printf("\n");
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值