题目传送门: POJ-2528

题意就是在一个高度固定的墙面上贴高度相同宽度不同的海报,问贴到最后还能看到几张?本质上是线段树区间更新问题,但是要注意的是题中所给数据范围庞大,直接搞肯定会搞出问题,所以要离散化,而离散化的过程中要注意一个问题,比方说1-10,1-5,6-10,本来是可以三张海报都可以看见的,但是按照题意来看是看不到的,因为他是一个点代表一个单位长度(诡异>_<),解决的办法则是对于距离大于1的两相邻点,中间再插入一个点......

代码如下,我用了vector,和用数组的相差大改50ms左右,如果时间不是特别紧的话,完全可以用vector,还可以借助STL来节省时间(懒???>_<)......

#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdio> using namespace std;
#define lson l, m, rt << 1
#define rson m+1, r, rt << 1 | 1 const int N = + ;
int col[N<<], li[N], ri[N],Hash[N], cnt;
vector<int> x; void PushDown(int rt){
if(col[rt] != -){
col[rt<<] = col[rt<<|] = col[rt];
col[rt] = -;
}
} void Updata(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) >> ;
if(L <= m) Updata(L, R, c, lson);
if(R > m) Updata(L, R, c, rson);
} void Query(int l,int r,int rt) {
if (col[rt] != -) {
if (!Hash[col[rt]]) cnt ++;
Hash[col[rt]] = true;
return ;
}
if (l == r) return ;
int m = (l + r) >> ;
Query(lson);
Query(rson);
} int Tran(int pos){
return distance(x.begin(), lower_bound(x.begin(), x.end(), pos)); //这个地方要注意, x.begin()要放前面, 不然出来的都是负值
} int Disc(vector<int> &x){ //vector离散化
sort(x.begin(), x.end());
x.erase(unique(x.begin(), x.end()), x.end());
for(int i = x.size() - ; i > ; i--)
if(x[i] != x[i-] + ) x.push_back(x[i]+);
sort(x.begin(), x.end());
return x.size();
} /**
int Disc(int cur, int X[]){ //数组离散化
sort(X, X + cur);
int m = 1;
for(int i = 1; i < cur; i++){
if(X[i]!=X[i-1]) X[m++] = X[i];
}
for(int i = m-1; i > 0; i--){
if(X[i] != X[i-1] + 1) X[m++] = X[i] + 1;
}
sort(X, X + m);
return m;
}
*/ void Init_work(){
memset(col, -, sizeof(col));
memset(Hash, false, sizeof(Hash));
cnt = ;
} int main(){
int T,n;
scanf("%d", &T);
while(T--){
Init_work();
scanf("%d", &n);
for(int i = ; i < n; i++){
scanf("%d %d", &li[i], &ri[i]);
x.push_back(li[i]);
x.push_back(ri[i]);
}
int m = Disc( x ); for(int i = ; i < n; i++){
Updata(Tran(li[i]), Tran(ri[i]), i, , m, );
} Query(, m, );
printf("%d\n",cnt);
}
}


POJ-2528 Mayor's posters (离散化, 线段树)的更多相关文章

  1. poj 2528 Mayor's posters(线段树+离散化)

    /* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...

  2. 【POJ】2528 Mayor's posters ——离散化+线段树

    Mayor's posters Time Limit: 1000MS    Memory Limit: 65536K   Description The citizens of Bytetown, A ...

  3. POJ 2528 Mayor's posters(线段树区间染色+离散化或倒序更新)

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 59239   Accepted: 17157 ...

  4. POJ 2528 Mayor's posters(线段树/区间更新 离散化)

    题目链接: 传送门 Mayor's posters Time Limit: 1000MS     Memory Limit: 65536K Description The citizens of By ...

  5. POJ 2528——Mayor's posters——————【线段树区间替换、找存在的不同区间】

    Mayor's posters Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  6. POJ 2528 ——Mayor's posters(线段树+区间操作)

    Time limit 1000 ms Memory limit 65536 kB Description The citizens of Bytetown, AB, could not stand t ...

  7. (中等) POJ 2528 Mayor's posters , 离散+线段树。

    Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...

  8. POJ 2528 Mayor's posters(线段树染色问题+离散化)

    http://poj.org/problem?id=2528 题意: 给出一面无限长的墙,现在往墙上依次贴海报,问最后还能看见多少张海报. 题意:这道题目就相当于对x轴染色,然后计算出最后还能看见多少 ...

  9. POJ 2528 Mayor's posters(线段树)

    点我看题目 题意 :建一堵墙粘贴海报,每个候选人只能贴一张海报,海报的高度与墙一样高,一张海报的宽度是整数个单位,墙被划分为若干个部分,每个部分的宽度为一个单位,每张海报完全的覆盖一段连续的墙体,墙体 ...

  10. POJ 2528 - Mayor's posters - [离散化+区间修改线段树]

    题目链接:http://poj.org/problem?id=2528 Time Limit: 1000MS Memory Limit: 65536K Description The citizens ...

随机推荐

  1. Linux为程序员添加了行为准则

    假如你是开发人员,如果您密切关注Linux开发,您就会知道Linux内核讨论会非常热烈.最近,LinusTorvalds承认Linux内核邮件列表(LKML)和其他Linux开发空间对许多人都是敌对的 ...

  2. 【leetcode】1106. Parsing A Boolean Expression

    题目如下: Return the result of evaluating a given boolean expression, represented as a string. An expres ...

  3. 用设计模式来替代if-else

    前言 物流行业中,通常会涉及到EDI报文(XML格式文件)传输和回执接收,每发送一份EDI报文,后续都会收到与之关联的回执(标识该数据在第三方系统中的流转状态).这里枚举几种回执类型:MT1101.M ...

  4. CSS背景和CSS3背景background属性

    css背景属性用于定义HTML元素的背景 背景属性既可以为单个的单元设置背景,也可以为整个页面设置背景,可以对上述二者的任意组合设置背景,段落.文字.不同状态的链接.图像.内容区域修改其背景样式.设置 ...

  5. 代理修饰词weak/assign/strong的区别

    基于项目报错: WebViewJavascriptBridgeBase 中定义:@property (assign) id <WebViewJavascriptBridgeBaseDelegat ...

  6. Comet OJ - Contest #7 D 机器学习题 斜率优化 + 未调完

    Code: #include <cstdio> #include <algorithm> #include <cstring> #define setIO(s) f ...

  7. 【gym102222K】Vertex Covers(高维前缀和,meet in the middle)

    题意:给定一张n点m边的图,点带点权,定义点覆盖的权值为点权之积,问所有点覆盖的权值之和膜q n<=36, 1<=a[i]<=1e9,1e8<=q<=1e9 思路:n&l ...

  8. UVALive 6858 Frame (模拟)

    Frame 题目链接: http://acm.hust.edu.cn/vjudge/contest/130303#problem/D Description http://7xjob4.com1.z0 ...

  9. 关于VS连接Oracle数据库提示:“尝试加载oracle客户端时引发badimage,如果在安装 32 位 Oracle 客户端组件的情况下以 64 位模式运行,将出现此问题”的解决方案。

    错误一.关于VS连接Oracle数据库提示:“尝试加载oracle客户端时引发badimage,如果在安装 32 位 Oracle 客户端组件的情况下以 64 位模式运行,将出现此问题”的解决方案. ...

  10. ODB Examples

    http://www.codesynthesis.com/products/odb/examples.xhtml The following list gives an overview of the ...