POJ 2528 Mayor's posters (線段樹 區間更新+離散化)
Mayor's posters
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 65645 | Accepted: 18967 |
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
題意:
給你無限長的廣告牌,給你n個廣告和他們放置的位置,按照輸入資料的順序放置前後,問你能看見幾個廣告,看見一部分也算。
POINT:
2018.6.1:
從後往前處理。
線段樹只存有沒有空位。
然後有空位就可以change l到r。都讓他沒空位就行了。
**每個數不是代表一個邊界而是代表一塊展板!**
所以去年理解錯了題目
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn = 10000*2+33;
#define lt x<<1
#define rt x<<1|1
int num[maxn<<3];
int lazy[maxn<<3];
int x[maxn*4];
int sz;
int findi(int xx)
{
return lower_bound(x+1,x+1+sz,xx)-(x);
}
struct node
{
int l,r,s;
}b[10100];
void up(int x)
{
num[x]=num[lt]|num[rt];
}
void build(int x,int l,int r)
{
if(l==r) num[x]=1;
else{
int mid = (l+r)>>1;
build(lt,l,mid);
build(rt,mid+1,r);
up(x);
}
lazy[x]=0;
}
void pushdown(int x)
{
if(lazy[x]){
num[lt]=0;
num[rt]=0;
lazy[lt]=lazy[rt]=1;
lazy[x]=0;
}
}
void change(int x,int l,int r,int ll,int rr)
{
if(ll<=l&&rr>=r){
num[x]=0;
lazy[x]=1;
return;
}
pushdown(x);
int mid = (l+r)>>1;
if(ll<=mid) change(lt,l,mid,ll,rr);
if(mid<rr) change(rt,mid+1,r,ll,rr);
up(x);
}
int query(int x,int l,int r,int ll,int rr)
{
if(ll<=l&&rr>=r){
if(num[x]>0)
return 1;
return 0;
}
pushdown(x);
int mid = (l+r)>>1;
int ans=0;
if(ll<=mid){
ans=query(lt,l,mid,ll,rr);
}
if(ans==0&&mid<rr){
ans=query(rt,mid+1,r,ll,rr);
}
up(x);
return ans;
}
int flag[10200];
int main()
{
//每個數不是代表一個邊界而是代表一塊展板!
int n;
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
memset(num,0,sizeof num);
int cnt=0;
x[++cnt]=0;
x[++cnt]=10000000;
for(int i=1;i<=n;i++){
scanf("%d%d",&b[i].l,&b[i].r);
x[++cnt]=b[i].l;
x[++cnt]=b[i].l+1;
x[++cnt]=max(1,b[i].l-1);
x[++cnt]=b[i].r;
x[++cnt]=b[i].r+1;
x[++cnt]=max(1,b[i].r-1);
}
sort(x+1,x+1+cnt);
sz=unique(x+1,x+1+cnt)-(x+1);
int R=findi(10000000);
build(1,1,R);
int ans=0;
for(int i=n;i>=1;i--){
int l=findi(b[i].l);
int r=findi(b[i].r);
if(query(1,1,R,l,r)){
ans++;
change(1,1,R,l,r);
}
}
printf("%d\n",ans);
}
}
歷史:
先離散化,但是如果直接按照順序去離散化,理論上是錯的,但是可以AC.
如果你AC了 這組資料必然過不了 1 3 1 10 1 3 6 10 正確是3 但是隻當你算出來是2的時候你才能A 為什麼呢 因為4-5這段區間離散化的時候並沒有處理到區間儲存的是被第幾個廣告完全覆蓋,有多個廣告則是零,這題折磨了我很久。
更悲催的是明明已經AC了,我卻看成WA又檢查了20分鐘!!!
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <map>
#include <string.h>
#include <algorithm>
#include <fstream>
using namespace std;
#define lt 2*x
#define rt 2*x+1
#define LL long long
const int N = 22000*5;
int b[N/5];
int col[N],fl[N];
struct node
{
int l,r;
}a[N/5];
void pushdown(int x)
{
fl[lt]=fl[x];
fl[rt]=fl[x];
col[lt]=fl[x];
col[rt]=fl[x];
fl[x]=0;
}
void add(int x,int l,int r,int ll,int rr,int i)
{
if(fl[x]) pushdown(x);
if(ll<=l&&rr>=r)
{
col[x]=i;
fl[x]=i;
}
else
{
int mid=(l+r)>>1;
if(ll<=mid) add(lt,l,mid,ll,rr,i);
if(mid<rr) add(rt,mid+1,r,ll,rr,i);
if(col[lt]==col[rt]) col[x]=col[rt];
else col[x]=0;
}
}
int vis[N];
int query(int x,int l,int r)
{
if(fl[x]) pushdown(x);
int ans=0;
if(col[x]||l==r)
{
if(!vis[col[x]])
{
vis[col[x]]=1;
ans++;
}
}
else
{
int mid=(l+r)>>1;
ans+=query(lt,l,mid);
ans+=query(rt,mid+1,r);
}
return ans;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
memset(b,0,sizeof b);
memset(col,0,sizeof col);
memset(fl,0,sizeof fl);
int cnt=0;
for(int i=1;i<=n;i++)
{
scanf("%d %d",&a[i].l,&a[i].r);
b[++cnt]=a[i].l;
b[++cnt]=a[i].r;
}
sort(b+1,b+1+cnt);
int m=unique(b+1,b+1+cnt)-b-1;
for(int i=1;i<=n;i++)
{
a[i].l=lower_bound(b+1,b+m+1,a[i].l)-b;
a[i].r=lower_bound(b+1,b+m+1,a[i].r)-b;
}
for(int i=1;i<=n;i++)
{
add(1,1,m,a[i].l,a[i].r,i);
}
memset(vis,0,sizeof vis);
int ans=query(1,1,m);
printf("%d\n",ans);
}
return 0;
}
相關文章
- POJ 2528 Mayor's posters (線段樹區間更新 + 離散化)
- POJ 2582 Mayor's posters 線段樹入門題+離散化
- POJ 3468 A Simple Problem with Integers (線段樹 區間更新)
- POJ 3468-A Simple Problem with Integers(區間更新線段樹)
- 【dp+離散化+線段樹優化】Paint優化AI
- POJ 3468 A Simple Problem with Integers(線段樹區間操作)
- hihocoder 1078 線段樹的區間修改 (線段樹 區間更新 模板)
- POJ 3468 A Simple Problem with Integers (線段樹 區間共加)
- POJ 3468 【區間修改+區間查詢 樹狀陣列 | 線段樹 | 分塊】陣列
- HDU 1698 Just a Hook (線段樹區間更新)Hook
- Codeforces 52C (線段樹區間更新)
- POJ 2777-Count Color(線段樹-區間染色查詢)
- 區間k小值(可持久化線段樹)持久化
- POJ 3264-Balanced Lineup詳解(線段樹區間求值)
- poj 3237 樹鏈剖分(區間更新,區間查詢)
- Codeforces 272C Dima and Staircase (線段樹區間更新 或 線性掃)AI
- 線段樹(3)——區間操作疊加
- hdu 2665 可持久化線段樹求區間第K大值(函式式線段樹||主席樹)持久化函式
- 芻議線段樹 2 (區間修改,區間查詢)
- 線段樹維護區間等差數列
- HDU 1754 I Hate It (線段樹 區間最值)
- Java 演算法-區間求和I(線段樹)Java演算法
- Leetcode 327. 區間和的個數 (字首和 + 離散化 + 樹狀陣列)LeetCode陣列
- POJ 1195-Mobile phones(二維樹狀陣列-區間更新區間查詢)陣列
- hdu4288 離線處理線段樹
- POJ 2991 Crane(線段樹+計算幾何)
- HDU 5862 Counting Intersections(樹狀陣列+掃描線+離散化)陣列
- hdu4325 樹狀陣列+離散化陣列
- 1082 線段樹練習 3 區間查詢與區間修改
- 離散化
- 可持久化線段樹持久化
- (poj3468)A Simple Problem with Integers(區間更新)
- POJ 2777 Count Color 線段樹入門題
- 區間演算法題用線段樹可以秒解?演算法
- POJ 2828 Buy Tickets 線段樹入門(建樹稍微有點抽象)抽象
- HDU1698 Just a Hook【線段樹基礎:區間修改+區間查詢】Hook
- POJ 1418 圓的基本操作以及 圓弧離散化
- 線段樹學習筆記(更新中)筆記