Description

小Q最近沉迷于一款新型《打砖块》游戏。在每局游戏中,呈现在屏幕上的是一堵无限大小的墙壁。墙壁上镶嵌着
无数长度为2、宽度为1的砖块。墙壁被分成若干行,每行宽度都为1,相邻两个格子形成一个砖块。相邻两行的砖
块是间隔摆放的。墙壁从下往上行编号递增,从左往右列编号递增。如下图所示:
在游戏的一开始,有n块砖块消失了。如果两块在同一行且相邻的砖块都消失了,那么玩家可以移除它们上方与它
们都相邻的那一个砖块。请写一个程序帮助小Q计算最多可以让多少个位置没有砖块。

Input

第一行包含两个正整数n(1<=n<=100000),表示一开始消失的砖块个数。
接下来n行,每行两个整数x_i,y_i(|x_i|,|y_i|<=10^9)
分别表示每个消失的砖块的位置,即左半部分位于第x_i行第y_i列。

Output

输出一行一个整数,即没有砖块的位置个数的最大值。

从下到上扫描线,用链表维护当前哪些位置可以有砖块,每向上移一行,每个有砖块的连续段最右边的砖块消失,因此可以暴力模拟,维护一个表记录哪些点当前在连续段最右侧,在扫描线移动时删除这些点。由于一开始需要排序,时间复杂度为O(nlogn)。

#include<bits/stdc++.h>
const int N=1e5+,P=;
char ib[N*],*ip=ib;
int _(){
int x=,f=;
while(*ip<)*ip++=='-'?f=-:;
while(*ip>)x=x*+*ip++-;
return x*f;
}
struct pos{
int x,y;
bool operator<(const pos&w)const{return x<w.x;}
}ps[N];
int n;
struct itv{
mutable int l,r;
bool operator<(const itv&w)const{return l<w.l;}
};
int now=-2e9,cnt=;
struct node{
int x;
bool ed,in;
node*pv,*nx;
}h[P];
node*at(int x,bool nw){
int w=unsigned(x)%P;
while(h[w].ed){
if(h[w].x==x)return h+w;
w=(w+)%P;
}
if(nw)return h[w].x=x,h[w].ed=,h+w;
return ;
}
long long ans=;
node*ts[N];
int tp=;
void upd(){
ans+=cnt;
++now;
for(int i=;i<tp;++i){
if(ts[i]->nx||!ts[i]->in)ts[i--]=ts[--tp];
else{
--cnt;
ts[i]->in=;
ts[i]=ts[i]->pv;
if(!ts[i])ts[i--]=ts[--tp];
}
}
for(int i=;i<tp;++i)ts[i]->nx=;
}
void ins(int x,int y){
while(now<x&&cnt)upd();
now=x;
node*p=at(y,);
if(p->in)return;
p->in=;
p->pv=p->nx=;
++cnt;
node*pv=at(y-,),*nx=at(y+,);
if(pv&&pv->in)(p->pv=pv)->nx=p;
if(nx&&nx->in)(p->nx=nx)->pv=p;
if(!p->nx)ts[tp++]=p;
}
int main(){
fread(ib,,sizeof(ib),stdin);
n=_();
for(int i=;i<n;++i){
int x=_(),y=_();
ps[i]=(pos){x,(y-x)/};
}
std::sort(ps,ps+n);
for(int i=;i<n;++i)ins(ps[i].x,ps[i].y);
while(cnt)upd();
printf("%lld\n",ans);
return ;
}

最新文章

  1. Idea创建Maven项目
  2. Java学习注意事项
  3. H5-考试判断题
  4. javascript + jquery函数大全
  5. 论文笔记之:MatchNet: Unifying Feature and Metric Learning for Patch-Based Matching
  6. Matlab实现网络拓补图
  7. python的一些总结1
  8. use of undeclared identifier *** , did you mean ***. in xcode
  9. Java学习----Java程序结构
  10. S5PV210启动过程分析
  11. hdu3656Fire station(DLX重复覆盖 + 二分)
  12. UILabel设置富文本格式显示
  13. 回溯法之求n个集合的幂集
  14. 浅谈JavaScript的面向对象程序设计(二)
  15. webdriver.chrome()禁止加载图片
  16. Html骨架、基本语法
  17. MSCRM中报表开发二:创建基于FetchXML报表
  18. 对比损失(Contrastive Loss)学习【转载】
  19. dom阻止事件冒泡
  20. JAVA-JSP内置对象之request获得封装所有参数值的Map

热门文章

  1. javascript 日常
  2. hdu4289 Control 最大流最小割
  3. SVN :Unable to connect to a repository at URL
  4. 求约束------------------------ do while循环 算法思想
  5. itcast-spring-三大框架整合
  6. Python实例属性限制(__slots__)
  7. 谈谈 SOA
  8. webstorm版本2017.2开发stylus报错
  9. VC++6 调用teststand api的方法
  10. Python应用场景 (转)