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 ...
随机推荐
- css初始化代码
最近老有新项目开发,一直在找存留的CSS初始化代码,索性放到这里备份下, @charset "utf-8"; /* -------------------------------- ...
- gdb调试汇编堆栈过程的学习
gdb调试汇编堆栈过程的学习 以下为C源文件 使用gcc - g code.c -o code -m32指令在64位的机器上产生32位汇编,然后使用gdb example指令进入gdb调试器: 进入之 ...
- PLSQL note
sql%count 最近一次sql执行的件数SUBSTR(string , int i) // i番目から最後までの文字列を切り取るSUBSTR(string , int i, int j) // i ...
- Shell 脚本实现随机抽取班级学生
#/bin/bash function rand(){ min=$ max=$(($-$min+)) num=$(date +%s%N) echo $(($num%$max+$min)) } rnd= ...
- java 代码解压7z(带密码)转载请注明出处,谢谢
<sevenzipjbinding.version>9.20-2.00beta</sevenzipjbinding.version> <dependency> &l ...
- HTML入门教程 这里可以免费学习啦
本文目标 30分钟内让你明白HTML是什么,并对它有一些基本的了解.一旦入门后,你可以从网上找到更多更详细的资料来继续学习. 什么是HTML HTML是英文Hyper Text Mark-up Lan ...
- Notepad++正则表达式语法
\ 转义字符 如:要使用 “\” 本身, 则应该使用“\\” \t Tab制表符 注:扩展和正则表达式都支持 \r 回车符CR 注:扩展支持,正则表达式不支持 \n 换行符LF ...
- zeromq系列
ZeroMQ系列 之NetMQ 一:zeromq简介 二:NetMQ 请求响应模式 Request-Reply 三:NetMQ 发布订阅模式 Publisher-Subscriber 四:NetMQ ...
- 函数式编程之柯里化(curry)
函数式编程curry的概念: 只传递给函数一部分参数来调用函数,然后返回一个函数去处理剩下的参数. var add = function(x) { return function(y) { retur ...
- python爬虫实战
http://www.jb51.net/article/57161.htm python在线编程:http://www.pythontip.com/coding/run c语言在线编程:http:// ...