Mayor's posters

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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

题目大意:
大体是说有人要竞选需要投票什么的,不用理会,总之后面就是贴海报, 后面的能把前面的海报覆盖,海报每次贴不会到方格中间,
如果一个海报不是完全被贴上就算可以看到,问最后你能看到几张海报(英语渣,反正我是这么理解的) 输入
1 //T个样例
5 //下面5(n)张海报及其范围 1 <= n <= 10000
1 4 //海报的左右边界 1--10000000
2 6
8 10
3 4
7 10 输出
4 能看到4张海报 解题思路:
由于海报粘贴的方式,在外面的绝对不会被里面的遮住,所以倒着来覆盖海报比较方便
主要还是由于海报边界太大需要离散化,如果把海报的每个边界看成一个节点话最多需要的范围就成了20000 节点是80000
不过我不会离散化第一次看了课件,模仿着写的,不过听学长说貌似不算很好的离散化 或者完全不合格的离散化
我用的这种离散化还是要开一个100000000的一维数组 虽然开的下,这道题也能解决,不过学长说如果范围是几亿的话就没办法了
不过这道题还是能过的,新的离散化方法明天再搞吧 下面是代码
/*毕竟我是王宇浩*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cstdlib>
#include <cmath>
#include <cctype>
#define Max(a,b) ((a)>(b)?(a):(b))
#define ls rt<<1
#define rs rt<<1|1
using namespace std;
const int N = ;
int xx[N];//x[i]用来储存海报左右边,排列后节点的位置为i;
int post[N * ];//post[i]用来储存边i应该排列在第几个位置上面
struct Poster
{
int l, r;
}P[N];//P[i]来储存第i张海报的左右边 struct node
{
int l, r;
bool cover;
int Mid()
{
return (l + r) >> ;
} }a[N<<]; int cmp(const void *a, const void *b)
{
int *c, *d;
c = (int *)a;
d = (int *)b;
return *c - *d;
} void Build(int rt, int l, int r)
{
a[rt].l = l, a[rt].r = r, a[rt].cover = false; if(l == r) return ; Build(ls, l, a[rt].Mid());
Build(rs, a[rt].Mid()+, r);
}
void UP(int rt)
{
//如果ls和rs都被覆盖, 则此区域被覆盖
if(a[rt].l == a[rt].r) return;
if(a[ls].cover && a[rs].cover)
a[rt].cover = true;
}
bool Search(int rt, int l, int r)//如果找到此区间未被覆盖,覆盖并返回true, 否则返回false;
{
bool ans;
if(a[rt].cover) return false;
if(a[rt].l == l && a[rt].r == r) {
if(a[rt].cover) return false;
else {
a[rt].cover = true;
return true;
}
}
if(l > a[rt].Mid()) {
ans = Search(rs, l, r);
}
else if(r <= a[rt].Mid()) {
ans = Search(ls, l, r);
}
else {
bool ans1 = Search(ls, l, a[rt].Mid());
bool ans2 = Search(rs, a[rt].Mid() + , r);
ans = ans1 || ans2;
}
UP(rt);//每次放置海报之后就要向上更新
return ans;
} int main()
{
int T, n;
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
int index = ;
for(int i = ; i < n; i++) {
scanf("%d %d", &P[i].l, &P[i].r);
xx[index++] = P[i].l;
xx[index++] = P[i].r;
}
qsort(xx, index, sizeof(xx[]), cmp);//对每个左右边的大小排序,确定其属于的节点位置
int index2 = ;
for(int i = ; i < index; i++) {//每个边的位置确定一下,相同值为一个节点,相邻节点也相邻,不相邻节点之间空一个节点
post[xx[i]] = index2;
if(i == index - ) break;//判断到最后一个,结束循环,因为每次循环保存的都是下个节点的状态,及时结束防止数组越界
if(xx[i + ] - xx[i] == ) index2++;
if(xx[i + ] - xx[i] > ) index2 += ;
}
Build(, , index2);//建立线段树
int ans = ;//保存答案
for(int i = n - ; i >= ; i--) {
if(Search(, post[P[i].l], post[P[i].r])) ans++;//每次贴一张海报,判断会不会被覆盖,不会则答案+1
}
printf("%d\n", ans);
}
}
/*
1
5
1 4
2 6
8 10
3 4
7 10 */

POJ 2528 Mayor's posters的更多相关文章

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

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

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

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

  3. POJ - 2528 Mayor's posters(dfs+分治)

    POJ - 2528 Mayor's posters 思路:分治思想. 代码: #include<iostream> #include<cstdio> #include< ...

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

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

  5. POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】

    任意门:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  6. POJ 2528 Mayor's posters(线段树区间染色+离散化或倒序更新)

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

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

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

  8. POJ 2528——Mayor's posters——————【线段树区间替换、找存在的不同区间】

    Mayor's posters Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

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

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

随机推荐

  1. windows重建图标缓存(解决快捷方式图标丢失,图标加载时间长问题)

    新建一个文本文档,把下边的代码输入进去,保存为.bat格式,运行即可 有快捷方式图标丢失或者觉得图标加载速度慢了,就run一下这个,很实用的小工具 rem 关闭Windows外壳程序explorer ...

  2. selenium启动Chrome浏览器和禁止证书错误提示弹出

    要把ChromeDriver放到代码中的文件夹中c://*******Application public static WebDriver WebDriverRun(WebDriver driver ...

  3. 伪共享和缓存行填充,从Java 6, Java 7 到Java 8

    关于伪共享的文章已经很多了,对于多线程编程来说,特别是多线程处理列表和数组的时候,要非常注意伪共享的问题.否则不仅无法发挥多线程的优势,还可能比单线程性能还差.随着JAVA版本的更新,再各个版本上减少 ...

  4. 求System.arraycopy的用法

    public class Shuzufuzhi { public static void main(String args[]) {  int myArray[]={1,2,3,4,5,6};  in ...

  5. python之socket

    一.初识socket      socket 是网络连接端点,每个socket都被绑定到一个特定的IP地址和端口.IP地址是一个由4个数组成的序列,这4个数均是范围 0~255中的值(例如,     ...

  6. ubuntu host备份 ubuntu无法解析主机

    /etc/hosts # Copyright (c) 2014-2016, racaljk.# https://github.com/racaljk/hosts# Last updated: 2016 ...

  7. 自定义样式 实现文件控件input[type='file']

    一般我们设计的上传按钮都是和整个页面风格相似的样式,不会使用html原生态的上传按钮,但是怎么既自定义自己的样式,又能使用file控件功能呢? 思路是这样的: 1.定义一个相对定位的DIV,按照整成步 ...

  8. ajax请求总是进入Error里

    ajax请求时找到了正确的地址,执行完返回总是进入error里,并且浏览器错误显示是找不到请求的地址. 解决办法: 查看配置文件的,把maxJsonLength值改大. <system.web. ...

  9. Unity3d外包(北京)公司(长年承接U3D外包)

    我们制作各类型严肃游戏,虚拟现实,增强现实项目! 品质保证,售后完备. 联系请加QQ:372900288  电话:13911652504 我们团队成立于2011年10月,是一个专业从事严肃游戏研发的团 ...

  10. Deep Learning 26:读论文“Maxout Networks”——ICML 2013

    论文Maxout Networks实际上非常简单,只是发现一种新的激活函数(叫maxout)而已,跟relu有点类似,relu使用的max(x,0)是对每个通道的特征图的每一个单元执行的与0比较最大化 ...