HDU3465 树状数组逆序数
Life is a Line
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 1927 Accepted Submission(s): 471
Maybe have met, maybe just a matter of time, two unparallel lines will always meet in some places, and now a lot of life (i.e. line) are in the same coordinate system, in a given open interval, how many pairs can meet each other?
Each test case begin with one integer N (1 ≤ N ≤ 50000), indicating the number of different lines.
Then two floating numbers L, R follow (-10000.00 ≤ L < R ≤ 10000.00), indicating the interval (L, R).
Then N lines follow, each line contains four floating numbers x1, y1, x2, y2 (-10000.00 ≤ x1, y1, x2, y2 ≤ 10000.00), indicating two different points on the line. You can assume no two lines are the same one.
The input terminates by end of file marker.
0.0 2.0
0.0 2.5 2.5 0.0
/*
两直线在l,r之内有交点当且仅当(a1-a1)*(b1-b2)<0,a1,b1是其中一条直线与l,r的两个交点,这样按照a排一次序
只要b有交错就会有交点,然后就转化成求逆序数,先按照a从小到大排,给他们编一个号,在按照b从小到大排序
这样只要求编号的逆序数即可。特殊情况是直线平行y轴,此时他与其他的不平行于y轴的直线都相交。
求逆序数是当前的数的位置减去前面比他小的数的个数也就是正序数个数。
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int A[];
struct Lu
{
double a,b;
int num;
}L[];
bool cmp1(Lu x,Lu y)
{
if(x.a==y.a)
return x.b<y.b;
return x.a<y.a;
}
bool cmp2(Lu x,Lu y)
{
if(x.b==y.b)
return x.a<y.a;
return x.b<y.b;
}
int lowbit(int id)
{
return id&(-id);
}
void add(int id,int v)
{
while(id<=){
A[id]+=v;
id+=lowbit(id);
}
}
int qury(int id)
{
int s=;
while(id>){
s+=A[id];
id-=lowbit(id);
}
return s;
}
int main()
{
int n;
double l,r,x1,x2,y1,y2;
while(scanf("%d",&n)!=EOF){
scanf("%lf%lf",&l,&r);
int ans=,tem=,cnt=;
for(int i=;i<n;i++){
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
if(x1==x2){ //在l,r以外就去掉,在l,r以内要计算,就是这里wa到吐血。
if(x1>l&&x1<r) tem++;
continue;
}
L[cnt].a=y2+(y1-y2)*(l-x2)/(x1-x2);
L[cnt++].b=y2+(y1-y2)*(r-x2)/(x1-x2);
}
sort(L,L+cnt,cmp1);
for(int i=;i<cnt;i++){
L[i].num=i+;
}
sort(L,L+cnt,cmp2);
memset(A,,sizeof(A));
for(int i=;i<cnt;i++){
add(L[i].num,);
ans+=(i-qury(L[i].num-)); //求逆序数
}
printf("%d\n",ans+tem*cnt);
}
return ;
}
HDU3465 树状数组逆序数的更多相关文章
- HDU 4911 (树状数组+逆序数)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4911 题目大意:最多可以交换K次,就最小逆序对数 解题思路: 逆序数定理,当逆序对数大于0时,若ak ...
- hdu2838Cow Sorting(树状数组+逆序数)
题目链接:点击打开链接 题意描写叙述:给定一个长度为100000的数组,每一个元素范围在1~100000,且互不同样,交换当中的随意两个数须要花费的代价为两个数之和. 问怎样交换使数组有序.花费的代价 ...
- HDU5196--DZY Loves Inversions 树状数组 逆序数
题意查询给定[L, R]区间内 逆序对数 ==k的子区间的个数. 我们只需要求出 子区间小于等于k的个数和小于等于k-1的个数,然后相减就得出答案了. 对于i(1≤i≤n),我们计算ri表示[i,ri ...
- [树状数组+逆序对][NOIP2013]火柴排队
火柴排队 题目描述 涵涵有两盒火柴,每盒装有n根火柴,每根火柴都有一个高度.现在将每盒中的火柴各自排成一列,同一列火柴的高度互不相同,两列火柴之间的距离定义为:∑ (ai-bi)2,i=1,2,3,. ...
- hdu 5497 Inversion 树状数组 逆序对,单点修改
Inversion Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5497 ...
- Codevs 3286 火柴排队 2013年NOIP全国联赛提高组 树状数组,逆序对
题目:http://codevs.cn/problem/3286/ 3286 火柴排队 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : ...
- Bzoj 2789: [Poi2012]Letters 树状数组,逆序对
2789: [Poi2012]Letters Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 278 Solved: 185[Submit][Stat ...
- Bzoj 3295: [Cqoi2011]动态逆序对 分块,树状数组,逆序对
3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2886 Solved: 924[Submit][Stat ...
- Bzoj 3289: Mato的文件管理 莫队,树状数组,逆序对,离散化,分块
3289: Mato的文件管理 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 1539 Solved: 665[Submit][Status][Di ...
随机推荐
- sh7.创建yum源脚本练习
练习1, 写一个脚本分别计算1-100,之间奇数和偶数之和 ji_sum.sh #!/bin/bash # let ..};do ] -eq ];then let sum+=I fi done ech ...
- java 多态和内部类
接口跟接口之间存在继承关系 一个接口可以继承多个接口 一个非抽象类:必须实现接口中的所有方法 一个抽象类实现接口 可以不实现接口中的方法 但是继承与抽象类的类必须要是实现接口中的方法 多态:一个对 ...
- 解决css3毛玻璃效果(blur)有白边问题
做一个登录页,全屏背景图毛玻璃效果,实现方法如下: HTML: <body> <div class="login-wrap"> <div class= ...
- mac包管理器Homebrew安装命令
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
- SQL 常用脚本
SELECT o.name AS tableName,c.name AS colName,k.keyno AS sort FROM sysindexes i JOIN sysindexkeys k O ...
- Sql server2012还原备份文件语句
--sql2012还原sql2008语句 --选择master数据库,新建查询 输入下面sql语句 --选择兼容模式(sql 2008)创建数据库db(还原时db写成原生数据库名称) RESTORE ...
- Jquery的$(selector).each()和$.each()原理和区别
我们都用过Jqurey中的each函数,都知道each()有两种方式去调用,一种是通过$.each()调用,另一种是$(selector).each()去调用,那么它们之间有什么区别? 翻看一下Jqu ...
- Java源码之 java.util.concurrent 学习笔记01
准备花点时间看看 java.util.concurrent这个包的源代码,来提高自己对Java的认识,努力~~~ 参阅了@梧留柒的博客!边看源码,边通过前辈的博客学习! 包下的代码结构分类: 1.ja ...
- make: *** [out/host/linux-x86/obj/EXECUTABLES/obbtool_intermediates/Main.o] Error 1
在ubuntu12.04下编译android时,若出现如此错误,那是因为gcc版本太高. 通过gcc -v命令可以发现gcc库在/usr/lib/gcc/i686-linux-gnu目录下,该目录下有 ...
- ExtJs服务器端代理(Ajax)
服务器端代理: Ajax:在当前域中发送请求 JsonP:跨域的请求 Rest:与服务器进行RESTful(GET/PUT/POST/DELETE)交互 Direct:使用 Ext.direct.M ...