POJ 2528 Mayor'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 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

离散化加线段树区间更新和查询。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <ctype.h>
#include <iostream>
#define lson o << 1, l, m
#define rson o << 1|1, m+1, r
using namespace std;
typedef long long LL;
const int MAX = 200000000;
const int maxn = 10010;
int t, n, a, b, c, ans;
int fu[maxn<<4], cnt[maxn<<4], vis[maxn<<2];
struct C{
    int x, y;
} in[maxn];
int bs(int v, int x, int y) {
    int m;
    while(x < y) {
        m = (x+y) >> 1;
        if(fu[m] >= v) y = m;
        else x = m+1;
    }
    return x;
}
void down(int o) {
    if(cnt[o] != -1) {
        cnt[o<<1] = cnt[o<<1|1] = cnt[o];
        cnt[o] = -1;
    }
}
void update(int o, int l, int r) {
    if(a <= l && r <= b) {
        cnt[o] = c;
        return;
    }
    down(o);
    int m = (l+r) >> 1;
    if(a <= m) update(lson);
    if(m < b ) update(rson);
}
void query(int o, int l, int r) {
    if(cnt[o] != -1) {
        if(!vis[ cnt[o] ]) {
            ans ++;
            vis[ cnt[o] ] = 1;
        }
        return ;
    }
    if(l == r) return;
    int m = (l+r) >> 1;
    query(lson);
    query(rson);
}
int main()
{
    cin >> t;
    while(t--) {
        cin >> n;
        int k = 0;
        for(int i = 0; i < n; i++) {
            scanf("%d%d", &in[i].x, &in[i].y);
            fu[k++] = in[i].x;
            fu[k++] = in[i].y;
        }
        sort(fu, fu+k);
        int m = 1;
        for(int i = 1; i < k; i++)
            if(fu[i] != fu[i-1]) fu[m++] = fu[i];
        for(int i = m-1; i >= 1; i--)
            if(fu[i] != fu[i-1] + 1) fu[m++] = fu[i-1]+1;
        sort(fu, fu+m);
        memset(cnt, -1, sizeof(cnt));
        for(int i = 0; i < n; i++) {
            a = bs(in[i].x, 0, m-1);
            b = bs(in[i].y, 0, m-1);
            c = i;
            update(1, 0, m-1);
        }
        memset(vis, 0, sizeof(vis));
        ans = 0;
        query(1, 0, m-1);
        printf("%d\n", ans);
    }
    return 0;
}



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

时间: 2024-08-10 13:05:19

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

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

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

POJ 2528 Mayor&#39;s posters(线段树区间染色+离散化或倒序更新)

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 59239   Accepted: 17157 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 Mayor&#39;s posters(线段树区间覆盖、离散化)

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 49385   Accepted: 14304 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——Mayor&#39;s posters——————【线段树区间替换、找存在的不同区间】

Mayor's posters Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2528 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been plac

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

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45031   Accepted: 13080 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 - Mayor&#39;s posters 【线段树+离散化+补点】

http://poj.org/problem?id=2528 #include <cstdio> #include <iostream> #include <set> #include <cstring> #include <string> #define left rt<<1 #define right rt<<1|1 using namespace std; const int MAXN = 32768 + 5; in

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

题目链接:Mayor's posters 题意:按顺序往墙上贴海报,可以重叠,问最后可以看到多少海报.(被覆盖的海报是看不到的) 注意: 因为数据比较大,所以不离散化,肯定爆内存. 还有就是,不能只是单纯的离散化,还要处理好点的边界 举个例子 4 2  10. 2  8 3  6 6  8 8  10 离散化后 2 3 6 8 10 1 2 3 4 5 覆盖掉了 1 5   和  1 4俩段 只剩下 2  3  .3  4. 4  5 答案是 3 但是正确答案是4 所以,离散化处理时要处理边界,

【POJ】2528 Mayor&#39;s posters ——离散化+线段树

Mayor's posters Time Limit: 1000MS    Memory Limit: 65536K 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

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

这道题目让我又重新认识了一下离散化: 首先总结一下离散化的特点: 1)有时区间的端点并不是整数,或者区间太大导致建树内存开销过大而MLE,那么就需要进行离散化后再建树. 2)意思是将区间范围很大的数据集映射到较小的数据集,这样建树更加有效,或者说我们只取需要的值来用. 这个意思说到底就是进行映射,把原来很大的映射到一个较小的空间中去. 题意: 给定一些海报,它们可能相互重叠,告诉你每个海报的宽度(它们的高度都是一样的)和先后的叠放次序,问没有被完全盖住的海报有多少张? 这里我们注意到了数据的范围