24h購物| | PChome| 登入
2014-02-17 09:22:27| 人氣1,921| 回應0 | 上一篇 | 下一篇

[UVA][線段樹] 10587 - Mayor's posters

推薦 0 收藏 0 轉貼0 訂閱站台

Problem G: Mayor's posters

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.

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 lili+1 ,... , ri.

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

Output for sample input

4

Author: Adapted from VI AMPwPZ by P. Rudnicki

題目描述:


給定每個海報張貼的區間,按照順序貼上去,求最後能看到幾種海報。

如果能看到同一張海報,卻被分開到好幾個地方,只能算看到一種。

題目解法:


需要離散化,離散化時特別小心,需要多離散化一些點,否則會有下方給的測資錯誤。

#include <stdio.h>
#include <map>
#include <string.h>
using namespace std;
int Tree[1048576];
int query(int k, int l, int r, int ql, int qr) {
    if(l > r)    return 1;
    if(ql <= l && r <= qr)
        return Tree[k];
    if(Tree[k])    return 1;
    int m = (l + r)/2;
    if(m >= qr)
        return query(k<<1, l, m, ql, qr);
    if(m < ql)
        return query(k<<1|1, m+1, r, ql, qr);
    return query(k<<1, l, m, ql, qr) &&
                query(k<<1|1, m+1, r, ql, qr);
}
void modify(int k, int l, int r, int ql, int qr) {
    if(l > r) {
        Tree[k] = 1;
        return;
    }
    if(ql <= l && r <= qr) {
        Tree[k] = 1;
        return ;
    }
    if(Tree[k])    return ;
    int m = (l + r)/2;
    if(m >= qr)
        modify(k<<1, l, m, ql, qr);
    else if(m < ql)
        modify(k<<1|1, m+1, r, ql, qr);
    else {
        modify(k<<1, l, m, ql, qr);
        modify(k<<1|1, m+1, r, ql, qr);
    }
    Tree[k] = Tree[k<<1]&Tree[k<<1|1];
}
int main() {
    int testcase, n;
    int i, j, k, L[10005], R[10005];
    scanf("%d", &testcase);
    while(testcase--) {
        scanf("%d", &n);
        map<int, int> RR;
        for(i = 0; i < n; i++) {
            scanf("%d %d", &L[i], &R[i]);
            RR[L[i]] = 1;
            RR[L[i]-1] = 1;
            RR[L[i]+1] = 1;
            RR[R[i]] = 1;
            RR[R[i]-1] = 1;
            RR[R[i]+1] = 1;
        }
        int size = 1;
        for(map<int, int>::iterator it = RR.begin();
            it != RR.end(); it++)
            it->second = size++;
        memset(Tree, 0, sizeof(Tree));
        int ret = 0;
        for(i = n-1; i >= 0; i--) {
            int l = RR[L[i]], r = RR[R[i]];
            // printf("[%d, %d]\n", l, r);
            int v = query(1, 1, size, l, r);
            if(v == 0)
                ret++;
            modify(1, 1, size, l, r);
        }
        printf("%d\n", ret);
    }
    return 0;
}
/*
1
3
1 10
1 3
6 10

*/

台長: Morris
人氣(1,921) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA][剪枝] 10549 - Russian Dolls
此分類上一篇:[UVA][窮舉] 10273 - Eat or Not to Eat

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文