[zoj]4586 Under Attack II

题目大意:

给出n条直线,求将x=a,x=b所围区域分成多少个小区域。

思路:

n条直线对应[a,b]区域内n条线段。按线段左端点排序,求右端点的逆序对。 

View Code
 1 #include<iostream>
2 #include<stdio.h>
3 #include<string.h>
4 #include<algorithm>
5 #define MAX 31000
6 using namespace std;
7 typedef struct Line{
8 int x,y;
9 };
10 Line c[MAX];
11 int f[MAX]={0};
12 int ans;
13
14 void msort(int l,int r){
15 int mid,i,s,t;
16 if(l!=r){
17 mid=(l+r) >> 1;
18 msort(l,mid);
19 msort(mid+1,r);
20 s=l;t=mid+1;
21 for(i=l;i<=r;i++){
22 if(s<=mid && (t>r || c[s].y<=c[t].y) ){
23 f[i]=c[s].y;
24 s++;
25 }
26 else{
27 f[i]=c[t].y;
28 t++;
29 ans+=mid-s+1;
30 }
31 }
32 for(i=l;i<=r;i++) c[i].y=f[i];
33 }
34 }
35 bool cmp(const Line a, const Line b){
36 return ((a.x<b.x)||((a.x==b.x)&&(a.y<b.y)))?1:0;
37 }
38 int main(){
39 int a,b;
40 while (scanf("%d%d",&a,&b)!=EOF){
41 int n,i;
42 scanf("%d",&n);
43 for(i=1;i<=n;i++) {
44 int k,d;
45 scanf("%d%d",&k,&d);
46 c[i].x=k*a+d;
47 c[i].y=k*b+d;
48 }
49 sort(c+1,c+n+1,cmp);
50 ans=0;
51 //for(i=1;i<=n;i++)printf("%d %d\n",c[i].x,c[i].y);
52 msort(1,n);
53 //for(i=1;i<=n;i++)printf("%d %d\n",c[i].x,c[i].y);
54 printf("%d\n",ans+n+1);
55 }
56 }
posted @ 2012-02-26 14:49  ustc-acm  阅读(255)  评论(1编辑  收藏  举报