题目:https://open.kattis.com/problems/intersectingrectangles

题意::给你n个矩形,每一个矩形给你这个矩形的左下角的坐标和右上角的坐标,然后问你这些矩形会不会相交,如果存在相交的点,输出1,否则输出0。

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define lson root<<1,l,midd
#define rson root<<1|1,midd+1,r
const int M=2e5+;
struct node{
int l,r,h,flag;
}a[M];
bool cmp(node p,node q){
return p.h<q.h;
}
int lisan[M<<],tot1,tot2;
int tree[M<<];
void up(int root){
tree[root]=tree[root<<]+tree[root<<|];
}
void update(int p,int delta,int root,int l,int r)
{ if(l==r)
{
tree[root]+=delta;
return ;
}
int midd=(l+r)>>;
if(p<=midd)update(p,delta,lson);
else update(p,delta,rson);
up(root);
}
int query(int L,int R,int root,int l,int r)
{
if(L<=l&&r<=R)return tree[root];
//cout<<l<<"(("<<r<<endl;
int midd=(l+r)>>,res=;
if(L<=midd)res+=query(L,R,lson);
if(R>midd)res+=query(L,R,rson);
return res;
}
int main(){
int n;
scanf("%d",&n);
for(int x1,x2,y1,y2,i=;i<=n;i++){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
a[++tot1]=node({x1,x2,y1,});///1代表上边界
a[++tot1]=node({x1,x2,y2,-});///-1代表下边界
// lisan[tot2++]=x1-1;
// lisan[tot2++]=x1+1;
lisan[++tot2]=x1;
// lisan[tot2++]=x2-1;
lisan[++tot2]=x2;
// lisan[tot2++]=x2+1;
}
// cout<<tot1<<"@@"<<tot2<<endl;
sort(a+,a++tot1,cmp); sort(lisan+,lisan+tot2+);
int m=unique(lisan+,lisan++tot2)-lisan-;
int ans=;
for(int i=;i<tot1;i++){
node u=a[i];
int x=lower_bound(lisan+,lisan++m,u.l)-lisan;
int y=lower_bound(lisan+,lisan++m,u.r)-lisan;
/// cout<<x<<"~~~"<<y<<endl;
if(u.flag==1)///因为是上边界,所以要在更新前加边,才不会误判
ans|=(query(x,y,,,*n)!=); update(x,u.flag,,,*n);
update(y,u.flag,,,*n);
if(u.flag==-)
ans|=(query(x,y,,,*n)!=);
}
printf("%d\n",ans!=);
return ;
}

Kattis - intersectingrectangles 扫描线+线段树的更多相关文章

  1. HDU 3642 - Get The Treasury - [加强版扫描线+线段树]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3642 Time Limit: 10000/5000 MS (Java/Others) Memory L ...

  2. 【BZOJ3958】[WF2011]Mummy Madness 二分+扫描线+线段树

    [BZOJ3958][WF2011]Mummy Madness Description 在2011年ACM-ICPC World Finals上的一次游览中,你碰到了一个埃及古墓. 不幸的是,你打开了 ...

  3. HDU 3265/POJ 3832 Posters(扫描线+线段树)(2009 Asia Ningbo Regional)

    Description Ted has a new house with a huge window. In this big summer, Ted decides to decorate the ...

  4. 【bzoj4491】我也不知道题目名字是什么 离线扫描线+线段树

    题目描述 给定一个序列A[i],每次询问l,r,求[l,r]内最长子串,使得该子串为不上升子串或不下降子串 输入 第一行n,表示A数组有多少元素接下来一行为n个整数A[i]接下来一个整数Q,表示询问数 ...

  5. hdu1542 Atlantis(扫描线+线段树+离散)矩形相交面积

    题目链接:点击打开链接 题目描写叙述:给定一些矩形,求这些矩形的总面积.假设有重叠.仅仅算一次 解题思路:扫描线+线段树+离散(代码从上往下扫描) 代码: #include<cstdio> ...

  6. P3722 [AH2017/HNOI2017]影魔(单调栈+扫描线+线段树)

    题面传送门 首先我们把这两个贡献翻译成人话: 区间 \([l,r]\) 产生 \(p_1\) 的贡献当且仅当 \(a_l,a_r\) 分别为区间 \([l,r]\) 的最大值和次大值. 区间 \([l ...

  7. BZOJ 2584: [Wc2012]memory(扫描线+线段树)

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2584 题意:给出平面n个线段,任意两个线段严格不相交,且每个线段不平行于坐标轴.移 ...

  8. [BZOJ 1218] [HNOI2003] 激光炸弹 【n logn 做法 - 扫描线 + 线段树】

    题目链接:BZOJ - 1218 题目分析 可以覆盖一个边长为 R 的正方形,但是不能包括边界,所以等价于一个边长为 R - 1 的正方形. 坐标范围 <= 5000 ,直接 n^2 的二维前缀 ...

  9. hdu4419 Colourful Rectangle 12年杭州网络赛 扫描线+线段树

    题意:给定n个矩形,每个矩形有一种颜色,RGB中的一种.相交的部分可能为RG,RB,GB,RGB,问这n个矩形覆盖的面积中,7种颜色的面积分别为多少 思路:把x轴离散化做扫描线,线段树维护一个扫描区间 ...

随机推荐

  1. JS ~ 返回上一步

      <a href=" javascript:window.history.back() "> 返回上一步 </a>

  2. python字符串常用函数

    # 索引与切片  *** capitalize()  **首字母大写 upper() lower() *** 大写和小写函数 startswith endswith  ***    判断以‘’字母’开 ...

  3. LeetCode | No.1 两数之和

    题目描述: Given an array of integers, return indices of the two numbers such that they add up to a speci ...

  4. eclipse导入maven工程,右键没有build path和工程不能自动编译解决方法

    原文链接:https://blog.csdn.net/wusunshine/article/details/52506389 eclipse导入maven工程,右键没有build path解决方法: ...

  5. HZNU-ACM寒假集训Day4小结 最短路

    最短路 1.Floy 复杂度O(N3)  适用于任何图(不存在负环) 模板 --kuangbin #include<iostream> #include<cstdio> #in ...

  6. python try catch 打印traceback

    1. import traceback try: print(AB) except Exception, e: traceback.print_exc()

  7. VUE.js入门学习(1)-起步

    1.hello world <div id="app">{{content}}</div>var app = new Vue({ el:'#app', da ...

  8. 了解redis

    redis:非关系型数据库,基于内存高性能,key-value存储,一般用作缓存,开源的使用ANSI C语言编写,遵守BSD协议,支持网络,可基于内存亦可持久化的日志型.Key-Value数据库,并提 ...

  9. 题解 Luogu P5434: 有标号荒漠计数

    妈妈我终于会这道题了! 设\(n\)个点的有根仙人掌个数的指数型生成函数(EGF)为\(F(x)\), 令\(f_i = [x^n]F(x)\) 对于\(f_i\), 我们考虑钦点\(1\)号点为根, ...

  10. LVS DR模式搭建、keepalived+LVS搭建介绍

    参考文献 http://blog.51cto.com/taoxie/2066993 疑问: 1.为什么要修改RealServer的返回arp响应和发送arp请求参数  echo "1&quo ...