码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-08-16 13:53:40      阅读:679      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   os   io   for   ar   art   

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.
bubuko.com,布布扣

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 (离散化+线段树区间更新),布布扣,bubuko.com

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

标签:des   style   http   os   io   for   ar   art   

原文地址:http://blog.csdn.net/u013923947/article/details/38612427

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!