poj 2528 - 线段树 - 离散化的小技巧
2016-11-22 12:23
471 查看
转自:http://www.notonlysuccess.com/index.php/segment-tree-complete/
题意:在墙上贴海报,海报可以互相覆盖,问最后可以看见几张海报
思路:这题数据范围很大,直接搞超时+超内存,需要离散化:
离散化简单的来说就是只取我们需要的值来用,比如说区间[1000,2000],[1990,2012] 我们用不到[-∞,999][1001,1989][1991,1999][2001,2011][2013,+∞]这些值,所以我只需要1000,1990,2000,2012就够了,将其分别映射到0,1,2,3,在于复杂度就大大的降下来了
所以离散化要保存所有需要用到的值,排序后,分别映射到1~n,这样复杂度就会小很多很多
而这题的难点在于每个数字其实表示的是一个单位长度(并且一个点),这样普通的离散化会造成许多错误(包括我以前的代码,poj这题数据奇弱)
给出下面两个简单的例子应该能体现普通离散化的缺陷:
1-10 1-4 5-10
1-10 1-4 6-10
为了解决这种缺陷,我们可以在排序后的数组上加些处理,比如说[1,2,6,10]
如果相邻数字间距大于1的话,在其中加上任意一个数字,比如加成[1,2,3,6,7,10],然后再做线段树就好了.
线段树功能:update:成段替换 query:简单hash
题意:在墙上贴海报,海报可以互相覆盖,问最后可以看见几张海报
思路:这题数据范围很大,直接搞超时+超内存,需要离散化:
离散化简单的来说就是只取我们需要的值来用,比如说区间[1000,2000],[1990,2012] 我们用不到[-∞,999][1001,1989][1991,1999][2001,2011][2013,+∞]这些值,所以我只需要1000,1990,2000,2012就够了,将其分别映射到0,1,2,3,在于复杂度就大大的降下来了
所以离散化要保存所有需要用到的值,排序后,分别映射到1~n,这样复杂度就会小很多很多
而这题的难点在于每个数字其实表示的是一个单位长度(并且一个点),这样普通的离散化会造成许多错误(包括我以前的代码,poj这题数据奇弱)
给出下面两个简单的例子应该能体现普通离散化的缺陷:
1-10 1-4 5-10
1-10 1-4 6-10
为了解决这种缺陷,我们可以在排序后的数组上加些处理,比如说[1,2,6,10]
如果相邻数字间距大于1的话,在其中加上任意一个数字,比如加成[1,2,3,6,7,10],然后再做线段树就好了.
线段树功能:update:成段替换 query:简单hash
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define LL rt<<1 #define RR rt<<1|1 #define lson l,m,LL #define rson m+1,r,RR #define bug puts("bugbug"); const int maxn = 11111; bool hash[10010]; int col[maxn<<4]; struct node{ int l,r; }q[10010]; int ans; int x[maxn<<2]; void pushdown(int rt){ if(col[rt]!=-1){ col[LL]=col[RR]=col[rt]; col[rt]=-1; } } void update(int L,int R,int c,int l,int r,int rt){ if(L<=l&&r<=R){ col[rt]=c; return ; } pushdown(rt); int m=(l+r)>>1; if(L<=m) update(L,R,c,lson); if(R>m) update(L,R,c,rson); } void query(int l,int r,int rt){ if(col[rt]!=-1){ if(!hash[col[rt]]) ans++; hash[col[rt]]=true; return; } if(l==r) return; int m=(l+r)>>1; query(lson); query(rson); } int main(){ int n,i,j,t; scanf("%d",&t); while(t--){ int cnt=0; scanf("%d",&n); for(i=0;i<n;i++){ scanf("%d%d",&q[i].l,&q[i].r); x[cnt++]=q[i].l;x[cnt++]=q[i].r; } sort(x,x+cnt); int m=1; for(i=1;i<cnt;i++) if(x[i]!=x[i-1]) x[m++]=x[i]; for(i=m-1;i>=1;i--) if(x[i]!=x[i-1]+1) x[m++]=x[i-1]+1; sort(x,x+m); memset(col,-1,sizeof(col)); for(i=0;i<n;i++){ int l=lower_bound(x,x+m,q[i].l)-x; int r=lower_bound(x,x+m,q[i].r)-x; update(l,r,i,0,m,1); } memset(hash,false,sizeof(hash)); ans=0; query(0,m,1); printf("%d\n",ans); } return 0; }
相关文章推荐
- poj 2528 线段树 主要是离散化的小技巧
- POJ 2528 Mayor's posters(离散化+线段树)
- poj 2528 数的离散化+线段树
- poj-2528(线段树+离散化)
- POJ - 2528 Mayor's posters(线段树+离散化)
- poj 2528 Mayor's posters(扫描线+堆维护||离散化+线段树)
- POJ 2528 Mayor's posters(离散化+区间set线段树)
- poj 2528 Mayor's posters(线段树+离散化)
- POJ_2528 Mayor's posters(线段树+离散化)
- POJ 2528 Mayor's posters // 线段树 区间更新 离散化
- poj 2528 (线段树+离散化)
- poj 2528 Mayor's posters【离散化+线段树】
- POJ 2528(线段树,区间覆盖,离散化思想)
- poj-----(2528)Mayor's posters(线段树区间更新及区间统计+离散化)
- POJ 2528 Mayor's posters 线段树的区间覆盖 离散化
- POJ 2528-Mayor's posters(线段树+离散化)
- POJ 2528:Mayor's posters(线段树区间更新+离散化)
- POJ 2528 Mayor's posters - 线段树区间更新+离散化
- POJ 2528 Mayor's posters(线段树+离散化)
- poj 2528 Mayor's posters(线段树 离散化 区间更新 贴海报)