Walk in the Park

题目连接:

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2222

Descriptionww.co

You are responsible for inspecting the trees located in a park, to make sure they remain healthy. The location of each tree is given to you as a point in the twodimensional plane, distinct from that of every other tree. Due to recentlyreplanted grass, you are only allowed to walk through the park along a collection of paths. Each path is described by an infinite-length horizontal or vertical line in the two-dimensional plane. No tree lies on any path.

You are concerned that it may not be possible to view all the trees in the park from the paths. In particular, a tree is visible only if you can view it by standing on some path while facing in a direction perpendicular to that path; there must be no intervening tree that obstructs your view. Given the geometrical configuration of the park, please report the number of visible trees.

Input

There will be multiple input sets. For each input set, the first line will contain two integers, N and M , ( 0 < N, M$ \le$100000 ), separated by a space. N is the number of trees, and M is the number of paths.

The next N lines each contain two space-separated integers, X and Y , specifying the coordinates of a tree. X and Y may be any 32-bit integers.

The next M lines each describe a path (a vertical or horizontal line). They have the form x = K or y = K , with no spaces. K may be any 32-bit integer. x and y will be lower case.

End of the input is signified by a line with two space-separated 0's.

Output

For each input set, print a single line containing one integer, specifying the number of visible trees. There should be no blank lines between outputs.

Sample Input

6 3

-1 3

4 2

6 2

6 3

6 4

4 3

x=0

y=-1

y=5

1 2

2 3

x=5

y=5

0 0

Sample Output

5

1

Hint

题意

在二维平面上有n棵树,然后有m条道路

你能看见这棵树的定义是,这棵树存在一条到垂直于道路的线上没有任何其他的树

然后问你最多能看到多少棵树

题解:

扫描线

我们分成四个步骤跑就好了

我们从x轴正半轴看过去能看到多少棵,负半轴看过去,能看到多少,从y轴正半轴看过去,负半轴看过去

每次我们用set来记录一下vis就好了

直接暴力扫一遍

代码

#include<bits/stdc++.h>
using namespace std; const int maxn = 100005;
inline long long read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,m;
struct node
{
int x,y;
int flag;
int id;
};
node p[maxn];
node l[maxn];
set<int> S;
set<int> Ans;
bool cmp1(node a,node b)
{
return a.y<b.y;
}
bool cmp2(node a,node b)
{
return a.y>b.y;
}
bool cmp3(node a,node b)
{
return a.x<b.x;
}
bool cmp4(node a,node b)
{
return a.x>b.x;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0&&m==0)break;
memset(p,0,sizeof(p));
memset(l,0,sizeof(l));
for(int i=1;i<=n;i++)
{
int x,y;
scanf("%d%d",&p[i].x,&p[i].y);
p[i].flag = 0;
p[i].id=i;
}
S.clear();
Ans.clear();
getchar();
for(int i=1;i<=m;i++)
{
char c = getchar();
getchar();
int p = read();
if(c=='x')
{
l[i].x=p;
l[i].flag=2;
}
else
{
l[i].y=p;
l[i].flag=1;
}
} vector<node> V;
int first; first = 1;
S.clear();
V.clear();
for(int i=1;i<=n;i++)
V.push_back(p[i]);
for(int i=1;i<=n;i++)
if(l[i].flag==1)
V.push_back(l[i]);
sort(V.begin(),V.end(),cmp1);
for(int i=0;i<V.size();i++)
{
if(V[i].flag==1)
{
S.clear();
first = 0;
}
else
{
if(first==1)continue;
if(S.count(V[i].x))
continue;
S.insert(V[i].x);
Ans.insert(V[i].id);
}
} S.clear();
V.clear();
first = 1;
for(int i=1;i<=n;i++)
V.push_back(p[i]);
for(int i=1;i<=n;i++)
if(l[i].flag==1)
V.push_back(l[i]);
sort(V.begin(),V.end(),cmp2);
for(int i=0;i<V.size();i++)
{
if(V[i].flag==1)
{
S.clear();
first = 0;
}
else
{
if(first==1)continue;
if(S.count(V[i].x))
continue;
S.insert(V[i].x);
Ans.insert(V[i].id);
}
} S.clear();
V.clear();
first = 1;
for(int i=1;i<=n;i++)
V.push_back(p[i]);
for(int i=1;i<=n;i++)
if(l[i].flag==2)
V.push_back(l[i]);
sort(V.begin(),V.end(),cmp3);
for(int i=0;i<V.size();i++)
{
if(V[i].flag==2)
{
first = 0;
S.clear();
}
else
{
if(first==1)continue;
if(S.count(V[i].y))
continue;
S.insert(V[i].y);
Ans.insert(V[i].id);
}
} first = 1;
S.clear();
V.clear();
for(int i=1;i<=n;i++)
V.push_back(p[i]);
for(int i=1;i<=n;i++)
if(l[i].flag==2)
V.push_back(l[i]);
sort(V.begin(),V.end(),cmp4);
for(int i=0;i<V.size();i++)
{
if(V[i].flag==2)
{
first=0;
S.clear();
}
else
{
if(first==1)continue;
if(S.count(V[i].y))
continue;
S.insert(V[i].y);
Ans.insert(V[i].id);
}
} printf("%d\n",Ans.size());
}
}

