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

Mayor‘s posters

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 86160   Accepted: 24734

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 li 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 <= li <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered li, li+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

题意:画N条线段,顺序从先到后,后面画的线段可以覆盖之前画的线段,每种线段颜色不同,最后输出有多少墙上有多少种颜色。

题解:长度建线段树不可行,离散化后处理

离散化:只考虑元素之间的相互关系,比如 1 10000 10000000000,可以映射成1 2 3
离散化操作一般用STL的sort和unique:
数组a[n],b[n](a[n]的副本),先对数组b进行排序,再用unique去重,离散的时候用lower_bound

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<math.h>
#include<vector>
#include<stack>
#include<string>
#include<stdio.h>

using namespace std;
typedef long long LL;
const int MAXN = 2e4 + 10;
int st[MAXN << 2];
int vis[MAXN << 2];
int b[MAXN];
struct node {
    int a,b;
}a[MAXN];

void build(int o,int l,int r)
{
    st[o] = 0;
    if(l == r) return;
    int m = (l + r) >> 1;
    build(o << 1,l,m);
    build(o << 1 | 1,m + 1,r);
}
void pushdown(int o)
{
    if(st[o])
    {
        st[o << 1] = st[o];
        st[o << 1 | 1] = st[o];
        st[o] = 0;
    }
}
void update(int o,int l,int r,int ql,int qr,int val)
{
    if(ql <= l && r <= qr)
    {
        st[o] = val;
        return;
    }
    pushdown(o);
    int m = (l + r) >> 1;
    if(ql <= m) update(o << 1,l,m,ql,qr,val);
    if(qr > m) update(o << 1 | 1,m + 1,r,ql,qr,val);
}
int query(int o,int l,int r,int ind)
{
    if(l == r) return st[o];
    pushdown(o);
    int m = (l + r) >> 1;
    if(m >= ind) return query(o << 1 ,l,m,ind);
    else return query(o << 1 | 1,m + 1,r,ind);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(vis,0,sizeof vis);
        int n;
        scanf("%d",&n);
        build(1,1,2 * n);
        int cnt = 0;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d %d",&a[i].a,&a[i].b);
            b[++cnt] = a[i].a;
            b[++cnt] = a[i].b;
        }
        sort(b + 1,b + 1 + cnt);
        cnt = unique(b + 1, b +1 + cnt) - b - 1;    //去重后的个数
        for(int i = 1; i <= n; i++)
        {
            int l = lower_bound(b + 1,b + 1 + cnt,a[i].a) - b;
            int r = lower_bound(b + 1,b + 1 + cnt,a[i].b) - b;
            update(1,1,2 * n,l,r,i);
        }
        for(int i = 1; i <= 2 * n; i++)
        {
            int tmp = query(1,1,2 * n,i);
//            printf("tmp = %d\n",tmp);
            vis[tmp] = 1;
        }
        int sum = 0;
        for(int i = 1;i <= 2 * n; i++)
            sum += vis[i];
        printf("%d\n",sum);
    }
}

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

原文地址:https://www.cnblogs.com/smallhester/p/11273422.html

时间: 2024-10-05 05:00:03

Mayor's posters POJ - 2528 (线段树 + 离散化)的相关文章

Mayor&#39;s posters POJ - 2528 线段树区间覆盖

//线段树区间覆盖 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int N=100010; int flag; struct node{ int l,r; //vis 是这块区域是否完全被覆盖 bool vis; }tr[N<<2]; struct point { int id; int x

POJ 2528 (线段树+离散化) Mayor&#39;s posters

因为将每个单位都作为一个最小单元的话会爆内存的 所以,将海报的每个端点进行排序,将这些端点最为最小的区间. 毕竟是刚刚接触线段树,理解起来还有些吃力,还是那句话,题做多了慢慢就好了. 萌萌的AC代码君贴上. 1 //#define LOCAL 2 #include <iostream> 3 #include <algorithm> 4 #include <cmath> 5 using namespace std; 6 7 int n; 8 struct CPost 9

poj 2528(线段树+离散化) 市长的海报

http://poj.org/problem?id=2528 题目大意是市长竞选要贴海报,给出墙的长度和依次张贴的海报的长度区间(参考题目给的图),问最后你能看见的海报有几张 就是有的先贴的海报可能会被后贴的海报完全盖住,那就看不见了 这里就非常抽象的区间更新,墙的长度为建立线段树的总区间,每贴一张海报代表将这个区间的颜色涂为相应的,每张海报的颜色当然 都不相同,求最后又多少种颜色就行,但这里还要用到基础的离散化 离散化是把无限空间中无限的个体映射到有限的空间中去,以此提高算法的时空效率. 简单

POJ 2528 线段树+离散化

题意是给你n张海报,告诉你每张海报的宽度和先后顺序,海报会重叠,问你露在外面的海报有多少张?这题主要是离散化理解了好久,关键在于建hash表时不能选择最普通的一一对应,为什么?看了网上一组数据后瞬间就明白了:1,10  1,4  6,10. Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 51347   Accepted: 14875 Description The citizens of

POJ 2528 Mayor&#39;s posters(离散+线段树)

题目大意:往一面墙上贴与墙等高的海报,n次贴完后,求可以看见的海报总数(看见一部分也算) 思路:明显的区间维护,用线段树,不过裸的线段树超时超空间,可以把坐标离散,得到不超过200000个有效点,每个点都表示一个小区间(a[i]~a[i+1]这一段),然后就可以轻松地解决了.不过题目有个坑,给定的右坐标其实不是实际坐标,还要+1,区间总数等于有效点数 -1,更新的时候把查找到的右端点-1再代入,具体见代码: 1 /*****************************************

Mayor&#39;s posters POJ - 2528

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

poj 2528 线段树+特殊离散化

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 51098   Accepted: 14788 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral post

poj 2528 线段树+延迟更新

题目链接:http://poj.org/problem?id=2528 题意: 在墙上贴海报,输入n(1<=n<=10000),表示n张海报,后n行输入 两整数l,r  ( 1<= l, r<= 1e9 ),表示海报从编号为l的石头一直贴到编号为r的石头,输入顺序即为粘贴顺序.问n张贴完之后,还能看到多少张海报. 思路: 显然区间操作,很容易联想到线段树操作,只不过区间 l,r 最大范围可达1e9,直接建树,内存必爆.   那么就需要避开1e9的数据,进行离散化,将区间变成(1到n

Picture POJ - 1177 线段树+离散化+扫描线 求交叉图像周长

参考  https://www.cnblogs.com/null00/archive/2012/04/22/2464876.html #include <stdio.h> #include <algorithm> #define LEN 10000 using namespace std; struct Node { int left; int right; int count;//被覆盖次数 //所包含的区间数量,如三条[1,2],[2,3],[4,5]线段被覆盖,则line=2