poj3067
求交点的个数;
容易发现,对于两条航线(xi,yi)和(xj,yj),设xi<xj
只有yi>yj时两条航线存在交点;
于是我们考虑以x为第一关键字减序,y为第二关键字为减序排序;
则对于当前航线(xi,yi),只要找之前所有yj小于yi的个数
所有交点数就是其总和,统计就要用到飘逸的树状数组了~
var a,c:array[..] of longint;
x,y:array[..] of longint;
i,j,n,m,k,t:longint;
ans:int64; procedure add(p:longint);
begin
while p<=m do
begin
inc(c[p]);
p:=p+lowbit(p);
end;
end;
function ask(p:longint):longint;
begin
ask:=;
while p<> do
begin
ask:=ask+c[p];
p:=p-lowbit(p);
end;
end;
procedure sort(l,r: longint);
var i,j,h,p: longint;
begin
i:=l;
j:=r;
h:=x[(l+r) div ];
p:=y[(l+r) div ];
repeat
while (x[i]<h) or (x[i]=h) and (y[i]<p) do inc(i);
while (h<x[j]) or (x[j]=h) and (p<y[j]) do dec(j);
if not(i>j) then
begin
swap(x[i],x[j]);
swap(y[i],y[j]);
inc(i);
j:=j-;
end;
until i>j;
if l<j then sort(l,j);
if i<r then sort(i,r);
end; begin
readln(t);
for i:= to t do
begin
readln(n,m,k);
for j:= to k do
readln(x[j],y[j]);
ans:=;
sort(,k);
fillchar(c,sizeof(c),);
fillchar(a,sizeof(a),);
inc(a[y[k]]);
add(y[k]);
for j:=k- downto do
begin
ans:=ans+ask(y[j]-); //这是唯一要注意的细节,交点一定不能在城市处
inc(a[y[j]]);
add(y[j]);
end;
writeln('Test case ',i,': ',ans);
end;
end.
poj3067的更多相关文章
- POJ-3067 Japan---树状数组逆序对变形
题目链接: https://vjudge.net/problem/POJ-3067 题目大意: 日本岛东海岸与西海岸分别有N和M个城市,现在修高速公路连接东西海岸的城市,求交点个数. 解题思路: 记每 ...
- poj3067树状数组求逆序数
Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Jap ...
- poj3067 Japan 树状数组求逆序对
题目链接:http://poj.org/problem?id=3067 题目就是让我们求连线后交点的个数 很容易想到将左端点从小到大排序,如果左端点相同则右端点从小到大排序 那么答案即为逆序对的个数 ...
- poj3067 二维偏序树状数组
题解是直接对一维升序排列,然后计算有树状数组中比二维小的点即可 但是对二维降序排列为什么不信呢?? /* */ #include<iostream> #include<cstring ...
- poj-3067(树状数组)
题目链接:传送门 题意:日本有东城m个城市,西城m个城市,东城与西城相互连线架桥,判断这些桥相交的次数. 思路:两个直线相交就是(x1-x2)*(y1-y2)<0,所以,对x,y进行排序,按照x ...
- poj3067 Japan(树状数组)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:id=3067">http://poj.org/problem? id=3067 Descri ...
- POJ3067(树状数组:统计数字出现个数)
Japan Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24151 Accepted: 6535 Descriptio ...
- POJ3067 Japan
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26270 Accepted: 7132 Description Japa ...
- POJ3067:Japan(线段树)
Description Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for th ...
随机推荐
- 前端encodeURIComponent 和后端http_build_query配合
解决特殊字符不能转义 1. function fixedEncodeURIComponent (str) { return encodeURIComponent(str).replace(/[!' ...
- container_of宏定义分析---linux内核
问题:如何通过结构中的某个变量获取结构本身的指针??? 关于container_of宏定义在[include/linux/kernel.h]中:/*_** container_of - cast a ...
- butterknife7.0.1使用
1.官网:http://jakewharton.github.io/butterknife/ Introduction Annotate fields with @Bind and a view ID ...
- [转]DRY原则和Shy原则
转自:http://blog.csdn.net/hukeab/article/details/2944675 保障可维护性的主要诀窍是遵循DRY原则和Shy原则. 在一个系统的整个生命周期里,理解 ...
- Linux安装oracle 10g常见问题之——OUI-25031
OUI-25031:Some of the configuration assistants failed/cancelled. 这是安装过程中常见的错误之一. 引起此错误的原因:/etc/hosts ...
- 元类metaClass
metaClass 实现动态改变对象的能力,这点特别像python(metaClass),Python中类(不是元类)的概念借鉴于Smalltalk groovy demo: class Person ...
- iOS 深复制&浅复制
1.无论是深复制还是浅复制,被复制的对象类型是不变的.此对象类型具有什么功能就具有什么功能,不会因为自行修改了返回对象的指针类型而改变. 比如: 这里的str和str1的值和指针地址完全一 ...
- uxpin books
http://uxpin.com/knowledge.html/?utm_source=Email+Marketing+Automation&utm_campaign=80d94e146a-l ...
- sql之解决数据库表的循环依赖问题
三张数据表关系如图: 其实出现表循环依赖情况:就是 同时 ConfigCompany和 Department 也有依赖,就会报错,说有循环依赖,就会报错. 所以 这个时候的解决办法:可以改 在数据库 ...
- Spring MVC与表单日期提交的问题
Spring MVC与表单日期提交的问题 spring mvc 本身并不提供日期类型的解析器,需要手工绑定, 否则会出现非法参数异常. org.springframework.beans.BeanIn ...