UVALive 4221 Walk in the Park 扫描线的更多相关文章

  1. hdu 4444 Walk (离散化+建图+bfs+三维判重 好题)

    Walk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submi ...

  2. Codeforces 209 C. Trails and Glades

    Vasya went for a walk in the park. The park has n glades, numbered from 1 to n. There are m trails b ...

  3. CodeForces 209C Trails and Glades

    C. Trails and Glades time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  4. DICOM开源库

    转载于 http://blog.csdn.net/jackmacro/article/details/5850142 Developers used to search for libraries , ...

  5. in on at 总结

    in,on,at的时间用法和地点用法 一.in, on, at的时间用法 ①固定短语: in the morning/afternoon/evening在早晨/下午/傍晚, at noon/night ...

  6. 数据库 —— 使用JDBC操作数据库

    [Link] http://www.developer.com/java/data/manipulating-a-database-with-jdbc.html Manipulating a Data ...

  7. CF Zepto Code Rush 2014 B. Om Nom and Spiders

    Om Nom and Spiders time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  8. How to Avoid Producing Legacy Code at the Speed of Typing

    英语不好翻译很烂.英语好的去看原文. About the Author I am a software architect/developer/programmer.I have a rather p ...

  9. [转]How to build a data storage and VM Server using comodity hardware and free software

    Source: http://learnandremember.blogspot.jp/2010_01_01_archive.html Requisites: 1) RAID protection f ...

随机推荐

  1. 浏览器的DNS缓存

    通过设置hosts文件可以强制指定域名对应的IP,当修改hosts文件,想要浏览器生效,最直接的方法关闭浏览器后重新开启:如果不想重启浏览器,只需要清空浏览器的DNS缓存即可.清空DNS缓存在chro ...

  2. 性能测试指标&说明 [解释的灰常清楚哦!!]

    详见: 浅谈软件性能测试中关键指标的监控与分析 http://www.51testing.com/html/18/n-3549018.html

  3. unity3d Hair real time rendering 真实头发实时渲染(转)

    惊现塞拉酱 算法是Weta Digital根据siggraph2003的论文加以改进,改进之前使用的是Kajiya and Kay’s 模型,它能量不守恒,也就是说不是基于物理的,不准确 电镜下真实头 ...

  4. 黑马程序员——经典C语言程序设计100例

    1.数字排列 2.奖金分配问题 3.已知条件求解整数 4.输入日期判断第几天 5.输入整数进行排序 6.用*号显示字母C的图案 7.显示特殊图案 8.打印九九口诀 9.输出国际象棋棋盘 10.打印楼梯 ...

  5. string中的substr() 和 find() 函数

    string问题中经常遇到在stringA中查找stringB,主要通过substr()跟find()来完成 substr().find().replace() 都可以用一个位置加上一个长读去描述子串 ...

  6. 移动开发必备!15款jQuery Mobile插件

    移动互联网的发展,来自PC端的网页并不能完全自适应移动端页面需求,使得响应式设计体验产生并成为潮流,也正是这样一种需求,促成了jQuery Mobile的流行.jQuery Mobile这样一款基于j ...

  7. 一款APP从设计稿到切图过程全方位揭秘 Mark

    纯干货!一款APP从设计稿到切图过程全方位揭秘   @BAT_LCK:我本身是一名GUI设计师,所以我只站在GUI设计师的角度去把APP从项目启动到切片输出的过程写一写,相当于工作流程的介绍吧.公司不 ...

  8. Kali vmtools

    参考链接:http://www.leavesongs.com/SHARE/kali-vmtools-install.html   安装环境:kali-1.1.0 ---3.18.0-kali1-amd ...

  9. 记录一下JS正则的坑

    JS正则的单行模式有点问题 总之 . 符号匹配换行符号会有问题 暂时的解决方案是 html = html.replace(/[\r\n]/g,""); 附加一个 html = ht ...

  10. SharePoint咨询师之路:设计之前的那些事二:规模

    提示:本系列只是一个学习笔记系列,大部分内容都可以从微软官方网站找到,本人只是按照自己的学习路径来学习和呈现这些知识. 有些内容是自己的经验和积 累,如果有不当之处,请指正. 咨询师更多的时候是解决方 ...