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 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

思路:很经典的题。。线段树+离散化,难点主要在于需要用特殊的离散化方法,一般的离散化方法会出错比如下面这串数据31-101-46-10一般离散化后就会变成 a[0] = 1;a[1] = 4;a[2] = 6;a[3]= 10;离散化将把这些数据的下标作为值放进线段树处理后就会成为:0 - 3 为1颜色0 - 1 为2颜色2 - 3 为3颜色最后只存在两种颜色但正确的过程应该是:1 - 10 为1颜色1 - 4 为2颜色6 - 10 为3颜色最后存在三种颜色

实现代码:
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid int m = (l + r) >> 1
const int M = 2e5+10;
int col[M<<2],Hash[M<<2],cnt,a[M],l[M],r[M];
void pushdown(int rt){
    if(col[rt]!=-1){
        col[rt<<1] = col[rt<<1|1] = col[rt];
        col[rt] = -1;
    }
}

void update(int L,int R,int c,int l,int r,int rt){
    if(L <= l&&R >= r){
        col[rt] = c;
        return ;
    }
    pushdown(rt);
    mid;
    if(L <= m) update(L,R,c,lson);
    if(R > m) update(L,R,c,rson);
}

void query(int l,int r,int rt){
    if(col[rt]!=-1){
        if(Hash[col[rt]]==0) cnt++;
        Hash[col[rt]] = 1;
        return ;
    }
    if(l == r) return;
    mid;
    query(lson);
    query(rson);
}

int bin(int key,int n,int a[]){
    int l = 0,r = n-1;
    while(l <= r){
        mid;
        if(a[m] == key) return m;
        else if(a[m] < key) l = m+1;
        else r = m-1;
     }
     return -1;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int t,n;
    cin>>t;
    while(t--){
        cin>>n;
        memset(col,-1,sizeof(col));
        memset(Hash,0,sizeof(Hash));
        int nn = 0;
        cnt = 0;
        for(int i = 0;i < n;i++){
            cin>>l[i]>>r[i];
            a[nn++] = l[i];a[nn++] = r[i];
        }
        sort(a,a+nn);
        int m = 1;
        for(int i = 1;i < nn;i ++){
            if(a[i]!=a[i-1]) a[m++] = a[i];
        }
        for(int i = m-1;i > 0;i --){
            if(a[i]!=a[i-1]+1) a[m++] = a[i] + 1;
        }
        sort(a,a+m);
        for(int i = 0;i < n;i ++){
            int li = bin(l[i],m,a);
            int ri = bin(r[i],m,a);
            update(li,ri,i,0,m,1);
        }
        query(0,m,1);
        cout<<cnt<<endl;
    }
}

原文地址:https://www.cnblogs.com/kls123/p/8590602.html

时间: 2024-07-31 10:46:35

poj 2528 线段树+特殊离散化的相关文章

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

Mayor&#39;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 post

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(线段树+离散化) 市长的海报

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

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

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(离散化线段树)

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

POJ 2528 Mayor&#39;s posters 离散化和线段树题解

本题就是要往墙上贴海报,问最后有多少可见的海报. 其实本题的难点并不是线段树,而是离散化. 因为数据很大,直接按原始数据计算那么就会爆内存和时间的. 故此需要把数据离散化. 比如有海报1 6   7 9   20 100  5 1000的原始数据,直接计算需要1-1000的内存,离散化之后只需要8内存,因为只有4组数据8个数. 本题更进一步高级一点的离散化就是需要把不相邻的两个数据插入一个数值,表示有空白的地方,不是所有海报都覆盖到的. 比如上面的数据要离散为:1 2  5 6  7 8 9 1

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

题意  在墙上贴n张海报  输入每张海报的的左右端点坐标  问最后可以看到多少张海报  能看到一点也是能看到 先把线段树初始化为0 输入一张海报  就把那个区间变成这张海报的序号  最后判断墙上有多少个不同的序号就行了 但是海报坐标的端点值高达10000000  直接用线段树会超时   但是注意到海报最多只有10000张  也就是最多有20000个不同的坐标  于是可以利用离散化的知识   把所有坐标排序 注意所有右端点坐标+1也要加入排序(注意1,10 ; 1,3;  7,10这种情况  如果