Problem Counting Intersections

题目大意

  给定n条水平或竖直的线段,统计所有线段的交点个数。 (n<=100000)

解题分析

  首先将线段离散化。

  然后将所有线段按照横坐标的顺序,按照先插入再查找再删除的顺序进行操作。

  对于横线 如果是左端点,则将其纵坐标加1,右端点则减1,对于竖线直接求和就行了。

参考程序

 #include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std; #define N 100008
#define M 50008
#define LL long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define clr(x,v) memset(x,v,sizeof(x));
#define rep(x,y,z) for (int x=y;x<=z;x++)
#define repd(x,y,z) for (int x=y;x>=z;x--)
const int mo = ;
const int inf = 0x3f3f3f3f;
const int INF = ;
/**************************************************************************/
struct line{
int x,y,z;
line(int x=,int y=,int z=):x(x),y(y),z(z){}
bool operator <(const line b) const{
return x<b.x || x==b.x && z<b.z;
}
}a[N],b[N],c[N*]; int val[N*],cnt; int id(int x){
return lower_bound(val+,val+cnt+,x)-val;
} struct Binary_Indexed_Tree{
int a[N*];
void clear(){
clr(a,);
}
void insert(int x,int val){
for (int i=x;i<=N<<;i+=i & (-i))
a[i]+=val;
}
int sigma(int x){
int res=;
for (int i=x;i>;i-=i & (-i))
res+=a[i];
return res;
}
int query(int x,int y){
return sigma(y)-sigma(x-);
}
}T; int main(){
int testcase;
scanf("%d",&testcase);
while (testcase--){
int n,na=,nb=,nc=;
cnt=;
scanf("%d",&n);
rep(i,,n){
int x1,y1,x2,y2;
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
val[++cnt]=x1;
val[++cnt]=y1;
val[++cnt]=x2;
val[++cnt]=y2;
if (x1==x2){
if (y1>y2) swap(y1,y2);
b[++nb]=line(x1,y1,y2);
}
if (y1==y2){
if (x1>x2) swap(x1,x2);
a[++na]=line(y1,x1,x2);
}
}
sort(val+,val+cnt+);
cnt=unique(val+,val+cnt+)-val-;
rep(i,,na){
a[i].x=id(a[i].x);
a[i].y=id(a[i].y);
a[i].z=id(a[i].z);
c[++nc]=line(a[i].y,i,);
c[++nc]=line(a[i].z,i,);
}
rep(i,,nb){
b[i].x=id(b[i].x);
b[i].y=id(b[i].y);
b[i].z=id(b[i].z);
c[++nc]=line(b[i].x,i,);
}
sort(c+,c+nc+);
T.clear();
LL ans=;
rep(i,,nc){
if (c[i].z==){
T.insert(a[c[i].y].x,);
}
if (c[i].z==){
ans+=T.query(b[c[i].y].y,b[c[i].y].z);
}
if (c[i].z==){
T.insert(a[c[i].y].x,-);
}
}
printf("%lld\n",ans );
}
}

  

HDU 5862(离散化+树状数组)的更多相关文章

  1. HDU 4325 离散化+树状数组 或者 不使用树状数组

    题意:给出一些花的开放时间段,然后询问某个时间点有几朵花正在开放. 由于ti<1e9,我们需要先将时间离散化,然后将时间点抽象为一个数组中的点,显然,我们需要进行区间更新和单点查询,可以考虑线段 ...

  2. HDU 5862 Counting Intersections(离散化+树状数组)

    HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...

  3. hdu 3015 Disharmony Trees (离散化+树状数组)

    Disharmony Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. HDU 6318.Swaps and Inversions-求逆序对-线段树 or 归并排序 or 离散化+树状数组 (2018 Multi-University Training Contest 2 1010)

    6318.Swaps and Inversions 这个题就是找逆序对,然后逆序对数*min(x,y)就可以了. 官方题解:注意到逆序对=交换相邻需要交换的次数,那么输出 逆序对个数 即可. 求逆序对 ...

  5. CodeForces 540E - Infinite Inversions(离散化+树状数组)

    花了近5个小时,改的乱七八糟,终于A了. 一个无限数列,1,2,3,4,...,n....,给n个数对<i,j>把数列的i,j两个元素做交换.求交换后数列的逆序对数. 很容易想到离散化+树 ...

  6. Ultra-QuickSort(归并排序+离散化树状数组)

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 50517   Accepted: 18534 ...

  7. BZOJ_4627_[BeiJing2016]回转寿司_离散化+树状数组

    BZOJ_4627_[BeiJing2016]回转寿司_离散化+树状数组 Description 酷爱日料的小Z经常光顾学校东门外的回转寿司店.在这里,一盘盘寿司通过传送带依次呈现在小Z眼前.不同的寿 ...

  8. poj-----Ultra-QuickSort(离散化+树状数组)

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 38258   Accepted: 13784 ...

  9. Code Forces 652D Nested Segments(离散化+树状数组)

     Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  10. 【bzoj4627】[BeiJing2016]回转寿司 离散化+树状数组

    题目描述 给出一个长度为n的序列,求所有元素的和在[L,R]范围内的连续子序列的个数. 输入 第一行包含三个整数N,L和R,分别表示寿司盘数,满意度的下限和上限. 第二行包含N个整数Ai,表示小Z对寿 ...

随机推荐

  1. OC 类别(分类)Categroy

    Categroy类别,又称为扩展类,在类的原基础上扩展方法,且不可添加变量,如果扩展的方法与原始类中的方法相同,则会隐藏原始方法,且不可在扩展方法中通过super调用原始方法,这里与继承不同. 定义: ...

  2. ERP开发分享 1 数据库表设计

    这是我的ERP设计经验分享系列,今天讲的是数据库的表设计(1),主要阐述: 1.单字段的主键:2.使用int32作为主键类型:3.使用版本字段处理乐观锁定:4.生效字段标明是否允许“被使用”:5.锁定 ...

  3. BZOJ1066 [SCOI2007]蜥蜴

    首先...这是道(很水的)网络流 我们发现"每个时刻不能有两个蜥蜴在同一个柱子上"这个条件是没有用的因为可以让外面的先跳,再让里面的往外跳 但是还有柱子高度的限制,于是把柱子拆点为 ...

  4. ArrayAdapter适配器的用法,模拟QQ发消息界面。

    import java.util.ArrayList; import android.app.Activity; import android.content.Context; import andr ...

  5. plist文件的读取

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"cinemalist" ofType:@"pl ...

  6. UVa 10561 - Treblecross

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  7. PowerMock与EasyMock的应用(转)

    Leader请求在做Junit测试的时辰,Mock掉各个办法之间的依附.这两天进修了下PowerMock的应用. PowerMock是EasyMock的一个扩大,参加了static,final,pri ...

  8. Ibatis.Net 各种配置说明(二)

    一.各个配置文件的作用说明 providers.config:指定数据库提供者,.Net版本等信息. xxxxx.xml:映射规则. SqlMap.config:大部分配置一般都在这里,如数据库连接等 ...

  9. jstl c:choose>、<c:when>和<c:otherwise>标签

    <c:choose>.<c:when>和<c:otherwise>在一起连用,可以实现Java语言中的if-else语句的功能.例如以下代码根据username请求 ...

  10. hdu 1034 (preprocess optimization, property of division to avoid if, decreasing order process) 分类: hdoj 2015-06-16 13:32 39人阅读 评论(0) 收藏

    IMO, version 1 better than version 2, version 2 better than version 3. make some preprocess to make ...