POJ 2528
来源:互联网 发布:开博尔网络机顶盒价格 编辑:程序博客网 时间:2024/06/10 17:15
离散 线段树 涂色
注意 如果 有两个坐标之差大于1则要添加其中间值!!!
详见数据:
1
5
1 4
2 6
8 10
3 4
7 10
附带码
#include<cstdio>#include<cstdlib>#include<iostream>#include<cstring>#include<algorithm>#define N 200010using namespace std;namespace segment_tree{ int tree[N]; bool vis[N]; void init(){ for(int i=0;i<N;i++)tree[i]=-1,vis[i]=1; } void pushdown(int root){//感觉这才是线段树比较精华的一部分,可以减少很多复杂度 tree[root<<1]=tree[root<<1|1]=tree[root]; tree[root]=-1; } void update(int l,int r,int root,int al,int ar,int color){ int mid=(l+r)>>1; if(al<=l&&ar>=r){ tree[root]=color; return ; } if(tree[root]!=-1)pushdown(root); if(al<=mid)update(l,mid,root<<1,al,ar,color); if(ar>mid)update(mid+1,r,root<<1|1,al,ar,color); return ; } int query(int l,int r,int root){ int mid=(l+r)>>1; if(l==r){ if(vis[tree[root]]){ vis[tree[root]]=0; return 1; }return 0; } if(tree[root]!=-1)pushdown(root); return query(l,mid,root<<1)+query(mid+1,r,root<<1|1); }};void read(int &x){ x=0; char c=getchar(); while(c<'0'||c>'9')c=getchar(); while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();}int find(int x,int* begin,int* end){ int n=end-begin,l=0,r=n,mid; while(l<r){ mid=(l+r)>>1; if(x>begin[mid])l=mid+1; else r=mid; } return r+1;}using namespace segment_tree;struct segment{ int l,r;}f[N];int map[N*2],n,test,cnt,ans;int main(){ read(test); while(test--){ read(n);init();cnt=ans=0; for(int i=1;i<=n;i++)read(f[i].l),read(f[i].r),map[cnt++]=f[i].l,map[cnt++]=f[i].r; sort(map,map+cnt); cnt=unique(map,map+cnt)-map; int cnt_=cnt; for(int i=0;i<cnt_-1;i++){ if(map[i]+1<map[i+1])map[cnt++]=map[i]+1; } sort(map,map+cnt);//至此为离散 for(int i=1;i<=n;i++){ update(1,cnt,1,find(f[i].l,map,map+cnt),find(f[i].r,map,map+cnt),i); } printf("%d\n",query(1,cnt,1)); }}
0 0
- POJ 2528
- poj 2528
- POJ 2528
- POJ 2528
- poj 2528
- poj 2528
- poj 2528
- poj 2528
- POJ 2528
- POJ 2528
- poj 2528
- poj-2528
- poj 2528
- poj 2528
- POJ 2528
- poj 2528
- POJ 2528
- POJ 2528
- 多态详解
- windows 服务访问共享目录(smb)
- Spring 控制反转
- BestCoder Round#77
- 机器学习实战中遇到的python问题
- POJ 2528
- hdu4786
- 朴素贝叶斯分类
- 如何在gns3上用路由器模拟PC机和交换机
- mybatis注解
- 三个最简单的排序算法:插入、冒泡、选择
- 第5周项目1—三角形类雏形(3、构造函数)
- 博客是什么东西?写这个有什么要求么?
- poj 3666 Making the Grade 动态规划+滚动数组