Description

The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for placing the posters and introduce the following rules:

  • Every candidate can place exactly one poster on the wall.
  • All posters are of the same height equal to the height of the wall; the width of a poster can be any integer number of bytes (byte is the unit of length in Bytetown).
  • The wall is divided into segments and the width of each segment is one byte.
  • Each poster must completely cover a contiguous number of wall segments.

They have built a wall 10000000 bytes long (such that there is enough place for all candidates). When the electoral campaign was restarted, the candidates were placing their posters on the wall and their posters differed widely in width. Moreover, the candidates started placing their posters on wall segments already occupied by other posters. Everyone in Bytetown was curious whose posters will be visible (entirely or in part) on the last day before elections.

Your task is to find the number of visible posters when all the posters are placed given the information about posters' size, their place and order of placement on the electoral wall.

Input

The first line of input contains a number c giving the number of cases that follow. The first line of data for a single case contains number 1 <= n <= 10000. The subsequent n lines describe the posters in the order in which they were placed. The i-th line among the n lines contains two integer numbers l
i and ri which are the number of the wall segment occupied by the left end and the right end of the i-th poster, respectively. We know that for each 1 <= i <= n, 1 <= l
i <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered l
i, l
i+1 ,... , ri.

Output

For each input data set print the number of visible posters after all the posters are placed.

The picture below illustrates the case of the sample input.


Sample Input

1
5
1 4
2 6
8 10
3 4
7 10

Sample Output

4
 
题意:给每个区间贴纸,后面贴的会覆盖前面贴的,问最后能看到的纸有几张
思路:很明显的线段树区间更新问题,但是由于数目大,所以我们要考虑离散化,由于之前没有弄过离散化,看了人家的代码,再加上自己的演草纸上模拟过程,终于弄懂了离散的原理。
 
首先,我们将区间存放在一个map数组之中,通过s的结构体,我们可以将每个点,与其相对应的区间序号整理起来,然后排序,为了方便判断,将左区间记为负数
通过S,我们重新将数据存入map数组之中,这次map数组存放的是他们位置的区间,由于n只有10000,所以位置最大也就只有10000位,如果存数值则要10000000的空间,泽阳无论在空间还有时间上都得到了优化
 
例如样例中输入后
   排序:1,2,3,4,6,7,8,10
对应位置:1,2,3,4,5,6,7,8
这样我们可以看到,以位置来建树只需要8个空间,但是如果用数值大小来建树则需要10个空间,在当位置与数字的大小差很大的时候,存放位置能优化更多的时间与空间
而现在,map数组中存放的是
1,4
2,5
7,8
3,4
6,8
可以发现,这些位置所对应的数字便是原本题目给出的数字区间,所以我们最后只要求出这些位置覆盖后有几个可见即可
 
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int n,cnt;
const int maxn = 10000+10; struct node
{
int l,r,n;//n统计颜色
} a[maxn<<2]; struct kode
{
int point,num;//point记录区间的边,num记录位置
} s[maxn<<2]; int map[maxn<<1][2],ans,flag[maxn<<1]; int cmp(kode x,kode y)
{
return x.point<y.point;
} void init(int l,int r,int i)//建树
{
a[i].l = l;
a[i].r = r;
a[i].n = 0;
if(l!=r)
{
int mid = (l+r)>>1;
init(l,mid,2*i);
init(mid+1,r,2*i+1);
}
} void insert(int i,int l,int r,int m)
{
if(a[i].l == l && a[i].r == r)//找到了区间,更新这个区间的颜色
{
a[i].n = m;
return;
}
int mid = (a[i].l+a[i].r)>>1;
if(a[i].n>0)//重点注意,如果这个区间被访问了,并且这个区间有颜色,就要将这个区间的颜色更新到其左右孩子的节点,并且要将这个区间的颜色清空,这样才能算是覆盖
{
a[2*i].n = a[2*i+1].n = a[i].n;
a[i].n = 0;
}
if(l>=a[2*i+1].l)
insert(2*i+1,l,r,m);
else if(r<=a[2*i].r)
insert(2*i,l,r,m);
else
{
insert(2*i,l,mid,m);
insert(2*i+1,mid+1,r,m);
}
} void solve(int i)
{
if(a[i].n)//如果有这个区间有颜色了,马上停止访问并返回,因为下面的无论有没有颜色都是已经被覆盖的了
{
if(!flag[a[i].n])//乳沟有颜色且没被统计过的,就统计一次
{
ans++;
flag[a[i].n] = 1;
}
return;
}
solve(2*i);
solve(2*i+1);
return;
} int main()
{
int t,i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i = 0; i<n; i++)//离散化
{
scanf("%d%d",&map[i][0],&map[i][1]);
s[2*i].point = map[i][0];
s[2*i+1].point = map[i][1];
s[2*i].num = -(i+1);
s[2*i+1].num = i+1;
}
sort(s,s+2*n,cmp);
int tmp = s[0].point,cnt = 1;
for(i = 0; i<2*n; i++)
{
if(tmp != s[i].point)//如果和前面的不同,这迭代加1
{
cnt++;
tmp = s[i].point;
}
if(s[i].num<0)
map[-s[i].num-1][0] = cnt;
else
map[s[i].num-1][1] = cnt;
}
init(1,cnt,1);
for(i = 0; i<n; i++)
insert(1,map[i][0],map[i][1],i+1);
memset(flag,0,sizeof(flag));
ans = 0;
solve(1);
printf("%d\n",ans);
} return 0;
}

