题意:那个城市里要竞选市长,然后在一块墙上可以贴海报为自己拉票,每个人可以贴连续的一块区域,后来帖的可以覆盖前面的,问到最后一共可以看到多少张海报。
思路:一看就知道是线段树,只是说要利用到离散化,也不是那么的难,当然注意,有的离散化错误也ac了......当然,在最后没必要还一个个去询问是否覆盖,直接开个标记数组,询问到一个区间只有一个覆盖值,然后进行标记就可以。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define M 100005
struct node
{
int ll,rr;
int num;
int cnt;
}tree[8*M];
int s[4*M][2],t[4*M];
int xx[4*M];
int ans=0,flag=0;
void creat(int i,int l,int r)
{
tree[i].ll=l;
tree[i].rr=r;
tree[i].cnt=0;
tree[i].num=0;
int mid=(l+r)/2;
if(l==r)
{
//tree[i].num=0;
return;
}
creat(i*2,l,mid);
creat(i*2+1,mid+1,r);
}
void updata(int i,int left,int right,int k)
{
if(tree[i].ll==left&&tree[i].rr==right)
{
tree[i].cnt=1;
tree[i].num=k;
//if(k==2)
//printf("%d %d\n",left,right);
return;
}
if(tree[i].cnt==1)
{
tree[i*2].cnt=1;
tree[i*2+1].cnt=1;
tree[i*2].num=tree[i].num;
tree[i*2+1].num=tree[i].num;
tree[i].cnt=2;
tree[i].num=0;
}
int mid=(tree[i].ll+tree[i].rr)/2;
if(mid>=right) updata(i*2,left,right,k);
else if(mid<left) updata(i*2+1,left,right,k);
else
{
updata(i*2,left,mid,k);
updata(i*2+1,mid+1,right,k);
}
if(tree[i*2].cnt==1&&tree[i*2+1].cnt==1&&tree[i*2].num==tree[i*2+1].num)
{
tree[i].cnt=1;
tree[i].num=tree[i*2].num;
} else tree[i].cnt=2;
//printf("updata\n");
}
void quest(int i)
{
//if(k==1)
//printf("%d %d %d %d %d\n",tree[i].ll,tree[i].rr,tree[i].cnt,tree[i].num,k);
if(tree[i].cnt==1)
{
xx[tree[i].num]=1;
return;
}
if(tree[i].ll==tree[i].rr)
return;
//if(flag==1)
//return;
if(tree[i].cnt==0)
return;
quest(i*2);
quest(i*2+1); // printf("quest\n");
}
int erfen(int ll,int rr,int num)
{
while(ll<=rr)
{
int mid=(ll+rr)/2;
if(t[mid]>num)
rr=mid-1;
else
ll=mid+1;
// printf("erfen\n");
}
return rr;
}
int main()
{
int text;
scanf("%d",&text);
while(text--)
{
int n;
scanf("%d",&n);
//creat(1,1,3*n);
int cnt=0;
for(int i=0;i<n;i++)
{
scanf("%d%d",&s[i][0],&s[i][1]);
t[cnt++]=s[i][0];
t[cnt++]=s[i][1];
}
sort(t,t+cnt);
int cnt1=1; for(int i=1;i<cnt;i++)
if(t[i]!=t[i-1]) t[cnt1++]=t[i]; for(int i=cnt1-1;i>0;i--)
if(t[i]!=t[i-1]+1) t[cnt1++]=t[i-1]+1;
sort(t,t+cnt1);
for(int i=cnt1;i>0;i--)
t[i]=t[i-1];
creat(1,1,cnt1+5);
for(int i=1;i<=n;i++)
{
int ll=1,rr=cnt1;
int tmp=erfen(ll,rr,s[i-1][0]);
int tmp1=erfen(ll,rr,s[i-1][1]);
//tmp;
//tmp1;
//printf("%d %d\n",tmp,tmp1);
updata(1,tmp,tmp1,i);
}
ans=0;
memset(xx,0,sizeof(xx));
quest(1);
for(int i=1;i<=n;i++)
if(xx[i]==1)
ans++;
printf("%d\n",ans);
}
return 0;
}

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

  1. poj2528(线段树区间替换&离散化)

    题目链接: http://poj.org/problem?id=2528 题意: 第一行输入一个 t 表 t 组输入, 对于每组输入: 第一行  n 表接下来有 n 行形如 l, r 的输入, 表在区 ...

  2. POJ2528:Mayor's posters(线段树区间更新+离散化)

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

  3. poj2528 Mayor's posters(线段树区间修改+特殊离散化)

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

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

    http://poj.org/problem?id=2528 https://www.luogu.org/problem/UVA10587 Description The citizens of By ...

  5. POJ-2528 Mayor's posters (线段树区间更新+离散化)

    题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...

  6. poj-2528线段树练习

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

  7. POJ 2482 Stars in Your Window (线段树区间合并+扫描线)

    这题开始一直被矩形框束缚了,想法一直都是枚举线,但是这样枚举都需要O(n^2)...但是看了别人的思路,感觉这题思想真心很好(PS:开头好浪漫的描述啊,可惜并没有什么用)  题意就是在平面上给你一些星 ...

  8. HDU3577 线段树(区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3577 ,普通的线段树区间更新题目,较简单. 相当于一个区间覆盖问题,有一点要注意的就是叶子节点是一个长 ...

  9. POJ 2823 Sliding Window 线段树区间求和问题

    题目链接 线段树区间求和问题,维护一个最大值一个最小值即可,线段树要用C++交才能过. 注意这道题不是求三个数的最大值最小值,是求k个的. 本题数据量较大,不能用N建树,用n建树. 还有一种做法是单调 ...

随机推荐

  1. c运行库、c标准库、windows API的区别和联系

    C运行时库函数C运行时库函数是指C语言本身支持的一些基本函数,通常是汇编直接实现的.  API函数API函数是操作系统为方便用户设计应用程序而提供的实现特定功能的函数,API函数也是C语言的函数实现的 ...

  2. HDU 2067 小兔的棋盘 (卡特兰数)

    小兔的棋盘 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  3. 下载远程(第三方服务器)文件、图片,保存到本地(服务器)的方法、保存抓取远程文件、图片 将图片的二进制字节字符串在HTML页面以图片形式输出 asp.net 文件 操作方法

    下载远程(第三方服务器)文件.图片,保存到本地(服务器)的方法.保存抓取远程文件.图片   将一台服务器的文件.图片,保存(下载)到另外一台服务器进行保存的方法: 1 #region 图片下载 2 3 ...

  4. WPF:“wpf类库项目改为Window应用程序项目”系列问题

    一.wpf类库项目改为Window应用程序项目1.错误 CS5001 Program does not contain a static 'Main' method suitable for an e ...

  5. linux 文件系统 xfs、ext4、ext3 的区别

    前言 centos7.0开始默认文件系统是xfs,centos6是ext4,centos5是ext3 ext3介绍 ext3和ext4的最大区别在于,ext3在fsck时需要耗费大量时间(文件越多,时 ...

  6. 全相FFT

    作者:桂. 时间:2017-12-02  23:29:48 链接:http://www.cnblogs.com/xingshansi/p/7956491.html 一.相位提取 以正弦信号为例,x = ...

  7. 在Windows系统上怎么使用SecureCRT链接Linux AWS EC2 -摘自网络

    在Windows系统上就需要使用SecureCRT,Putty等工具,进行连接.但是AWS提供的XXX.pem文件,需要做一些处理SecureCRT的方法: 1.使用XXX.pem文件生成一个公钥文件 ...

  8. Flume 相关

    在CentOS 7上安装配置Flume https://mos.meituan.com/library/41/how-to-install-flume-on-centos7/ Flume NG 学习笔 ...

  9. linux 安装 Headless Chrome

    http://blog.csdn.net/goodzyw/article/details/77269875 https://chromedriver.storage.googleapis.com/in ...

  10. MySql(十七):MySql架构设计——高可用设计之思路及方案

    前言: 数据库系统是一个应用系统的核心部分,要想系统整体可用性得到保证,数据库系统就不能出现任何问题.对于一个企业级的系统来说,数据库系统的可用性尤为重要.数据库系统一旦出现问题无法提供服务,所有系统 ...