题目链接: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. [ZZ]MTSC 2017 Mobile Testing@Google 演讲的感受

    原文地址: https://testerhome.com/topics/9364 Mobile Testing@Google 其实在开始听谷歌的张南和潘岩开始演讲前,了解下 Google Test C ...

  2. SpringBoot集成Atomikos使用Oracle数据库mybatis、jta框架

    项目中需要数据库分布式事物的实现,于是采用了atumikos技术. 因为生产上需要稳定,所以采用了springboot 1.5.9.RELEASE版本. 本文代码gitlab下载地址: https:/ ...

  3. java时间日期类(Date、DateFormat、Calendar)学习

    1.Date类 常用方法:long getTime(),用于返回当前时刻的毫秒值 Date d = new Date(2000); System.out.println(d.getTime());// ...

  4. word 添加文本框

    转https://blog.csdn.net/sroco/article/details/17044973 如何在word2013(2007.2010)中添加带滚动条的文本框 2013年11月30日 ...

  5. chrome的console功能

    Console的常用用法: 1:显示信息 常用的有 console.log(),console.info(),console.warn(),console.error() 2:占位符 3:查看对象的信 ...

  6. JAVA 中BIO,NIO,AIO的理解 (转)

    转自: http://qindongliang.iteye.com/blog/2018539 另外类似可参考资料 :http://www.360doc.com/content/13/1029/20/9 ...

  7. Chapter07 链表(下):如何轻松学出正确的链表代码?

    技巧一:理解指针或引用的含义 技巧二:指针丢失和内存泄漏 技巧三:利用哨兵简化实现难度 技巧四:重点留意边界条件处理 如果链表未空时,代码是否能够正常运行 如果链表只包含一个节点时,代码能否正常运行 ...

  8. BootStrap table动态增删改表格内数据

    1:添加一个[操作]列   { title: "操作", align: 'center', valign: 'middle', width: 160, // 定义列的宽度,单位为像 ...

  9. 前三次OO作业小结

    I used to be enamored of object-oriented programming. I'm now finding myself leaning toward believin ...

  10. springCloud 服务注册启动报错<com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect>

    报错:com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: ...