Under Attack II

来源:互联网 发布:js弹出选择保存路径 编辑:程序博客网 时间:2024/05/18 23:56

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 Input1 2
1
1 5


Sample Output2


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 30010using 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;}