Crossing(一维单点更新,区间查询)

Problem R

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 8   Accepted Submission(s) : 4
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.

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
 

Statistic |  Submit |  Back

思路:(借鉴别人的思路)

先将点和原点以纵坐标为关键字排序,此题目需要用到两个数状数组,分别来维护左上方和右上方的点,一开始时把所有点放在右上数状数组(这也是原点要排序的原因,原点要先处理位置在左上的,再处理右下的,随着处理原点的不同逐渐把右上的删除放到左边的树状数组(因为原点越来越向右)。

代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#define maxn 500010
using namespace std;
int l[maxn],r[maxn];
struct node
{
	int x;
	int y;
	int id;
}point[50005],coin[50005];

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

int cmp(node a,node b)
{
	if(a.x!=b.x)
		return a.x<b.x;
	else
		return a.y<b.y;
}

void add(int *s,int pos,int n,int val){
	while(pos<=n){
		s[pos]+=val;
		pos+=lowbit(pos);
	}
}

int sum(int *s,int pos)
{
	int sum=0;
	while(pos>0){
		sum+=s[pos];
		pos-=lowbit(pos);
	}
	return sum;
}

int main()
{
	ios::sync_with_stdio(false);
	int i,t,n,m,ans[50005],y;
	cin>>t;
	while(t--)
	{
		memset(l,0,sizeof(l));
		memset(r,0,sizeof(r));
		memset(ans,0,sizeof(ans));
		y=-0x3f;
		cin>>n>>m;
		for(i=1;i<=n;i++)
		{
			cin>>coin[i].x>>coin[i].y;
			coin[i].x++;
			coin[i].y++;
			if(y<coin[i].y)
				y=coin[i].y;
		}
		sort(coin+1,coin+1+n,cmp);
		for(i=1;i<=m;i++)
		{
			cin>>point[i].x>>point[i].y;
			point[i].x++;
			point[i].y++;
			point[i].id=i;
			if(y<point[i].y)
				y=point[i].y;
		}
		sort(point+1,point+1+m,cmp);
		for(i=1;i<=n;i++)
		add(r,coin[i].y,y,1);
		int ss=1;
		for(i=1;i<=m;i++)
		{
			while(point[i].x>=coin[ss].x&&ss<=n)
			{
				add(l,coin[ss].y,y,1);
				add(r,coin[ss].y,y,-1);
				ss++;
			}
			int a=sum(r,y)-sum(r,point[i].y)+sum(l,point[i].y);
			int b=sum(l,y)-sum(l,point[i].y)+sum(r,point[i].y);
			ans[point[i].id]=abs(a-b);
		}
		for(i=1;i<=m;i++)
		cout<<ans[i]<<endl;
		cout<<endl;
	}
	return 0;
}
读仔细题目,看准要求!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值