POJ 3470 Walls(线段树+扫描线)
【题目链接】 http://poj.org/problem?id=3470
【题目大意】
给出几面墙,均垂直于x轴或者y轴,给出一些鸟的位置(二维坐标点),
鸟只会垂直x轴或者y轴飞行,并且会撞上最近的墙,问每面墙最后会有几只鸟撞上去。
【题解】
我们将所有的二维坐标离散,对xy方向分别进行扫描线,
以y轴方向为例,我们先从y最小的线开始扫,
如果是墙,那么在线段树中更新其在x轴上的分布位置
如果是鸟的坐标,那么在线段树中查找其位置,更新其答案。
之后从y最大的开始反向扫描,更新,x方向也同理。
【代码】
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <climits>
using namespace std;
const int N=50010;
int n,m,wall,tot,dis[N],v[N],w[N],T[10*N],*arr;
int x[3*N],y[3*N],ry[3*N],rx[3*N],r[3*N],xs[3*N],ys[3*N],px[3*N],py[3*N];
bool cmp(int a,int b){return arr[a]<arr[b];}
void compress(int *x,int *r,int n,int *a,int *mp){
for(int i=0;i<n;i++)r[i]=i;
arr=x; sort(r,r+n,cmp);
int m=-1; a[r[0]]=++m; mp[m]=x[r[0]];
for(int i=1;i<n;i++){
int cur=r[i],lst=r[i-1];
if(x[cur]==x[lst])a[cur]=m;
else a[cur]=++m,mp[m]=x[cur];
}
}
int query(int q,int x,int l,int r){
if(T[x]!=-2)return T[x];
int mid=(l+r)>>1;
if(q<mid)return query(q,x<<1|1,l,mid);
return query(q,x+1<<1,mid,r);
}
void update(int a,int b,int x,int l,int r,int val){
if(r<=a||b<=l)return;
else if(a<=l&&r<=b)T[x]=val;
else{
if(T[x]!=-2){
T[x+1<<1]=T[x<<1|1]=T[x];
T[x]=-2;
}int mid=(l+r)>>1;
update(a,b,x<<1|1,l,mid,val);
update(a,b,x+1<<1,mid,r,val);
}
}
void scan(int k,int *ys,int *xs,int *py,int W){
if(k<wall){
int _k=k^1;
if(xs[_k]>=xs[k])update(xs[k],xs[_k]+1,0,0,W,k/2);
}else{
int t=query(xs[k],0,0,W);
if(~t){
int d=min(abs(py[ys[k]]-py[ys[t*2]]),abs(py[ys[k]]-py[ys[t*2+1]]));
k-=wall; if(d<dis[k])dis[k]=d,v[k]=t;
}
}
}
void fly(int *ry,int *ys,int *xs,int *py,int W){
T[0]=-1; for(int i=0;i<tot;i++)scan(ry[i],ys,xs,py,W);
T[0]=-1; for(int i=tot-1;i>=0;i--)scan(ry[i],ys,xs,py,W);
}
int main(){
scanf("%d%d",&n,&m);
wall=2*n; tot=wall+m;
for(int i=0;i<tot;i++)scanf("%d%d",x+i,y+i);
compress(x,rx,tot,xs,px);
compress(y,ry,tot,ys,py);
fill(dis,dis+m,INT_MAX);memset(w,0,n);
fly(ry,ys,xs,py,xs[rx[tot-1]]+1);
fly(rx,xs,ys,px,ys[ry[tot-1]]+1);
for(int i=0;i<m;i++)++w[v[i]];
for(int i=0;i<n;i++)printf("%d\n",w[i]);
return 0;
}
POJ 3470 Walls(线段树+扫描线)的更多相关文章
- POJ 1151 - Atlantis 线段树+扫描线..
离散化: 将所有的x轴坐标存在一个数组里..排序.当进入一条线段时..通过二分的方式确定其左右点对应的离散值... 扫描线..可以看成一根平行于x轴的直线..至y=0开始往上扫..直到扫出最后一条平行 ...
- POJ 1177 Picture(线段树 扫描线 离散化 求矩形并面积)
题目原网址:http://poj.org/problem?id=1177 题目中文翻译: 解题思路: 总体思路: 1.沿X轴离散化建树 2.按Y值从小到大排序平行与X轴的边,然后顺序处理 如果遇到矩形 ...
- hdu 1542&&poj 1151 Atlantis[线段树+扫描线求矩形面积的并]
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- poj 1177 --- Picture(线段树+扫描线 求矩形并的周长)
题目链接 Description A number of rectangular posters, photographs and other pictures of the same shape a ...
- poj 1177 Picture (线段树 扫描线 离散化 矩形周长并)
题目链接 题意:给出n个矩形,每个矩形给左下 和 右上的坐标,求围成的周长的长度. 分析: 首先感谢大神的博客,最近做题经常看大神的博客:http://www.cnblogs.com/kuangbin ...
- Picture POJ - 1177 (线段树-扫描线)
A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wa ...
- 线段树 扫描线 L - Atlantis HDU - 1542 M - City Horizon POJ - 3277 N - Paint the Wall HDU - 1543
学习博客推荐——线段树+扫描线(有关扫描线的理解) 我觉得要注意的几点 1 我的模板线段树的叶子节点存的都是 x[L]~x[L+1] 2 如果没有必要这个lazy 标志是可以不下传的 也就省了一个pu ...
- 【POJ-2482】Stars in your window 线段树 + 扫描线
Stars in Your Window Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11706 Accepted: ...
- 【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],对于每一个查 ...
随机推荐
- unity值得推荐的网址
免费字体下载网站:http://www.dafont.com/ 免费声音文件下载网站:http://freesound.org/ http://incompetech.com/mus ...
- event.returnValue=false与event.preventDefault()
event.preventDefault()方法是用于取消事件的默认行为,但此方法并不被ie支持,在ie下需要用window.event.returnValue = false; 来实现. funct ...
- EK算法模板
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> ...
- ACM ICPC China final G Pandaria
目录 ACM ICPC China final G Pandaria ACM ICPC China final G Pandaria 题意:给一张\(n\)个点\(m\)条边的无向图,\(c[i]\) ...
- margin百分比,重叠和auto
一. margin百分比 1. 普通元素的百分比margin都是相对于容器的宽度计算 2. 绝对定位元素的百分比margin是相对于第一个定位祖先元素(relative/absolute/fixed) ...
- JAVA学习资料大全
最高端的JAVA架构师资源(来自龙果学院 价值¥1399元).JAVA互联网分布式架构(龙果学院 价值¥899元).Spring Boot(2017年最新 包括源码原理分析) + Spring Clo ...
- js实现封装和继承
封装(模拟对象) http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_encapsulation.html 继承 htt ...
- c#字典序
using System; using System.Collections.Generic; public class Example { public static void Main() { / ...
- Linux命令之rhn_check
NAME rhn_check - check for queued actions on RHN and execute them SYNOPSIS /usr/sbin/rhn_check [-v] ...
- Linux下find命令参数及用法详解
由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时间来了解一下.即使系统中含有网络文件系统( NFS),find命令在该文件系统中同样有效,只你具有相应的权限.在运行一个非常 ...