POJ 2582 Mayor's posters 線段樹入門題+離散化

畫船聽雨發表於2014-02-20

本來線段樹就學的不紮實,後來個什麼離散化,亂亂的啊、、後來看了一人寫的,用結構體記錄的方法進行離散化。感覺寫的挺好的。

題意很簡單:就是在一堵牆上塗色,然後輸出你能看到的顏色的種類。離散化+塗色問題。

Mayor's posters
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 37301   Accepted: 10846

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

#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-7
#define M 10001000
//#define LL __int64
#define LL long long
#define INF 0x3f3f3f3f
#define PI 3.1415926535898

const int maxn = 100010;
using namespace std;

struct node
{
    int l, r;
    int col;
    int vis;
} f[4*maxn];

struct node1
{
    int a, n, f;
} p[maxn*2];

struct node2
{
    int l, r, col;
} q[maxn];

int used[maxn];

int cmp(node1 a, node1 b)
{
    return a.a < b.a;
}

void Bulid(int l, int r, int site)
{
    f[site].l = l;
    f[site].r = r;
    f[site].col = 0;
    f[site].vis = 0;
    if(l == r)
        return;
    int mid = (l+r)>>1;
    Bulid(l, mid, site<<1);
    Bulid(mid+1, r, site<<1|1);
}

int Search(int l, int r, int site)
{
    int cum = f[site].col;
    if(f[site].vis)
    {
        if(!used[cum])
        {
            used[cum] = 1;
            return 1;
        }
        else
            return 0;
    }
    int mid = (f[site].l+f[site].r)>>1;//注意這裡面的邊界,是二分子樹。
    return Search(l, mid, site<<1)+Search(mid+1, r, site<<1|1);
}

void Updata(int l, int r, int cnum, int site)
{
    if(f[site].vis && f[site].col == cnum)
        return;
    if(f[site].l == l && f[site].r == r)
    {
        f[site].col = cnum;
        f[site].vis = 1;
        return ;
    }
    if(f[site].vis)
    {
        f[site<<1].col = f[site<<1|1].col = f[site].col;
        f[site<<1].vis = f[site<<1|1].vis = f[site].vis;
        f[site].vis = 0;
    }
    int mid = (f[site].l+f[site].r)>>1;
    if(r <= mid)
        Updata(l, r, cnum, site<<1);
    else if(l > mid)
        Updata(l, r, cnum, site<<1|1);
    else
    {
        Updata(l, mid, cnum, site<<1);
        Updata(mid+1, r, cnum, site<<1|1);
    }
}

int main()
{
    int k;
    int n;
    cin >>k;
    int l, r;
    while(k--)
    {
        scanf("%d",&n);
        for(int i = 1; i <= n; i++)//離散化
        {
            scanf("%d %d",&l, &r);
            p[i*2-1].a = l;//儲存起始位置
            p[i*2-1].n = i;
            p[i*2-1].f = 0;
            p[i*2].a = r;//儲存結束位置
            p[i*2].n = i;
            p[i*2].f = 1;
        }
        sort(p+1, p+n*2+1, cmp);
        p[0].a = p[1].a;
        int m = 1, nn, f;
        for(int i = 1; i <= 2*n; i++)
        {
            nn = p[i].n;
            f = p[i].f;
            if(p[i].a != p[i-1].a)
                m++;
            if(!f)
            {
                q[nn].l = m;
                q[nn].col = nn;
            }
            else
            {
                q[nn].r = m;
                q[nn].col = nn;
            }
        }
        Bulid(1,m, 1);
        for(int i = 1; i <= n; i++)
            Updata(q[i].l, q[i].r, q[i].col, 1);
        memset(used, 0 , sizeof(used));
        printf("%d\n",Search(1, m, 1));
    }
    return 0;
}


相關文章