poj 2528 线段树 离散化的小技巧
题意:在墙上贴海报,海报可以互相覆盖,问最后可以看见几张海报
思路:直接搞超时+超内存,需要离散化。
离散化简单的来说就是只取我们需要的值来
用,比如说区间[1000,2000],[1990,2012]
我们用不到[-∞,999][1001,1989][1991,1999][2001,2011][2013,+∞]这些值,所以我只需要
1000,1990,2000,2012就够了,将其分别映射到0,1,2,3,在于复杂度就大大的降下来了
所以离散化要保存所有需要用到的值,排序后,分别映射到1~n,这样复杂度就会小很多很多
而这题的难点在于每个数字其实表示的是一个单位长度(并且一个点),这样普通的离散化会造成许多错误
给出下面两个简单的例子应该能体现普通离散化的缺陷:
1-10 1-4 5-10
1-10 1-4 6-10
为了解决这种缺陷,我们可以在排序后的数组上加些处理,比如说[1,2,6,10]
如果相邻数字间距大于1的话,在其中加上任意一个数字,比如加成[1,2,3,6,7,10],然后再做线段树就好了.
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
#define clc(a,b) memset(a,b,sizeof(a))
//#include <bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn=;
int vis[maxn<<];
int ans=;
int x[maxn];
int hashh[maxn<<];
struct node
{
int l,r;
} q[maxn]; void pushdown(int rt)
{
if(vis[rt]!=-)
{
vis[rt<<]=vis[rt<<|]=vis[rt];
vis[rt]=-;
}
} void update(int L,int R,int c,int l,int r,int rt)
{
if(L<=l&&R>=r)
{
vis[rt]=c;
return;
}
pushdown(rt);
int m=(l+r)>>;
if(L<=m) update(L,R,c,l,m,rt<<);
if(R>m) update(L,R,c,m+,r,rt<<|);
} void query(int l,int r,int rt)
{
if(vis[rt]!=-)
{
if(!hashh[vis[rt]])
ans++;
hashh[vis[rt]]=;
return;
}
if(l==r)
return;
int m=(l+r)>>;
query(l,m,rt<<);
query(m+,r,rt<<|);
} int main()
{
int n,t;
scanf("%d",&t);
while(t--)
{
int cnt=;
scanf("%d",&n);
for(int i=; 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=;
for(int i=; i<cnt; i++)
{
if(x[i]!=x[i-])
x[m++]=x[i];
}
for(int i=m-; i>=; i--)
if(x[i]!=x[i-]+)
x[m++]=x[i-]+;
sort(x,x+m);
clc(vis,-);
for(int i=; 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,,m,);
}
clc(hashh,);
ans=;
query(,m,);
printf("%d\n",ans);
}
return ;
}
poj 2528 线段树 离散化的小技巧的更多相关文章
- poj 2528(线段树+离散化) 市长的海报
http://poj.org/problem?id=2528 题目大意是市长竞选要贴海报,给出墙的长度和依次张贴的海报的长度区间(参考题目给的图),问最后你能看见的海报有几张 就是有的先贴的海报可能会 ...
- POJ 2528 (线段树 离散化) Mayor's posters
离散化其实就是把所有端点放在一起,然后排序去个重就好了. 比如说去重以后的端点个数为m,那这m个点就构成m-1个小区间.然后给这m-1个小区间编号1~m-1,再用线段树来做就行了. 具体思路是,从最后 ...
- Mayor's posters POJ - 2528(线段树 + 离散化)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 74745 Accepted: 21574 ...
- poj 2528 线段树+离散化
题意:在墙上贴一堆海报(只看横坐标,可以抽象成一线段),新海报可以覆盖旧海报.求最后能看到多少张海报 sol:线段树成段更新.铺第i张海报的时候更新sg[i].x~sg[i].y这一段为i. 然而坐标 ...
- poj 2528 线段树区间修改+离散化
Mayor's posters POJ 2528 传送门 线段树区间修改加离散化 #include <cstdio> #include <iostream> #include ...
- Mayor's posters POJ - 2528 线段树(离散化处理大数?)
题意:输入t组数据,输入n代表有n块广告牌,按照顺序贴上去,输入左边和右边到达的地方,问贴完以后还有多少块广告牌可以看到(因为有的被完全覆盖了). 思路:很明显就是线段树更改区间,不过这个区间的跨度有 ...
- Picture POJ - 1177 线段树+离散化+扫描线 求交叉图像周长
参考 https://www.cnblogs.com/null00/archive/2012/04/22/2464876.html #include <stdio.h> #include ...
- Mayor's posters POJ - 2528 线段树区间覆盖
//线段树区间覆盖 #include<cstdio> #include<cstring> #include<iostream> #include<algori ...
- POJ 2528 线段树
坑: 这道题的坐标轴跟普通的坐标轴是不一样的-- 此题的坐标轴 标号是在中间的-- 线段树建树的时候就不用[l,mid][mid,r]了(这样是错的) 直接[l,mid][mid+1,r]就OK了 D ...
随机推荐
- R语言的一些笔记
(1)包中函数必须在NAMESPACE中进行标记导出,否则就不认识了: 例如叫做rtest.Model.LogisticreRression 就能识别,而叫做Model.LogisticreRress ...
- puppet 部署 horizon server 所需的参数和部署逻辑
所需要的参数: $secret_key, $bind_address = '127.0.0.1', $cache_server_ip = '127.0.0.1', $cache_ser ...
- SQL sum case when then else【转】
数据库 t 表 b 表内容 Id Name 胜负 1 张三 胜 2 李四 ...
- Educational Codeforces Round 8 D. Magic Numbers
Magic Numbers 题意:给定长度不超过2000的a,b;问有多少个x(a<=x<=b)使得x的偶数位为d,奇数位不为d;且要是m的倍数,结果mod 1e9+7; 直接数位DP;前 ...
- codeforces edu round3
B. The Best Gift 传送门:http://codeforces.com/problemset/problem/609/B Emily's birthday is next week a ...
- 敏捷开发的特点(转自MBAlib)
敏捷开发的特点 敏捷方法主要有两个特点,这也是其区别于其他方法,尤其是重型方法的最主要特征: (1)敏捷开发方法是“适应性”(Adaptive)而非“预设性” (Predictive). 这里说的预设 ...
- 自适应单本小说网站源码,基于bootstrap+dedecms。
具体效果:http://www.ishengxu.cc/ 基于bootstrap+dedecms,PC端与手机端自适应,广告位也都设计好了,很简单.
- php多线程thread开发与应用的例子
Php多线程的使用,首先需要PHP5.3以上版本,并安装pthreads PHP扩展,可以使PHP真正的支持多线程,扩展如何安装请自行百度 PHP扩展下载:https://github.com/kra ...
- Codeforces Round #232 (Div. 1)
这次运气比较好,做出两题.本来是冲着第3题可以cdq分治做的,却没想出来,明天再想好了. A. On Number of Decompositions into Multipliers 题意:n个数a ...
- 【Xamarin挖墙脚系列:现有IPhone/IPad 设备尺寸】
原文:[Xamarin挖墙脚系列:现有IPhone/IPad 设备尺寸]