题意:那个城市里要竞选市长,然后在一块墙上可以贴海报为自己拉票,每个人可以贴连续的一块区域,后来帖的可以覆盖前面的,问到最后一共可以看到多少张海报。
思路:一看就知道是线段树,只是说要利用到离散化,也不是那么的难,当然注意,有的离散化错误也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. google开发新人入职100天,聊聊自己的经验&教训 个人对编程和开发的理解 技术发展路线

    新人入职100天,聊聊自己的经验&教训 这篇文章讲了什么? 如题,本屌入职100天之后的经验和教训,具体包含: 对开发的一点感悟. 对如何提问的一点见解. 对Google开发流程的吐槽. 如果 ...

  2. 微信app支付

    http://www.bubuko.com/infodetail-1062014.html 要完成手机APP跳转到微信的APP进行微信支付,需要进行如下操作: 1.先去微信的开放平台(http://o ...

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

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

  4. Twitter Bootstrap 中文帮助文档

    http://wrongwaycn.github.io/bootstrap/docs/index.htmlTwitter Bootstrap 中文帮助文档 翻译得很不错~~~ 但是,还是要看英文文档 ...

  5. 编码 GBK 的不可映射字符

    一般做项目公司都会统一要求文件编码类型,很多为了实现应用国际化和本地化和更高的性能,而选用UTF-8而非GBK. 但在开发过程中我们都用的是IDE,只要更改了配置就不用操心了,但有时我们也会用命令行来 ...

  6. mongoDB 32位 安装包地址

    https://www.mongodb.org/dl/win32/i386 http://downloads.mongodb.org/win32/mongodb-win32-i386-3.2.4-si ...

  7. MySQL和ORACLE、SQL Server、PostgreSQL相比

  8. Java 8 – Convert a Stream to LIST

    Java 8 – Convert a Stream to LIST package com.mkyong.java8; import java.util.List;import java.util.s ...

  9. java 中获得 资源文件方法

    1 java 中获取资源文件的方法 项目目录如下 获取当前项目的目录路径 方法一:使用类名 MergeDocHandler.class.getClassLoader().getResource(&qu ...

  10. Jacobi并行拆解

    作者:桂. 时间:2018-04-23  21:12:02 链接:http://www.cnblogs.com/xingshansi/p/8921815.html 前言 本文主要是复数矩阵分解的拆解思 ...