POJ2528 Mayor's posters(線段樹+離散化)
Mayor’s posters
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 57835 Accepted: 16725
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
Alberta Collegiate Programming Contest 2003.10.18
給你一塊公告板,然後有很多人往上面貼海報,後面往上貼的人可以把前面貼的海報蓋住,問你最後能看到幾張海報(只要有露出來就算)。
首先這題告訴你公告板的寬度是千萬級別的,想用線段樹做區間更新,肯定不能用它直接作為葉子節點了,我們就想先給他離散化一下。因為點最多就10000*2(最多10000張 假設所有點都不一樣) 那麼離散化之後最多也就這些點,這樣就可以建樹了。再用二分查詢做一下對映。然後就是正常的線段樹區間更新了。
#include "cstring"
#include "cstdio"
#include "iostream"
#include "string.h"
#include "algorithm"
#include "vector"
#define MAXSIZE 200005
using namespace std;
vector<int> p;
typedef struct
{
int mark;
int left, right;
}NODE;
NODE tree[MAXSIZE * 5];
int temp[200005];
void build(int root, int lf, int rt)
{
int mid;
tree[root].left = lf;
tree[root].right = rt;
tree[root].mark = 0;
if (lf == rt)
{
tree[root].mark = 0;
return;
}
mid = (lf + rt) / 2;
build(2 * root, lf, mid);
build(2 * root + 1, mid + 1, rt);
return;
}
void update(int root, int left, int right, int id)
{
if (tree[root].left == left&&tree[root].right == right)
{
tree[root].mark = id;
return;
}
if (tree[root].mark != 0)
{
tree[root * 2 + 1].mark = tree[root * 2].mark = tree[root].mark;
tree[root].mark = 0;
}
int mid = (tree[root].left + tree[root].right) / 2;
if (mid<left)
update(root * 2 + 1, left, right, id);
else
{
if (mid >= right)
update(root * 2, left, right, id);
else
{
update(root * 2, left, mid, id);
update(root * 2 + 1, mid + 1, right, id);
}
}
return;
}
bool vis[20005];
int sum = 0;
void count(int root)
{
int l = tree[root].left;
int r = tree[root].right;
if (tree[root].mark != 0)
{
if (!vis[tree[root].mark])
{
sum++;
vis[tree[root].mark] = 1;
}
return;
}
if (l == r)
return;
count(root * 2);
count(root * 2 + 1);
}
int main()
{
//freopen("in.txt", "r", stdin);
int c;
scanf("%d", &c);
while (c--)
{
int mm;
scanf("%d", &mm);
memset(temp, 0, sizeof(temp));
memset(vis, 0, sizeof(vis));
sum = 0;
int cnt = 1;
struct node
{
int left, right;
} poster[20005];
for (int i = 1; i <= mm; i++)
{
scanf("%d%d", &poster[i].left, &poster[i].right);
p.push_back(poster[i].left);
p.push_back(poster[i].right);
}
sort(p.begin(), p.end());
p.erase(unique(p.begin(), p.end()), p.end());
for (int i = 1; i <= mm; i++) {
poster[i].left = lower_bound(p.begin(), p.end(), poster[i].left) - p.begin() + 1;
poster[i].right = lower_bound(p.begin(), p.end(), poster[i].right) - p.begin() + 1;
}
build(1, 1, p.size());
for (int i = 1; i <= mm; i++)
{
int l, r;
update(1,poster[i].left, poster[i].right, i);
}
count(1);
printf("%d\n", sum);
}
}
相關推薦
POJ2528 Mayor's posters(線段樹+離散化)
Mayor’s posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 57835 Accepted: 16725 Description The ci
POJ 2528 Mayor's posters(線段樹+離散化)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 60691 Accepted: 17565 Description The citizens of Byteto
Mayor's posters(線段樹+離散化)
++ main splay d3d close tree pac pair lin 這道題最關鍵的點就在離散化吧。 假如有三張海報[1, 10] [10, 13][15, 20] 僅僅三個區間就得占用到20了。 但是離散化後就可以是[1, 2] [2, 3] [
POJ2528——Mayor's posters (線段樹區間更新查詢+離散化)
The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral poste
POJ 2528 Mayor's posters (線段樹 離散化+區間更新+區間求值 )
href eof 求值 給定 一個點 一個 stream 問題 void 題目鏈接:http://poj.org/problem?id=2528 題意:塗色問題,給定n個要塗色的區間(每次用的顏色不一樣,顏色覆蓋性極強),問最後能看到多少種顏色。(貼海報問題轉換) 題解
Mayor's posters【線段樹+離散化】
題解:區間修改線段樹+離散化。cnt=unique(arr,arr+cnt)-arr 用於離散化時非常方便。程式碼:#include<stdio.h> #include<string.
HIHO #1079 : 離散化(線段樹+離散化)
#include<bits/stdc++.h> using namespace std; #define cl(a,b) memset(a,b,sizeof(a)) #define LL long long #define pb push_back #define gcd __gcd #defi
poj2528——Mayor's posters(離散化+區間覆蓋)
Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing t
【POJ 2482】 Stars in Your Window(線段樹+離散化+掃描線)
d+ opera algorithm ans som lov ble word wait 【POJ 2482】 Stars in Your Window(線段樹+離散化+掃描線) Time Limit: 1000MS M
N - Picture POJ - 1177(矩形周長並 + 線段樹 + 離散化)
本程式碼採用的是橫向掃描一遍和縱向掃描一遍來得到最終的結果,核心部分就是求舉行周長並,當然也要對線段樹比較熟悉才行,畢竟矩形周長並,面積並,面積交都要用到線段樹來計算。說說求舉行周長並的過程吧,我們先計算橫向線段的長度,把所有座標的y軸座標按照升序排列,掃描線從y的最小值依次向上掃描,求出每
POJ 1151 Atlantis(線段樹離散化求面積並)(C++)
題目連結:http://poj.org/problem?id=1151 算是模板題,做這個之前要搞懂離散化。這裡,區間最好用[L,R),要不然有些區間無法計算得到。人比較懶,自己去琢磨,不寫註釋了QAQ。 #include <cstdio> #include <v
HDU 2836 Traversal(線段樹+離散化+DP)
題意:給你n個數的序列, 一個數h, 求相鄰數之差不超過h的子序列的個數和 % 9901。 思路:經典水題, 顯然用d[i]表示以a[i]結尾的滿足條件的子序列個數。 那麼對於j < i , | a[j] - a[i] | <= h , 等價於 a[j] &
poj2528 - Mayor's posters - 線段樹離散化(詳解)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 79035 Accepted
poj2528--Mayor's posters(線段樹+離散化)
The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral poste
【POJ2528】Mayor's Posters-線段樹+離散化
(本人本題完成於2016-7-23) 題目大意:有一面很長(10000000節)的牆,要在上面貼N張海報,對於第i張海報,它將會覆蓋牆面的第Li~Ri節。剛開始牆面是空的,求最後還有多少張海報是可見的
資料結構--線段樹&離散化可能有的問題--poj2528 Mayor's posters
給定n個區間,按順序覆蓋後,問最後幾個是可見的。 l,r,範圍1e7,需要離散化。 //但是注意簡單的離散化可能會出現錯誤,給出下面兩個簡單的例子應該能體現普通離散化的缺陷: //例子一:1-10 1-4 5-10 //例子二:1-10 1-4 6-10 //解決的
D - Mayor's posters (線段樹 + 離散化)
滴答滴答---題目連結 The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral pos
poj 2528 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
Mayor's posters 線段樹+離散化
%%% //感覺博主對於離散的解釋很好,很感謝。 %%% //博主解釋了一下本題離散的特殊性,感謝 如三張海報為:1~10 1~4 6~10 離散化時 X[ 1 ] = 1, X[ 2 ] = 4, X[ 3 ] = 6, X[ 4 ] = 10 第一張海報時
Mayor's posters (線段樹+離散化+加思維)
The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral poste