POJ 3416——Crossing(两个树状数组,poj2352)

Crossing
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 1562 Accepted: 724

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

——————————————————分割线——————————————

题目大意:

有n个硬币,每个硬币的坐标(x,y),有m次的操作,每次给一个点(i,j),做水平线x=i,垂直线y=j,这两条直线将区域分为4块,求1,3块的硬币数跟2,4块硬币数差的绝对值


思路:

对n个硬币坐标按x升序排列,m次操作也按x升序排列。用两个树状数组分别记录在x轴左边的点的个数,在x轴右边点的个数。初始时都在右边,然后进行更新时,把比它小的点插入到坐标,把右边的删除。

统计的时候按平常那样子就可以了。

对了,下标从0开始,点的坐标要+1


这题数组越界搞得我都醉了。问了大神才知道怎么错的,ORZ。

这题让我学了好多,虽然是个水题!


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
const int M=500001;
using namespace std;
int n,m;
int l[M+10],r[M+10],ans[M];
struct node
{
    int x,y,index;
    bool operator<(const node&A)const
    {
        return x<A.x;
    }
}a[M],b[M];
void update(int *s,int x,int v)
{
    for(int i=x;i<=M;i+=i&-i){
        s[i]+=v;
    }
}
int getsum(int *s,int x)
{
    int sum=0;
    for(int i=x;i>0;i-=i&-i){
        sum+=s[i];
    }
    return sum;
}
int main()
{
    int T;
    cin>>T;
    while(T--){
        scanf("%d %d",&n,&m);
        for(int i=0;i<n;++i){
            scanf("%d %d",&a[i].x,&a[i].y);
            a[i].x++,a[i].y++;
        }
        for(int i=0;i<m;++i){
            scanf("%d %d",&b[i].x,&b[i].y);
            b[i].x++,b[i].y++;
            b[i].index=i;
        }
        sort(a,a+n);sort(b,b+m);
        memset(l,0,sizeof(l));memset(r,0,sizeof(r));
        for(int i=0;i<n;++i){
            update(r,a[i].y,1);
        }
        int st=0,ed;
        for(int i=0;i<m;++i){
            for(ed=st;ed<n;++ed){
                if(a[ed].x>b[i].x) break;
            }
            for(int j=st;j<ed;++j){
                update(r,a[j].y,-1);
                update(l,a[j].y,1);
            }
            int s13=getsum(l,b[i].y)+getsum(r,M)-getsum(r,b[i].y);
            int s24=getsum(r,b[i].y)+getsum(l,M)-getsum(l,b[i].y);
            ans[b[i].index]=abs(s13-s24);
            st=ed;
        }
        for(int i=0;i<m;++i){
            printf("%d\n",ans[i]);
        }
        if(T) printf("\n");
    }
    return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值