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

Source

思路:不知道为什么,我提交 long long就是不对,全改为int就过了,好气啊
#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
using namespace std;
#define ll long long

int t, n, ans;
int a[400000+8], pos[100000+8][2];
bool sign[400000+8];

struct edge///离散
{
    int l, r;
}e[400000+8];

struct node
{
    int l, r, num;
}tree[400000+8];

void build(int i, int l, int r)///建树
{
    tree[i].l = l;
    tree[i].r = r;
    tree[i].num = 0;
    if(tree[i].l == tree[i].r)///tree[i].l与l无区别,tree[i].r与r无区别
        return;
    int mid = (tree[i].l + tree[i].r) / 2;///tree[i].l与l无区别,tree[i].r与r无区别
    build(i * 2, l, mid);
    build(i * 2 + 1, mid + 1, r);
}

void push_down(int i)///向下传递num的值
{
    if(tree[i].num != 0)
    {
        tree[i * 2].num = tree[i * 2 + 1].num = tree[i].num;
        tree[i].num = 0;
    }
    return;
}

void pls(int i, int l, int r, int k)///区间修改
{
    if(tree[i].l >= l && tree[i].r <= r)
    {
        tree[i].num = k;
        return;
    }
    push_down(i);
    int mid = (tree[i].l + tree[i].r) / 2;///tree[i].l与l无区别,tree[i].r与r无区别
    if(l <= mid)
        pls(i * 2, l, r, k);
    if(mid < r)
        pls(i * 2 + 1, l, r, k);
}

void search(int i)
{
    if(tree[i].num != 0)///如果那个点不等于0
    {
        if(!sign[tree[i].num])///且这个点的值没数过
        {
            sign[tree[i].num] = 1;///标记他数过了
            ans++;///总数量加
        }
        return;
    }
    search(i * 2);
    search(i * 2 + 1);
}

int main()
{
    for(scanf("%d", &t); t--; )///
    {
        memset(sign, 0, sizeof(sign));
        memset(a, 0, sizeof(a));
        memset(pos, 0, sizeof(pos));
        scanf("%d", &n);
        int id = 1;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d", &pos[i][0], &pos[i][1]);
            a[id++] = pos[i][0];
            a[id++] = pos[i][1];
        }
        sort(a + 1, a + id);
        int index = 1;
        for(int i = 2; i < id; i++)
            if(a[i] != a[i-1])
                a[++index] = a[i];
        ans = 0;
        int r = -1;
        for(int i = 1; i <= n; i++)
        {
            e[i].l = lower_bound(a + 1, a + index + 1, pos[i][0]) - a;
            e[i].r = lower_bound(a + 1, a + index + 1, pos[i][1]) - a;
            r = max(r, e[i].r);///确定区间最长可到达哪里
        }
        build(1, 1, r);
        for(int i = 1; i <= n; i++)
            pls(1, e[i].l, e[i].r, i);
        search(1);
        printf("%d\n", ans);
    }
    return 0;
}

 

posted @ 2019-08-07 18:33  明霞  阅读(165)  评论(0编辑  收藏  举报