POJ2528:Mayor's posters(线段树区间更新+离散化)的更多相关文章

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

    题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...

  2. poj-----(2528)Mayor's posters(线段树区间更新及区间统计+离散化)

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

  3. POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

    POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...

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

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

  5. poj 2528 Mayor's posters 线段树区间更新

    Mayor's posters Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Descript ...

  6. poj2528 Mayor's posters(线段树区间覆盖)

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

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

    题意: 一共有n张海报, 按次序贴在墙上, 后贴的海报可以覆盖先贴的海报, 问一共有多少种海报出现过. 题解: 因为长度最大可以达到1e7, 但是最多只有2e4的区间个数,并且最后只是统计能看见的不同 ...

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

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

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

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

随机推荐

  1. ApplicationContext中getBean详解

    在org.springframework.context包中有一个接口叫 applicationContext applicationContext中有一个getBean方法,此方法继承之BeanFa ...

  2. salt 常用命令整理

    salt 常用命令整理 ***********模块*********** 查看模块列表module salt 'minion' sys.list_modules 查看指定module的function ...

  3. Latex 学习之旅

    学习资料 A simple guide to LaTeX - Step by Step LaTeX WikiBook LaTeX 科技排版 TeXdoc Online (TeX and LaTeX d ...

  4. Ubuntu下环境变量该写进哪个文件里

    Linux中环境变量包括系统级和用户级,系统级的环境变量是每个登录到系统的用户都要读取的系统变量,而用户级的环境变量则是该用户使用系统时加载的环境变量. 所以管理环境变量的文件也分为系统级和用户级的. ...

  5. Hibernate 使用MyEclipse简化开发

    在平时开发中写配置文件比较繁琐,在这里写一下如何使用myEclipse简化开发. 1.打开MyEclipse,创建数据库连接 单机测试连接按钮,如果出现成功建立连接,则连接成功. 然后Finish 2 ...

  6. python3-开发进阶 django-rest framework 中的 版本操作(看源码解说)

    今天我们来说一说rest framework 中的 版本 操作的详解 首先我们先回顾一下 rest framework的流程: 请求进来走view ,然后view调用视图的dispath函数 为了演示 ...

  7. bzoj 1269 bzoj 1507 Splay处理文本信息

    bzoj 1269 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1269 大致思路: 用splay维护整个文本信息,splay树的中序遍历即为 ...

  8. 厄拉多塞筛法和普通方法求素数表(python实现)

    厄拉多赛筛法(sieve of Eratosthenes): 想要得到一个不大于N的数所有素数,可以先找到不超过根号N的所有素数,设2 = p1 < p2 < ......<pk ≤ ...

  9. 如何解决IIS7上传文件大小限制,.NET 上传文件后 找不到目录解决

    IIS7 默认文件上传大小是30M,那么超过30M的文件就无法上传了,那么就需要对IIS的配置文件进行修改. 在实际应用中往往会出现上传文件太大,无法上传的情况,那是因为IIS对上传文件大小有限制,I ...

  10. CentOS 6.9/7通过yum安装指定版本的Tomcat

    说明:通过yum好处其实很多,环境变量不用配置,配置文件放在大家都熟悉的地方,通过rpm -ql xxx可以知道全部文件的地方等等. 一.安装Tomcat(8+) // 下载脚本 # git clon ...