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最近又较量上了,但是毕竟都是成年人,他们已经不 ...
随机推荐
- javascript多投事件的处理 (转)
出处 http://blog.csdn.net/dead_of_winter/article/details/1646367 尽管ecma标准指定了addEventListener这样的方法来实现事件 ...
- pip install 升级时候 出现报asciii码错误的问题。
原因是pip安装python包会加载我的用户目录,我的用户目录恰好是中文的,ascii不能编码.解决办法是: python目录 Python27\Lib\site-packages 建一个文件site ...
- day09作业
一.填空题 1.方法 2.堆内存 3.构造方法 4.this 5.this 6.static 7.使用类名进行访问 8.package import class 9.关键字 10.lang 二.选择题 ...
- MFC中CString.Format类详解
在MFC程序中,使用CString来处理字符串是一个很不错的选择.CString既可以处理Unicode标准的字符串,也可以处理ANSI标准的字符串.CString的Format方法给我们进行字符串的 ...
- 序列化 json和pickle
序列化 1. 什么叫序列化 将原本的字典.列表等内容转换成一个字符串的过程就叫做序列化. 2. json dumps loads 一般对字典和列表序列化 dump load 一般对 ...
- IntelliJ IDEA + Maven + Tomcat 本地开发、部署、调试。
1.maven 下载 解压 配置下 远程仓库( 用阿里云的 比较快).本地仓库 (可以本地C盘建立个文件夹当仓库).环境变量(方便使用maven命令)就可以了. 2.tomcat 下载 解压 配置下 ...
- ZooKeeper的典型应用场景
<从Paxos到Zookeeper 分布式一致性原理与实践>读书笔记 本文:总结脑图地址:脑图 前言 所有的典型应用场景,都是利用了ZK的如下特性: 强一致性:在高并发情况下,能够保证节点 ...
- C++之构造函数的继承
#include<iostream> usingnamespace std; classBase1 { public: Base1()=default; Base1(const strin ...
- R vs Python:载入包 import & library
数据科学:R & Python 工作 & Kaggle机器学习比赛 可重复函数式编程 一.Python模块的载入 包 Package 模块 module import pandas a ...
- Vue-Socket.io
github地址:https://github.com/MetinSeylan/Vue-Socket.io 安装: npm install vue-socket.io -S 注册: import Vu ...