线段树+扫描线 NAIPC 2019 Intersecting Rectangles
你看看你有多菜,一点线段树的小小的运用,就不会写了;
题意:如果矩阵有交集,输出1,否则输出0(不包含内嵌);
思路:本题求交集,还得不包括内嵌的情况;
做过一道是求面积的题。跟这道类似,但在这里定义的方式跟那道题定的相反。
这里把下面的线定为了-1,上面定为了1;
在这道题里,先把矩阵的横向边按上下两种储存在一个结构体里,上的权值为1,下的为-1;
然后离散化这些坐标(为浮点数的时候和数太大的时候都要离散化,太大的话线段树放不下)
所以用离散化后的x1,x2去更新线段树;
当更新的边为下边的时候(权值为-1) 如果这个时候这个区间里已经有值了,证明相交;
当更新的边为上边的时候(权值为1) 如果这个时候这个区间里的值没有变为0,也证明相交;
#include<cstdio>
#include<algorithm>
#include<math.h>
#include<string.h>
using namespace std;
const int maxn=2e5+;
int cnt1;
int cnt2,c[maxn];
struct node
{
int l,r,h,id;
}b[maxn];
struct Node
{
int l,r,sum;
}tree[maxn<<];
void init()
{
memset(c,,sizeof(c));
cnt1=cnt2=;
}
bool cmp(node x,node y)
{
return x.h<y.h;
}
void build(int l,int r,int root)
{
tree[root].l=l,tree[root].r=r;
tree[root].sum=;
if(l==r) return;
int mid=l+r>>;
build(l,mid,root<<);
build(mid+,r,root<<|);
}
void pushup(int root)
{
tree[root].sum=tree[root<<].sum+tree[root<<|].sum;
}
void update(int base,int key,int root)
{
int l=tree[root].l,r=tree[root].r;
if(l==r){
tree[root].sum+=key;
return;
}
int mid=l+r>>;
if(mid>=base) update(base,key,root<<);
else update(base,key,root<<|);
pushup(root);
}
int query(int L,int R,int root)
{
int l=tree[root].l,r=tree[root].r;
if(l>=L&&r<=R){
return tree[root].sum;
}
int mid=l+r>>;
int ans=;
if(mid>=L) ans+=query(L,R,root<<);
if(mid<R) ans+=query(L,R,root<<|);
return ans;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF){
init();
for(int i=;i<=n;i++){
int x1,x2,y1,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
b[++cnt1].l=x1,b[cnt1].r=x2,b[cnt1].h=y1,b[cnt1].id=-; //下边;
b[++cnt1].l=x1,b[cnt1].r=x2,b[cnt1].h=y2,b[cnt1].id=; //上边;
c[++cnt2]=x1; //离散化坐标的预处理;
c[++cnt2]=x2; //离散化坐标的预处理;
}
sort(c+,c++cnt2); //离散化
int len=unique(c+,c++cnt2)-c-;
for(int i=;i<=cnt1;i++){
b[i].l=lower_bound(c+,c++len,b[i].l)-c; //将离散化后的值存储起来
b[i].r=lower_bound(c+,c++len,b[i].r)-c;
}
sort(b+,b++cnt1,cmp); //从小到排序,这里的操作弄不太清楚,只知道这样子之后
//才能一步一步的去判断是否相交;
build(,cnt1,); //建树;
int flag=;
for(int i=;i<=cnt1;i++){
if(b[i].id==-){ //如果为下边
int tmp=query(b[i].l,b[i].r,);
if(tmp){
flag=;
break;
}
}
update(b[i].l,b[i].id,);
update(b[i].r,b[i].id,);
if(b[i].id==){
int tmp=query(b[i].l,b[i].r,);
if(tmp){
flag=;
break;
}
}
}
printf("%d\n",flag);
}
return ;
}
线段树+扫描线 NAIPC 2019 Intersecting Rectangles的更多相关文章
- hdu 1828 线段树扫描线(周长)
Picture Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- hdu1542 Atlantis (线段树+扫描线+离散化)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HDU 1542 - Atlantis - [线段树+扫描线]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- HDU 1264 Counting Squares (线段树-扫描线-矩形面积并)
版权声明:欢迎关注我的博客.本文为博主[炒饭君]原创文章,未经博主同意不得转载 https://blog.csdn.net/a1061747415/article/details/25471349 P ...
- 【Codeforces720D】Slalom 线段树 + 扫描线 (优化DP)
D. Slalom time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...
- Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)
题目链接:http://codeforces.com/contest/522/problem/D 题目大意: 给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...
- 【POJ-2482】Stars in your window 线段树 + 扫描线
Stars in Your Window Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11706 Accepted: ...
- HDU 4419 Colourful Rectangle --离散化+线段树扫描线
题意: 有三种颜色的矩形n个,不同颜色的矩形重叠会生成不同的颜色,总共有R,G,B,RG,RB,GB,RGB 7种颜色,问7种颜色每种颜色的面积. 解法: 很容易想到线段树扫描线求矩形面积并,但是如何 ...
- BZOJ-3228 棋盘控制 线段树+扫描线+鬼畜毒瘤
3228: [Sdoi2008]棋盘控制 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 23 Solved: 9 [Submit][Status][D ...
随机推荐
- [Python]pyhon去除txt文件重复行 python 2020.2.10
代码如下: import shutil readPath='E:/word4.txt' #要处理的文件 writePath='E:/word5.txt' #要写入的文件 lines_seen=set( ...
- Networking POJ - 1287 最小生成树板子题
#include<iostream> #include<algorithm> using namespace std; const int N=1e5; struct edge ...
- [CQOI2009] 中位数 - 桶
给出 \(1~n\) 的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是 \(b\).中位数是指把所有元素从小到大排列后,位于中间的数. Solution (这个题为什么会被打上数学标签? ...
- Oracle - 拼接多个字段 - wm_concat()函数
Oracle wm_concat()函数oracle wm_concat(column)函数使我们经常会使用到的,下面就教您如何使用oraclewm_concat(column)函数实现字段合并如:s ...
- springboot中使用kaptcha验证码
maven依赖 <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptc ...
- python3练习100题——011
原题链接:http://www.runoob.com/python/python-exercise-example11.html 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔 ...
- Shell的 for 循环小例子
<1> 上例子 for i in f1 f2 f3; do @echo $i; done 执行结果: f1 f2 f3 但是,请注意:如果是在makefile 中写,要写成这个样子: al ...
- Java对象构成所有Java应用程序的基础
通过在优锐课的ange交流下,掌握了很多编程思想方法 特来分享 对象具有状态和行为 Java中的对象以及其他任何``面向对象''语言都是所有Java应用程序的基本组成部分,代表了你可能在你周围找到的任 ...
- sql语句代码规范
19年年底的时候领导一直强调代码规范化以前写代码的时候很随意后来越来越看自己写的代码难受逐渐的也像规范化走去,今天又学了一招记录分享一下 这张图就是以前写代码的时候正常情况很是杂乱无章 这张就是规范话 ...
- 如何强制文件上传git仓库里
1.首先在本地创建一个文件夹里写入一个txt文件 2.然后复制你仓库的地址 3.平时正常操作都是这样上传不上 4.没关系我们使用强制上传 代码是 git push -f “你的仓库地址” mast ...