欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

POJ-3416-Crossing-(树状数组)

程序员文章站 2022-03-26 16:52:35
...

Problem 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.

POJ-3416-Crossing-(树状数组)

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
<div><p>The first line contains an integer <i>T</i>, indicating the number of test cases. Then <i>T</i> blocks of test cases follow. For each case, the first line contains two integers <i>N</i> and <i>M</i>, where <i>N</i> is the number of coins on the ground and <i>M</i> indicates how many times they are going to draw the lines. The 2nd to (<i>N</i>+1)-th lines contain the co-ordinates of the coins and the last <i>M</i> lines consist of the <i>M</i> pairs integers (<i>x</i>, <i>y</i>) which means that the two splitting lines intersect at point (<i>x</i>, <i>y</i>).</p><p>(<i>N</i>,<i>M </i>≤ 50000, 0 ≤<i>x</i>,<i>y </i>≤ 500000)</p>
 

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

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

题意:给n个点,可以用一个有确定原点的坐标系将其划分为4个象限的点,1,3象限的点求个数和,2,4象限的点求个数和,最后求两者差的绝对值,对于每个点阵还可以有多种划分。

先将point(原点)和coin(硬币)按先x后y升序排列,这里要用两个树状数组l,r分别维护原点左右两边点,即原点左边的点都放在l,右边的点都放在r,这里将所有点先都存放入r,循环point时逐渐从r取出放入l,因为是排好序的那么处理完一个原点时,原点左边的点都在l,右边的点都在r,那么:

第一象限: sum(r,maxy)-sum(r,point[i].y)

第三象限: sum(l,point[i].y);

第二象限: sum(l,maxy)-sum(l,point[i].y)

第四象限: sum(r,point[i].y);


#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
#define M1 500005
#define M2 50005
int l[M1],r[M1];
int maxy;
struct node {
    int x,y,id;
    bool operator < (const node &obj) const
    {
        return x<obj.x||(x==obj.x&&y<obj.y);
    }
}point[M2],coin[M2];
inline int lowbit(int i)
{
    return i&(-i);
}
void add(int *t,int pos,int v)
{
    while(pos<=maxy)
    {
        t[pos]+=v;
        pos+=lowbit(pos);
    }
}
int sum(int *t,int i)
{
    int res=0;
    while(i>0)
    {
        res+=t[i];
        i-=lowbit(i);
    }
    return res;
}
int main()
{
    int T,n,m,i;
    int ans[M2];
    scanf("%d",&T);
    while(T--)
    {
        memset(l,0,sizeof(l));
        memset(r,0,sizeof(r));
        scanf("%d%d",&n,&m);
        maxy=-1;
        for(i=1;i<=n;i++)
        {
            scanf("%d%d",&coin[i].x,&coin[i].y);
            coin[i].x++; coin[i].y++;//树状数组下标从1开始
            if(maxy<coin[i].y)
                maxy=coin[i].y;
        }
        sort(coin+1,coin+1+n);
        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(maxy<point[i].y)
                maxy=point[i].y;
        }
        sort(point+1,point+1+m);
        for(i=1;i<=n;i++)
            add(r,coin[i].y,1);
        int it=1;
        for(i=1;i<=m;i++)
        {
            while(point[i].x>=coin[it].x&&it<=n)
            {
                add(l,coin[it].y,1);
                add(r,coin[it].y,-1);
                it++;
            }
            int one_three=sum(r,maxy)-sum(r,point[i].y)+sum(l,point[i].y);
            int two_four=sum(l,maxy)-sum(l,point[i].y)+sum(r,point[i].y);
            ans[point[i].id]=abs(one_three-two_four);
        }
        for(i=1;i<=m;i++)
            printf("%d\n",ans[i]);
        if(T) printf("\n");
    }
    return 0;
}