Under Attack II

Problem Description

Because of the sucessfully calculation in Under Attack I, Doctor is awarded with Courage Cross and promoted to lieutenant. But the war seems to end in never, now Doctor has a new order to help anti-aircraft troops calculate the proper number of supply sites needed for SAMs in battle regions.

According to intel, enemy bombers go straight across battle region and the bombing runs are continous. So their routes divides the region into several parts. The missles SAM needed are provided by supply sites. Because it's dangerous to cross fireline, Ufo suggests that every part of battle regions divided by firelines should have a supply site so that the SAMs can safely get enough ammo.

Now that the task is clear, Doctor is asked to calculate how many supply sites are at least needed. The bombers' routes are linesy=kx+b given in format ask,b, of coursekb are same to their ordinary meanings. Assume the battle region is a rectangle with infinity height, the left x-cooridinate and right x-cooridinate are given so that the width of rectangle is fixed.

Input

The input consists of multiple cases.
The first line are the left x-cooridinate a and right x-cooridinate b of battle region. a b are both in the range of [0,1000]. Of coursea will not exceedb.
Next lines will describe enemy bombing routes number n.n can be up to 30000.
Following n lines are k and b of each bombing route.kb are in range of [-100000,100000].
It's guaranteed that no three lines (including the two battle region bound lines) will go through one point.

Output

Output the least number of supply sites needed. 


Sample Input
1 2
1
1 5



Sample Output
2


Hint

In sample, line y=x+5 divides the region between x=1 and x=2 into two parts, so the outcome is 2. 


在l,r区域之间求多条线段能把区域分成多少块,ans=区域内的交点个数+总线段条数+1

由于线与左右边界的焦点,k*l+b与k*r+b的值有正有负,统一按从大到小排序

结构按左边界从大到小,右边界从大到小排序

由于k*l+b的值太大,所以树状数组只存放右边界所会出现点的个数

用map存放右边界ki*r+bi在从上往下数第几个位置



#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<map>
#define Max 30010
using namespace std;
int j;
long long c[Max];
long long y[Max];
map<long long,int> mp;

struct me{
	long long x;
	long long  y;
}a[Max];
int lowbit(int t){
	return t&(-t);
}
void insert(int t){
	while(t<=j-1){
		c[t]++;
		t+=lowbit(t);
	}
}
long long getsum(int t){
	long long sum=0;
	while(t>0){
		sum+=c[t];
		t-=lowbit(t);
	}
	return sum;
}
int imp(const void *a,const void *b){
	if(((me *)b)->x==((me *)a)->x)
		return ((me *)b)->y-((me *)a)->y;
	else return ((me *)b)->x-((me *)a)->x;
}
int imp1(const void *a,const void *b){
	return *(long long *)b-*(long long *)a;
}
int main(void){
	int l,r,n;
	int k,b,i;
	while(~scanf("%d%d",&l,&r)){
		scanf("%d",&n);
		mp.clear();
		memset(c,0,sizeof(c));
		for(i=0;i<n;i++){
			scanf("%d%d",&k,&b);
			a[i].x=k*l+b;
			a[i].y=k*r+b;
			y[i]=a[i].y;
		}
		qsort(a,n,sizeof(a[0]),imp);
		qsort(y,n,sizeof(y[0]),imp1);
		j=1;
		for(i=0;i<n-1;i++)
			if(y[i]!=y[i+1]){
				mp[y[i]]=j;
				j++;
			}
		mp[y[i]]=j;
		j++;
		long long sum=0;
		for(i=0;i<n;i++){
            sum+=getsum(j-1)-getsum(mp[a[i].y]);
            insert(mp[a[i].y]);
        }
        sum=sum+n+1;
        printf("%lld\n",sum);
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值