题目链接:https://vjudge.net/problem/POJ-2528

题意:在区间[1,1e7]内染色,依次染n(<=1e4)中颜色,给出每种颜色染色的范围,可重叠,求最终有多少种颜色。

思路:继续肝线段树。。被这题虐了一下午加一晚上QAQ。

   首先要想到离散化,因为区间为1e7,直接做的话时间空间都做不到。但因为是区间,这里不能简单的离散化,比如1、5离散化为1、2是有问题的,必须离散化为1,3,5; 还记得要先去重。

   其次是线段树,这道题是典型的线段树的题,直接对点操作的话复杂度很大,所以用线段树完成对区间的操作。线段树结点包括l(区间左端点)、r(区间右端点)、value(为0表示该区间没有颜色或有多种颜色,为>0表示对应的颜色编号)。

   最后进行一次询问即可,仅当区间的value>0时再判断颜色,并且用hash数组记录颜色i是否出现过。

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=; struct node{
int l,r,value;
}tr[*maxn]; int a[maxn][],b[*maxn],T,n,hash[maxn],ans; void build(int v,int l, int r){
tr[v].l=l,tr[v].r=r,tr[v].value=;
if(l==r) return;
int mid=(l+r)>>;
build(v<<,l,mid);
build(v<<|,mid+,r);
} void pushdown(int v){
tr[v<<].value=tr[v].value;
tr[v<<|].value=tr[v].value;
tr[v].value=;
} void update(int v,int l,int r,int k){
if(l<=tr[v].l&&r>=tr[v].r){
tr[v].value=k;
return;
}
if(tr[v].value) pushdown(v);
int mid=(tr[v].l+tr[v].r)>>;
if(l<=mid) update(v<<,l,r,k);
if(r>mid) update(v<<|,l,r,k);
} void query(int v){
if(tr[v].value){
if(!hash[tr[v].value]){
++ans;
hash[tr[v].value]=;
}
return;
}
if(tr[v].l==tr[v].r) return;
query(v<<);
query(v<<|);
} int main(){
scanf("%d",&T);
while(T--){
memset(hash,,sizeof(hash));
ans=;
scanf("%d",&n);
int cnt1=;
for(int i=;i<=n;++i){
scanf("%d%d",&a[i][],&a[i][]);
b[++cnt1]=a[i][];
b[++cnt1]=a[i][];
}
sort(b+,b+cnt1+);
int cnt2=;
for(int i=;i<=cnt1;++i)
if(b[i]!=b[i-]) b[++cnt2]=b[i];
int t=cnt2;
for(int i=;i<=t;++i)
if(b[i]-b[i-]>) b[++cnt2]=b[i-]+;
sort(b+,b+cnt2+);
build(,,cnt2);
for(int i=;i<=n;++i){
int x=lower_bound(b+,b+cnt2+,a[i][])-b;
int y=lower_bound(b+,b+cnt2+,a[i][])-b;
update(,x,y,i);
}
query();
printf("%d\n",ans);
}
return ;
}

poj2528(线段树+离散化)的更多相关文章

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

    2016-08-15 题意:一面墙,往上面贴海报,后面贴的可以覆盖前面贴的.问最后能看见几种海报. 思路:可以理解成往墙上涂颜色,最后能看见几种颜色(下面就是以涂色来讲的).这面墙长度为1~1000 ...

  2. poj2528 线段树+离散化 (倒序)

    The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign h ...

  3. poj2528 线段树+离散化

    由于坐标可能很大,此时需要离散化,将值转化为对应的坐标. #include<stdio.h> #include<algorithm> using namespace std; ...

  4. POJ2528 线段树离散化

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

  5. [poj2528] Mayor's posters (线段树+离散化)

    线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...

  6. poj-2528线段树练习

    title: poj-2528线段树练习 date: 2018-10-13 13:45:09 tags: acm 刷题 categories: ACM-线段树 概述 这道题坑了我好久啊啊啊啊,,,, ...

  7. POJ 2528 Mayor's posters(线段树+离散化)

    Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...

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

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

  9. [UESTC1059]秋实大哥与小朋友(线段树, 离散化)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1059 普通线段树+离散化,关键是……离散化后建树和查询都要按照基本法!!!RE了不知道多少次………………我真 ...

  10. poj 2528 Mayor's posters 线段树+离散化技巧

    poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...

随机推荐

  1. 三、thymeleaf模板引擎构建前台html, 后台使用 ModelAndView 和 Model 模型

    项目源码:https://github.com/y369q369/springBoot.git      ->     thymeleaf 私聊QQ: 1486866853 1.pom.xml中 ...

  2. windows短路径转换成长路径

    参考: https://blog.csdn.net/wxqian25/article/details/43951281 https://docs.microsoft.com/en-us/windows ...

  3. [转]AJAX POST请求中参数以form data和request payload形式在servlet中的获取方式

    转载至 http://blog.csdn.net/mhmyqn/article/details/25561535 最近在写接收第三方的json数据, 因为对java不熟悉,有时候能通过request能 ...

  4. mysql创建表及插入数据操作步骤及注意要点

    环境:mysql workbench 1.创建新的表,注意,指定要存放的数据库 列名可以加单引号(键盘上1左边的引号),也可以不加 2.插入数据 注意:如果数据是字符型,必须使用单引号或者双引号,如: ...

  5. Vue ElementUI 按需引入

    一.element UI组件的单独使用(第一种方法): 1.cnpm install babel-plugin-component -D         2.找到.babelrc 配置文件       ...

  6. Linq to SQL -- Union All、Union、Intersect和Top、Bottom和Paging和SqlMethods

    Union All/Union/Intersect操作 适用场景:对两个集合的处理,例如追加.合并.取相同项.相交项等等. Concat(连接) 说明:连接不同的集合,不会自动过滤相同项:延迟. 1. ...

  7. 彻底解决windows10+matlab2018a调用libsvm时出现找不到编译器问题

    本文转载自:Shane Zhao博客(CSDN) https://blog.csdn.net/silence2015/article/details/53106156 个人申明,只是因为解决这个问题花 ...

  8. JavaScript数组方法--filter、find、findIndex

    继续数组方法,今天应该到filter了. filter:filter() 方法创建一个新数组, 其包含通过所提供函数实现的测试的所有元素. 使用: var words = ['spray', 'lim ...

  9. java生成简单验证码图片

    概要 最近项目需要用java实现输出随机验证码图片到前台,正好有机会接触下java的绘图类,完成需求后也有时间做个总结,写篇随笔记录下也希望能帮助到有同样需求的人! 需求流程图 1.生成随机数 在ja ...

  10. 自定义Windows右击菜单调用Winform程序

    U9_Git中ignore文件处理 背景 U9代码中有许多自动生成的文件,不需要上传Git必须BE Entity中的.target文件 .bak 文件 Enum.cs结尾的文件,还有许多 extand ...