POJ 2582 Mayor's posters 線段樹入門題+離散化
本來線段樹就學的不紮實,後來個什麼離散化,亂亂的啊、、後來看了一人寫的,用結構體記錄的方法進行離散化。感覺寫的挺好的。
題意很簡單:就是在一堵牆上塗色,然後輸出你能看到的顏色的種類。離散化+塗色問題。
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:
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.
- 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.
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;
}
相關文章
- POJ 2528 Mayor's posters (線段樹 區間更新+離散化)
- POJ 2528 Mayor's posters (線段樹區間更新 + 離散化)
- 【dp+離散化+線段樹優化】Paint優化AI
- POJ 2777 Count Color 線段樹入門題
- POJ 2828 Buy Tickets 線段樹入門(建樹稍微有點抽象)抽象
- 線段樹入門
- 線段樹入門理解
- 線段樹入門(Segment Tree)
- POJ 3264 Balanced Lineup 線段樹入門(點的查詢)
- HDU 1556 Color the ball 線段樹入門題
- 線段樹模版:從入門到入墳
- HDU 1754 I Hate It 線段樹入門
- 資料處理 | pandas入門專題——離散化與one-hot
- HDU 1166 敵兵佈陣 線段樹入門題目
- hdu4288 離線處理線段樹
- POJ 3468 A Simple Problem with Integers(線段樹區間操作)
- POJ 2991 Crane(線段樹+計算幾何)
- POJ 3468 A Simple Problem with Integers (線段樹 區間更新)
- HDU 5862 Counting Intersections(樹狀陣列+掃描線+離散化)陣列
- hdu4325 樹狀陣列+離散化陣列
- 離散化
- 可持久化線段樹持久化
- POJ 3468 A Simple Problem with Integers (線段樹 區間共加)
- POJ 3468-A Simple Problem with Integers(區間更新線段樹)
- POJ 1418 圓的基本操作以及 圓弧離散化
- 【離散優化】覆蓋問題優化
- POJ 2886 Who Gets the Most Candies?(線段樹+反素數)
- 線段樹也能是 Trie 樹 題解
- 線~段~樹
- 線段樹
- TZOJ 8472 : Tree (重鏈剖分+線段樹) POJ 3237
- 線段樹最佳化建圖
- POJ 3368-Frequent values(RMQ+離散化-最頻繁的元素)MQ
- POJ 3468 【區間修改+區間查詢 樹狀陣列 | 線段樹 | 分塊】陣列
- HDU 3333 Turing Tree(線段樹+離線操作)
- POJ 2777-Count Color(線段樹-區間染色查詢)
- 線段樹 hate it
- 【模版】線段樹