题目传送门: 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运维、架构之路-SSH远程管理服务

    一.SSH服务功能介绍 1.远程登录管理 提供类似telnet远程联机服务器的服务,即上面提到的SSH服务 2.远程传输文件 是类似FTP服务的sftp-server,借助SSH协议来传输数据的,提供 ...

  2. JavaScript三元运算符以及运算符顺序

    三目运算符(三元运算符) 三目运算符:运算符需要三个操作 语法:表达式1?表达式2:表达式3 表达式1是一个条件,值为Boolean类型 若表达式1的值为true,则执行表达式2的操作,并且以表达式2 ...

  3. Go实现分布式外部排序

    Go实现分布式外部排序 项目路径: https://github.com/Draymonders/go_external_sort 默认读入文件: small.in 默认输出文件:small.out ...

  4. Java 内存屏障

    内存屏障(Memory Barrier,或有时叫做内存栅栏,Memory Fence)是一种CPU指令,用于控制特定条件下的重排序和内存可见性问题.Java编译器也会根据内存屏障的规则禁止重排序. 内 ...

  5. Leetcode 9. Palindrome Number(水)

    9. Palindrome Number Easy Determine whether an integer is a palindrome. An integer is a palindrome w ...

  6. Python_019(六星级别之反射方法)

    1.反射 1)神赐给你的内置函数 : a: getattr(命名空间,'函数名') == 命名空间.属性名; 这里的命名空间指的是对象或者类; b: getattr四个应用场景: 1)类名.名字 &l ...

  7. 【CF686D】Kay and Snowflake(树的重心)

    题意:给定一棵n个点的树,q次询问,每次询问以某个点为根的子树编号是多少 n,q<=3e5 思路:设sz[u]为以u为根子树的size,v为u的size最大的儿子 若sz[v]*2<sz[ ...

  8. Software-Defined Networking: A Comprehensive Survey

    文章名称:Software-Defined Networking: A Comprehensive Survey 文章来源:Proceedings of the IEEE ( Volume: 103  ...

  9. .NET(c#) 移动APP开发平台 - Smobiler(2) - 平台介绍

    看到大家很多人在后台问我一些问题,所以准备写一个系列了,下面给个目录 目录: .NET(c#) 移动APP开发平台 - Smobiler(1) 环境的搭建及上手第一个应用 类似开发WinForm的方式 ...

  10. nvm 管理 node 版本

    nvm 有 Mac 版本 num 亦有 windows 版本(可以搜索 nvm for windows) 安装后 运行 nvm v 可查看版本 运行 nvm install latest 安装最新版本 ...