1. 程式人生 > >Mayor's posters (線段樹+離散化)

Mayor's posters (線段樹+離散化)

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

這是一道很經典的線段樹離散化問題,題目中給出的區間[li,ri]的資料都很大,直接用線段樹做的話至少需要10000000*4的空間,因此,我們首先要對資料進行離散化處理
首先,我們要弄清楚離散化的概念,我們根據題目樣例進行分析,可以看到[1,4],[2,6],[8,10],[3,4],[7,10]這五個區間,我們將這10個數都拿出來排個序,建立序列a
1 2 3 4 4 6 7 8 10 10
接下來我們對序列a去下重
1 2 3 4 6 7 8 10
然後我們用他們的相對大小(例如:6在這個序列中是第5大,10在這個序列中是第8大)建立另一個序列b
1 2 3 4 5 6 7 8
最後我們用每個數對應的相對大小來替換原來的區間就變成了這樣
[1,4],[2,5],[7,8],[3,4],[6,8]
我們用這個區間來計算一下最後能觀察到的海報數的話會發現,結果仍然是4

我個人對離散化的理解就是用數字的相對大小來替換他的實際大小從而達到減小其值,卻不破壞它的位置關係的目的
這道題我們利用這樣的方法就能將li,ri這麼大的數縮小到最大隻有20000(因為最多有20000個點)
理解題意後我們直接看程式碼,至於線段樹部分就是普通的區間修改而已
#pragma GCC optimize("Ofast")

#include <iostream>
#include <algorithm>
#include <set>

using namespace std;

#define endl '\n'
#define ll long long

struct node
{
    int l, r, flag;
} tree[800005];

int base[20005], ls[20005];
set<int> s;

void tree_build(int i, int l, int r)
{
    tree[i].l = l;
    tree[i].r = r;
    tree[i].flag = 0;
    if (l == r)
    {
        return;
    }
    int mid = (l + r) >> 1;
    tree_build(i << 1, l, mid);
    tree_build(i << 1 | 1, mid + 1, r);
}

void push_down(int i)
{
    if (tree[i].flag)
    {
        tree[i << 1].flag = tree[i].flag;
        tree[i << 1 | 1].flag = tree[i].flag;
        tree[i].flag = 0;
    }
}

void update(int i, int l, int r, int num)
{
    if (tree[i].l >= l && tree[i].r <= r)
    {
        tree[i].flag = num;
        return;
    }
    if (tree[i].r < l || tree[i].l > r)
    {
        return;
    }
    push_down(i);
    if (tree[i << 1].r >= l)
    {
        update(i << 1, l, r, num);
    }
    if (tree[i << 1 | 1].l <= r)
    {
        update(i << 1 | 1, l, r, num);
    }
}

void search(int i, int l, int r)
{
    if (tree[i].flag)
    {
        s.insert(tree[i].flag);//利用set自動去重的性質來記錄海報數量
        return;
    }
    if (l == r)
    {
        return;
    }
    int mid = (l + r) >> 1;
    search(i << 1, l, mid);
    search(i << 1 | 1, mid + 1, r);
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        int n, x, y, pos = 1;
        cin >> n;
        s.clear();
        tree_build(1, 1, 200005);
        for (int i = 1; i <= n; ++i)
        {
            //這地方我們開兩個陣列,base用與計算每個數的相對位置,ls是離散化後的陣列
            cin >> x >> y;
            base[pos] = x;
            ls[pos++] = x;
            base[pos] = y;
            ls[pos++] = y;
        }
        //無論是sort還是unique還是lower_bound區間設定都是左閉右開的形式,品,你細細的品
        sort(base + 1, base + pos);
        int num = unique(base + 1, base + pos) - base;//對base排序並去重,必須排序後才能用unique
        for (int i = 1; i < pos; ++i)
        {
            ls[i] = lower_bound(base + 1, base + num, ls[i]) - base;
        }
        for (int i = 2; i < pos; i += 2)
        {
            update(1, ls[i - 1], ls[i], i);
        }
        search(1, 1, 200005);
        cout << s.size() << endl;
    }
    return 0;
}

 

你以為這就完事了?其實這樣離散化在這道題中會有一些bug,我們看這組資料[1,10],[1,3],[6,10],很明顯答案是3
但是離散化之後為[1,4],[1,2],[3,4],答案變成了2
為解決這種問題,我們可以在更新線段樹的時候將區間從[l,r]變成[l,r-1],就將區間轉化成了[1,3],[1,1],[3,3]這樣的樹
但是當我們遇到這樣的資料[1,3],[1,1],[2,2],[3,3],就會導致區間更新時出錯,我們可以將初始資料的r都加上1,就排除了li和ri相等的情況,如果沒有這種情況,離散化後的區間也都是一樣的
其實這道題資料很弱,不管這樣的情況也能過(逃
#pragma GCC optimize("Ofast")

#include <iostream>
#include <algorithm>
#include <set>

using namespace std;

#define endl '\n'
#define ll long long

struct node
{
    int l, r, flag;
} tree[100005];

int base[20005], ls[20005];
set<int> s;

void tree_build(int i, int l, int r)
{
    tree[i].l = l;
    tree[i].r = r;
    tree[i].flag = 0;
    if (l == r)
    {
        return;
    }
    int mid = (l + r) >> 1;
    tree_build(i << 1, l, mid);
    tree_build(i << 1 | 1, mid + 1, r);
}

void push_down(int i)
{
    if (tree[i].flag)
    {
        tree[i << 1].flag = tree[i].flag;
        tree[i << 1 | 1].flag = tree[i].flag;
        tree[i].flag = 0;
    }
}

void update(int i, int l, int r, int num)
{
    if (tree[i].l >= l && tree[i].r <= r)
    {
        tree[i].flag = num;
        return;
    }
    if (tree[i].r < l || tree[i].l > r)
    {
        return;
    }
    push_down(i);
    if (tree[i << 1].r >= l)
    {
        update(i << 1, l, r, num);
    }
    if (tree[i << 1 | 1].l <= r)
    {
        update(i << 1 | 1, l, r, num);
    }
}

void search(int i, int l, int r)
{
    //cout << tree[i].flag << " " << tree[i].l << " " << tree[i].r << endl;
    if (tree[i].flag)
    {
        //cout << tree[i].flag << " " << tree[i].l << " " << tree[i].r << endl;
        s.insert(tree[i].flag);
        return;
    }
    if (l == r)
    {
        return;
    }
    int mid = (l + r) >> 1;
    search(i << 1, l, mid);
    search(i << 1 | 1, mid + 1, r);
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        int n, x, y, pos = 1;
        cin >> n;
        s.clear();
        tree_build(1, 1, 20005);
        for (int i = 1; i <= n; ++i)
        {
            cin >> x >> y;
            base[pos] = x;
            ls[pos++] = x;
            base[pos] = y + 1;
            ls[pos++] = y + 1;
        }
        sort(base + 1, base + pos);
        int num = unique(base + 1, base + pos) - base;
        for (int i = 1; i < pos; ++i)
        {
            ls[i] = lower_bound(base + 1, base + num, ls[i]) - base;
        }
        for (int i = 2; i < pos; i += 2)
        {
            update(1, ls[i - 1], ls[i] - 1, i);
        }
        search(1, 1, 20005);
        cout << s.size() << endl;
    }
    return 0;
}

 

&nbs