zoj 3157 Weapon 逆序数/树状数组
Description
In World War 3, your countries' scientists have invented a special weapon. Assume that the enemy's city can be described by rectangular coordinates and it has n roads which are all lines. None of the road is paralled with Y-axis. Besides, each road is represented by two different points (ai,bi) (ci,di) on it. Any three roads will not intersect at one point.
This special weapon can destroy all the castles whose x coordinate belongs to (l,r). After spying, you know that all the castles are set in the crossing point of two roads and in each crossing point there is a castle. In addition, each road's end-point's x coordinate does not belong to (l,r).
The scientists want to check the weapon's effect. If its effect can not reach army's expectation, they have to spend more time and more money in expanding its range. Obviously, the number of castles it can destroy plays an important role on the effect. So you are asked to calculate how many castles can be destroyed by this special weapon.
Input
Input contains multiple cases.
Every test case, the first line is an integers n (2 <= n <= 10000). Then n lines follow. The (i+1)-th line contains four integers ai,bi,ci,di (-1E8 <= ai,bi,ci,di <= 1E8). The (n+2)-th line contains two doubles l,r (-1E8 <= l,r <= 1E8) There is a blank line between two cases.
Output
For each case, output the number of castles that can be destroyed by the weapon.
Sample Input
3
0 0 1 1
2 0 1 1
0 0 2 0
0 2.5
Sample Output
2
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 10001
#define eps 1e-6
const int inf=0x7fffffff; //无限大
int N;
struct node
{
double x;
double y;
};
double d[maxn];
node point1[maxn],point2[maxn];
node kiss[maxn],kill[maxn];
int lowbit(int x)
{
return x&(-x);
} void update1(int x)
{
while(x<=N)
{
d[x]++;
x+=lowbit(x);
}
} void update2(int x,int num)
{
while(x>)
{
d[x]+=num;
x-=lowbit(x);
}
} int getSum1(int x)
{
int s=;
while(x>)
{
s+=d[x];
x-=lowbit(x);
}
return s;
} bool cmp(node x,node y)
{
if(x.x==y.x)
return x.y<y.y;
return x.x<y.x;
}
int main()
{
sspeed;
int n;
while(cin>>n)
{
memset(d,,sizeof(d));
N=n;
for(int i=;i<n;i++)
{
cin>>point1[i].x>>point1[i].y>>point2[i].x>>point2[i].y;
}
double l,r;
cin>>l>>r;
l+=eps;
r-=eps;
for(int i=;i<n;i++)
{
double k;
k=(point2[i].y-point1[i].y)/(point2[i].x-point1[i].x);
kill[i].x=k*(l-point2[i].x)+point2[i].y;
kill[i].y=k*(r-point2[i].x)+point2[i].y;//求出
}
ll ans=;
sort(kill,kill+n,cmp);
for(int i=;i<n;i++)
{
kiss[i].x=kill[i].y;
kiss[i].y=i+;
}
sort(kiss,kiss+n,cmp);
for(int i=n-;i>=;i--)
{
ans+=getSum1(kiss[i].y);
update1(kiss[i].y);
}
cout<<ans<<endl;
}
return ;
}
zoj 3157 Weapon 逆序数/树状数组的更多相关文章
- POJ 2299 Ultra-QuickSort 逆序数 树状数组 归并排序 线段树
题目链接:http://poj.org/problem?id=2299 求逆序数的经典题,求逆序数可用树状数组,归并排序,线段树求解,本文给出树状数组,归并排序,线段树的解法. 归并排序: #incl ...
- Ultra-QuickSort---poj2299 (归并排序.逆序数.树状数组.离散化)
题目链接:http://poj.org/problem?id=2299 题意就是求把数组按从小到大的顺序排列,每次只能交换相邻的两个数, 求至少交换了几次 就是求逆序数 #include<std ...
- ACM学习历程—HDU5592 ZYB's Premutation(逆序数 && 树状数组 && 二分)(BestCoder Round #65 1003)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5592 题目大意就是给了每个[1, i]区间逆序对的个数,要求复原原序列. 比赛的时候2B了一发. 首先 ...
- cdoj 841 休生伤杜景死惊开 逆序数/树状数组
休生伤杜景死惊开 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 陆伯言军陷八卦 ...
- hdu 1394 Minimum Inversion Number 逆序数/树状数组
Minimum Inversion Number Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showprob ...
- [BZOJ 3295] [luogu 3157] [CQOI2011]动态逆序对(树状数组套权值线段树)
[BZOJ 3295] [luogu 3157] [CQOI2011] 动态逆序对 (树状数组套权值线段树) 题面 给出一个长度为n的排列,每次操作删除一个数,求每次操作前排列逆序对的个数 分析 每次 ...
- Bzoj 2141: 排队 分块,逆序对,树状数组
2141: 排队 Time Limit: 4 Sec Memory Limit: 259 MBSubmit: 1310 Solved: 517[Submit][Status][Discuss] D ...
- 求逆序对[树状数组] jdoj
求逆序对 题目大意:给你一个序列,求逆序对个数. 注释:n<=$10^5$. 此题显然可以跑暴力.想枚举1到n,再求在i的后缀中有多少比i小的,统计答案即可.这显然是$n^2$的.这...显然过 ...
- 洛谷 P1908 逆序对(树状数组解法)
归并排序解法:https://www.cnblogs.com/lipeiyi520/p/10356882.html 题目描述 猫猫TOM和小老鼠JERRY最近又较量上了,但是毕竟都是成年人,他们已经不 ...
随机推荐
- MySQL分布式集群之MyCAT(一)简介【转】
隔了好久,才想起来更新博客,最近倒腾的数据库从Oracle换成了MySQL,研究了一段时间,感觉社区版的MySQL在各个方面都逊色于Oracle,Oracle真的好方便!好了,不废话,这次准备记录一些 ...
- Python爬虫学习1: Requests模块的使用
Requests函数库是学习Python爬虫必备之一, 能够帮助我们方便地爬取. Requests: 让HTTP服务人类. 本文主要参考了其官方文档. Requests具有完备的中英文文档, 能完全满 ...
- java基础43 IO流技术(输入字节流/缓冲输入字节流)
通过File对象可以读取文件或者文件夹的属性数据,如果要读取文件的内容数据,那么我们就要使用IO技术. 一.输入字节流 输入字节流的体系: -------| InputStream:所有输入字节流的 ...
- asterisk控制台取消NOTICE信息
中道拨号方案取消了控制台输出的NOTICE信息: 方法:vim /etc/asterisk/logger.conf;console => notice,warning,error把上面这行取消 ...
- Java 泛型和类型安全的容器
使用java SE5之前的容器的一个主要问题就是编译器允许你向容器插入不正确的类型,例如: //: holding/ApplesAndOrangesWithoutGenerics.java // Si ...
- 接口测试工具--Poster与Postman的简单实用
HTTP/SOAP协议接口的功能测试: 1.浏览器URL(GET请求) http://127.0.0.1:8000/login/?username=zhangsan&password=1234 ...
- hdu 1398 整数划分变形 (母函数)
有1,4,9,16,25.....2^17这么多面值的硬币,问任意给定一个不大于300的正整数面额,用这些硬币来组成此面额总共有多少种组合种数 比如10全14 + 6个 14+4+1+19+1 求(1 ...
- android拾遗——四大基本组件介绍与生命周期
Android四大基本组件分别是Activity,Service服务,Content Provider内容提供者,BroadcastReceiver广播接收器. 一:了解四大基本组件 Activity ...
- centos7 关闭默认firewalld,开启iptables
编者按: 对于使用了centos6系列系统N年的运维来说,在使用centos7的时候难免会遇到各种不适应.比如防火墙问题.本文主要记录怎么关闭默认的firewalld防火墙,重新启用iptables. ...
- HashMap 在 Java1.7 与 1.8 中的区别
hashMap 数据结构 如上图所示,JDK7之前hashmap又叫散列链表:基于一个数组以及多个链表的实现,hash值冲突的时候,就将对应节点以链表的形式存储. JDK8中,当同一个hash值(Ta ...