Zoj 3157 calculates the ry + tree array (number of reverse orders)

Source: Internet
Author: User

Http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 3128

Question: give you n straight lines (n <= 10000) and calculate the number of intersections in the (L, R) interval.

Solution: Calculate the intersection between each line and x = L and x = R respectively, and then convert the two ends of N line segments to X = L and x = R. There are several intersections, in this way, the reverse order number is solved.

#include<cstdio>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>using namespace std;const double eps = 1e-8;const int maxn = 10010;int c[maxn],ans;void update(int x,int d){for(;x>0;x-=x&-x)  c[x]+=d;}int sum(int x){for(ans=0;x<maxn;x+=x&-x)  ans+=c[x];return ans;}struct point {double x,y;};struct L{point a,b;double h1,h2;int id;}in[maxn];int tmp[maxn];int cmp(L x,L y){if(fabs(x.h1-y.h1)<eps)  return x.h2<y.h2;return x.h1<y.h1;}int cmp2(L x,L y){if(fabs(x.h2-y.h2)<eps)  return x.h1<y.h1;return x.h2<y.h2;}int main(){int n;double l,r;while(scanf("%d",&n)!=EOF){for(int i=1;i<=n;i++)scanf("%lf%lf%lf%lf",&in[i].a.x,&in[i].a.y,&in[i].b.x,&in[i].b.y);scanf("%lf%lf",&l,&r);for(int i=1;i<=n;i++){double k=(in[i].b.y-in[i].a.y)/(in[i].b.x-in[i].a.x);in[i].h1=k*(l-in[i].a.x)+in[i].a.y;in[i].h2=k*(r-in[i].a.x)+in[i].a.y;}sort(in+1,in+n+1,cmp);for(int i=1;i<=n;i++)in[i].id=i;sort(in+1,in+n+1,cmp2);for(int i=1;i<=n;i++) tmp[in[i].id]=i;memset(c,0,sizeof(c));int s=0;for(int i=1;i<=n;i++){s+=sum(tmp[i]+1);update(tmp[i],1);}printf("%d\n",s);}return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.