这个题的想法很好想,就是进行排序之后直接检查每个点的上下左右是否有黑点就行.但是直接枚举显然不行,那怎么办呢?我们就用树状数组维护扫描线,把每排左右点看成一条线覆盖,然后从下往上扫,遇到下加一,遇到上减一并记录答案.这样用扫描线维护就行了.

题干:

Description
无限大正方形网格里有n个黑色的顶点,所有其他顶点都是白色的(网格的顶点即坐标为整数的点,又称整点)。每秒钟,所有内部白点同时变黑,直到不存在内部白点为止。你的任务是统计最后网格中的黑点个数。 内部白点的定义:一个白色的整点P(x,y)是内部白点当且仅当P在水平线的左边和右边各至少有一个黑点(即存在x1 < x < x2使得(x1,y)和(x2,y)都是黑点),且在竖直线的上边和下边各至少有一个黑点(即存在y1 < y < y2使得(x,y1)和(x,y2)都是黑点)。
Input
输入第一行包含一个整数n,即初始黑点个数。以下n行每行包含两个整数(x,y),即一个黑点的坐标。没有两个黑点的坐标相同,坐标的绝对值均不超过109。
Output
输出仅一行,包含黑点的最终数目。如果变色过程永不终止,输出-。
Sample Input -
-
Sample Output 数据范围
%的数据满足:n < =
%的数据满足:n < =
%的数据满足:n < =

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
#define duke(i,a,n) for(int i = a;i <= n;i++)
#define lv(i,a,n) for(int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
const int INF = << ;
typedef long long ll;
typedef double db;
template <class T>
void read(T &x)
{
char c;
bool op = ;
while(c = getchar(), c < '' || c > '')
if(c == '-') op = ;
x = c - '';
while(c = getchar(), c >= '' && c <= '')
x = x * + c - '';
if(op) x = -x;
}
template <class T>
void write(T x)
{
if(x < ) putchar('-'), x = -x;
if(x >= ) write(x / );
putchar('' + x % );
}
struct point
{
int x,y;
}a[];
struct seg
{
int k,x,y,r;
}s[];
int n;
int hsh[],cnt = ;
int tr[],ans = ;
bool cmp1(point a,point b)
{
if(a.x == b.x)
return a.y < b.y;
else
return a.x < b.x;
}
bool cmp2(point a,point b)
{
if(a.y == b.y)
return a.x < b.x;
else
return a.y < b.y;
}
bool cmp3(seg a,seg b)
{
if(a.y == b.y)
return a.k < b.k;
else
return a.y < b.y;
}
int find(int x)
{
int l = ,r = n,mid;
while(l <= r)
{
int mid = (l + r) >> ;
if(hsh[mid] < x)
l = mid + ;
else if(hsh[mid] > x)
r = mid - ;
else return mid;
}
}
void insert(int k,int l,int r,int t)
{
if(!k)
{
s[++cnt].x = find(l);
s[cnt].r = find(r);
s[cnt].y = t;
}
else
{
s[++cnt].x = find(t);
s[cnt].y = l;
s[cnt].k = ;
s[++cnt].x = find(t);
s[cnt].y = r;
s[cnt].k = -;
}
}
int lowbit(int x)
{
return x & -x;
}
void build()
{
sort(a + ,a + n + ,cmp1);
duke(i,,n)
{
if(a[i].x == a[i - ].x)
insert(,a[i - ].y,a[i].y,a[i].x);
}
sort(a + ,a + n + ,cmp2);
duke(i,,n)
{
if(a[i].y == a[i - ].y)
insert(,a[i - ].x,a[i].x,a[i].y);
}
}
void update(int x,int y)
{
while(x <= n)
{
tr[x] += y;
x += lowbit(x);
}
}
int ask(int x)
{
int s = ;
while(x)
{
s += tr[x];
x -= lowbit(x);
}
return s;
}
void work()
{
duke(i,,cnt)
{
if(!s[i].k)
ans += ask(s[i].r - ) - ask(s[i].x);
else
update(s[i].x,s[i].k);
}
}
int main()
{
read(n);
duke(i,,n)
{
read(a[i].x);
read(a[i].y);
hsh[i] = a[i].x;
}
sort(hsh + ,hsh + n + );
build();
sort(s + ,s + cnt + ,cmp3);
work();
printf("%d\n",ans + n);
return ;
}
/*
4
0 2
2 0
-2 0
0 -2
*/

最新文章

  1. dataTables获取当前行json格式数据
  2. solr suggest智能提示配置
  3. 【JAVA】Runtime
  4. C# 中的 Static
  5. jQuery 元素的选中, 置顶、上移、下移、置底、删除
  6. C++混合编程之idlcpp教程Python篇(2)
  7. DICOM:Ubuntu14环境下安装dcm4chee+oviyam2.1
  8. poj2631 树的直径 + bfs
  9. 软件测试之WEB测试经典总结
  10. 函数textread
  11. DBNull.value
  12. Spring MVC 基本注解
  13. wiki leaks file link url
  14. jQuery的deferred对象使用详解
  15. 13. Roman to Integer (JAVA)
  16. ECS服务器搭建Discuz 邮箱设置,报错处理
  17. [SCOI2014]方伯伯的商场之旅
  18. 【C/C++】C语言嵌入式编程修炼&#183;背景篇&#183;软件架构篇&#183;内存操作篇
  19. angular自定义指令 repeat 循环结束事件;limitTo限制循环长度、限定开始位置
  20. 【转】input file accept属性可以限制的文件类型

热门文章

  1. RabbitMQ系列(三)--Java API
  2. 如何快速的vue init 属于自己的vue模板?
  3. JAVA基础——链表结构之单链表
  4. 剑指offer---圆圈中最后剩下的数
  5. mysql (5.7版本)---的配置
  6. APUE 文件IO
  7. idea 获取当前git最新分支
  8. 【Codeforces 992B】Nastya Studies Informatics
  9. 20181012关于mysql内部执行流程
  10. 函数式语言(functional language)定义、函数式语言的种类以及为什么函数式语言会流行起来的学习笔记