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. linux安装.run

    chmod +x ./framework-3.6.0-linux-full.runsudo ./framework-3.6.0-linux-full.run

  2. MVC扩展ValueProvider,通过实现IValueProvider接口创建SessionValueProvider

    □ ValueProvider的大致工作原理 →通过Request.Form, Request.QueryString, Request.Files, RouteData.Values获取数据.→然后 ...

  3. sql里Where条件顺序

    以前的理解: sql语句里where后面的条件是否分先后顺序的 ,比如 A and B and C和 C and B and A 是一样,不像C语言 A && B 与B &&a ...

  4. Tomcat配置虚拟主机的两种方式

    1.基于主机名的虚拟主机配置 在随意盘符下建立一个目录作为虚拟地址的目录.例如:F:\virtualhost1,在其下建立 test1.html,写入内容例如:test 在tomcat/conf/se ...

  5. UVALive 6948 Jokewithpermutation dfs

    题目链接:UVALive 6948  Jokewithpermutation 题意:给一串数字序列,没有空格,拆成从1到N的连续数列. dfs. 可以计算出N的值,也可以直接检验当前数组是否合法. # ...

  6. 流控panabit的安装及配置

    软件: 在panabit的下载页面上,没有最新的版本.刚开始就是从这个地方下载的,但是有一块网卡怎么也找不到.各种加载网卡驱动,最后失败. 之后,从其论坛中发现了最新的2013.05版本,将ISO刻盘 ...

  7. 创建缓存文件(。php)

    public function user_dengji(){        $this->sdb->select('groupid,grouptitle');        $query ...

  8. 保护WIFI无线网络的安全

    本篇博客属于我们隐私与安全小贴士系列博客的一部分,其目的是确保您以及您的家人的上网安全.隐私与安全问题无论对我们还是对您都至关重要.我们在“不可 不知的小知识”网站上为您提供了如何安全,便捷地使用互联 ...

  9. [开发笔记]-获取歌曲ID3信息

    ID3介绍: ID3,一般是位于一个mp3文件的开头或末尾的若干字节内,附加了关于该mp3的歌手,标题,专辑名称,年代,风格等信息,该信息就被称为ID3信息,ID3信息分为两个版本,v1和v2版. 获 ...

  10. Cisco IOS Debug Command Reference Command E through H

    debug eap through debug he-module subslot periodic debug eap : to display information about Extensib ...