zoj 3574 求给定区间内n条线段能将区间内的区域划分成几个区域

答案是:n+1+逆序数的数量

因为如果n条线段都不相交,一定可以划分成n+1个区域,然后就画图YY一下

View Code
#include <cstdio>  
#include <algorithm>
using namespace std;
int left[50003], right[50003];
int cnt;
struct node {
int y1,y2;
}p[30010];
int cmp(node a,node b){
return a.y1<b.y1;
}
void merge(int a[], int l, int m, int r) {
int i, j, k, n1, n2;
n1 = m - l + 1;
n2 = r - m;
for (i = 0; i < n1; i++)
left[i] = a[l+i];
for (i = 0; i < n2; i++)
right[i] = a[m+i+1];
left[n1] = right[n2] = 0x7fffffff;
i = j = 0;
for (k = l; k <= r; k++) {
if (left[i] <= right[j]) a[k] = left[i++];
else {
a[k] = right[j++];
cnt += n1 - i;
}
}
}
void mergeSort(int a[], int l, int r) {
if (l < r) {
int m = (l + r) / 2;
mergeSort(a, l, m);
mergeSort(a, m+1, r);
merge(a, l, m, r);
}
}
int a[50011];
int main() {
int n;
int l,r;
while (scanf("%d%d",&l,&r)!=EOF) {
scanf("%d",&n);
int k,b;
for(int i=0;i<n;i++){
scanf("%d%d",&k,&b);
p[i].y1=k*l+b;
p[i].y2=k*r+b;
}
sort(p,p+n,cmp);
for(int i=0;i<n;i++) a[i]=p[i].y2;
cnt=0;mergeSort(a, 0, n-1);
printf("%d\n", n+cnt+1);
}
}



你可能感兴趣的:(ZOJ)