[POJ] 2528 - Mayor's posters - 線段樹

Mayor's posters
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 70483   Accepted: 20331

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


給你一個個區間

你要貼廣告

每次後面的廣告會蓋上前面的廣告


詢問最後能看到多少個廣告



線段樹的題

好像剛開始想歪了

我也不知道怎麼想歪的



其實只要到把貼廣告的順序倒過來

這樣如果每次貼的廣告能覆蓋到未被貼廣告的區間

那就說明這個廣告能被看到


然後用線段樹維護

每個節點有左節點右節點的編號

以及左右節點編號包含的區間是否被貼廣告(0爲未被貼, 1爲被貼)


每次貼廣告的時候從樹頂往下更新

如果掃到一個節點的廣告標記爲 1

那就不往下掃了


如果區間恰好重疊就把廣告標記標記爲1

同時每輪貼廣告ans只加一次



同時這道題需要離散化

然後離散化還不是一般的離散化

例如

3

1 10

1 3

8 10


答案是 3


離散化以後是

1 4

1 2

3 4


這樣答案就成2了,,


所以防止這種情況

第一次離散化以後XJB判一下就好了


剛開始忘記寫up操作了WA了


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;

const int N = 400100;

struct  Three
{
	int l;
	int r;
	int mark;
};

struct Quary
{
    int l;
    int r;
};

Quary quary[N];
Three tree[N * 4];
int n;
int mark;
int ans;
int al[N];
int ar[N];

void up(int x)
{
    if(tree[x << 1].mark == 1 && tree[x << 1 | 1].mark == 1){
        tree[x].mark = 1;
    }
}

void build(int x, int l, int r)
{
	tree[x].l = l;
	tree[x].r = r;
	tree[x].mark = 0;

	if(l != r){
		int mid = l + ((r - l) >> 1);

		build(x << 1, l, mid);
		build(x << 1 | 1, mid + 1, r);
		up(x);
	}
}

void update(int x, int l, int r)
{
	int ll = tree[x].l;
	int rr = tree[x].r;

	if(l > r || tree[x].mark){
		return ;
	}
	if(ll == l && rr == r){
		if(!mark){
			mark = 1;
			ans ++;
			tree[x].mark = 1;
		}
		else{
			tree[x].mark = 1;
		}
	}
	else{
        int mid = ll + ((rr - ll) >> 1);

        update(x << 1, l, min(mid, r));
        update(x << 1 | 1, max(mid + 1, l), r);
        up(x);
	}
}

int main(int argc, char const *argv[])
{
	int ncase;

	scanf("%d", &ncase);
	while(ncase --){
		ans = 0;
		scanf("%d", &n);
		int rec[N], p = 0;
		for(int i = 0; i < n; i ++){
			scanf("%d%d", &quary[i].l, &quary[i].r);
			rec[p++] = quary[i].l;
			rec[p++] = quary[i].r;
		}
		/*小太陽寫的離散化*/
        sort(rec, rec + p);
        p = unique(rec, rec + p) - rec;
        int tmp = p;
		for(int i = 1; i < tmp; ++i)
            if(rec[i] - rec[i-1] > 1)rec[p++] = rec[i-1] + 1;
        sort(rec, rec + p);
        p = unique(rec, rec + p) - rec;
        /*小太陽寫的離散化*/
        build(1, 1, p);
		for(int i = n - 1; i >= 0; i --){
            int lll = lower_bound(rec, rec + p, quary[i].l) - rec + 1;
            int rrr = lower_bound(rec, rec + p, quary[i].r) - rec + 1;

            mark = 0;
            update(1, lll, rrr);
		}

		printf("%d\n", ans);
	}

	return 0;
}


發佈了77 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章