题目:

  有一个无限长的纸带··上面被划分为若干个格子··现在进行N次操作,第i次操作在L到R上擦出曾经写上的数字(如果有),并写上数字i,询问最终可以看到多少个数字

  N小于10^6

题解:

  首先毫无疑问离散化,但注意离散化时候如果相邻两个数的差大于1··需要在中间插入一个在两数之间的数···不然会错··

  然后正常的方法可以线段树来搞··然而可能被卡常··

  最快的方法同时也是最巧的方法:并查集,我们将询问倒着来··然后用并查集维护每次染色的最右端的位置,具体实现看代码,可以证明为O(n)的复杂度

代码:

  

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
inline int R(){
char c;int f=;
for(c=getchar();c<''||c>'';c=getchar());
for(;c<=''&&c>='';c=getchar()) f=(f<<)+(f<<)+c-'';
return f;
}
const int N=4e6+;
struct node{int l,r;}q[N];
bool visit[N];
int n,b[N*],cnt=,father[N*],ans;
inline int get(int a){
if(father[a]==a) return a;
else return father[a]=get(father[a]);
}
int main()
{
//freopen("a.in","r",stdin);
n=R();
for(int i=,x,y;i<=n;i++) x=R(),y=R(),q[i].l=x+,q[i].r=y,b[++cnt]=x+,b[++cnt]=y;
sort(b+,b+cnt+);cnt=unique(b+,b+cnt+)-b-;int temp=cnt;
for(int i=;i<=cnt;i++)
if(b[i-]+!=b[i]) b[++temp]=b[i-]+;
cnt=temp;sort(b+,b+cnt+);cnt=unique(b+,b+cnt+)-b-;
for(int i=;i<=cnt;i++) father[i]=i;
for(int i=;i<=n;i++) q[i].l=lower_bound(b+,b+cnt+,q[i].l)-b,q[i].r=lower_bound(b+,b+cnt+,q[i].r)-b;
for(int i=n;i>=;i--){
bool flag=false;
for(int pre=,pos=q[i].l;pos<=q[i].r;pos++){
if(pre) father[pre]=pos;
if(!visit[pos]) pre=pos,visit[pos]=true,flag=true;
else pre=pos,pos=get(pos);
}
if(flag) ans++;
}
cout<<ans<<endl;
return ;
}

最新文章

  1. Spring学习(三)
  2. noip2016十连测round2
  3. JAVA创建多线程
  4. 【BZOJ 2693】jzptab
  5. BZOJ 2541: [Ctsc2000]冰原探险
  6. 让IE7 IE8支持CSS3 background-size属性
  7. ffrpc相关文章列表
  8. ie10中元素超出父元素的宽度时不能自动隐藏
  9. Path Sum II 解答
  10. poj 3269 Building A New Barn
  11. Windows Phone 8.1 应用生命周期
  12. MIT 计算机科学及编程导论 Python 笔记 1
  13. Android SQLite 简易指北
  14. Gitpage + hexo(3.0以上)搭建博客
  15. ROS_Kinetic_22 使用ROS的qt插件即ros_qtc_plugin实现Hi ROS!!!!
  16. lua c函数注册器
  17. pdb调试神器使用终极指南
  18. python中的time模块和datetime模块
  19. 中断MSI INTA
  20. web API简介(二):客户端储存之document.cookie API

热门文章

  1. 升级win10后电脑经常自动重启的问题
  2. &lt;转载&gt;一般筛法和快速线性筛法求素数
  3. eclipse中使用git上传项目
  4. python网络-多任务实现之协程(27)
  5. A * B Problem Plus HDU - 1402 (FFT)
  6. 格雷码Gray Code详解
  7. BFS:胜利大逃亡
  8. [原]sencha touch之carousel
  9. Android开发——HandlerThread以及IntentService详解
  10. git使用问题整理