題目鏈接:[點擊打開鏈接](http://poj.org/problem?id=2528)
題意:在墻上貼海報,海報可以互相覆蓋,問最后可以看見幾張海報。
該題是線段樹區間修改+離散化的應用。
不難想到, 每次對一個最長10^7的線段進行線段樹的區間修改, 最后統計。
線段樹的復雜度是log10^7, 應該不會超時, 但是會超內存。 所以想到要離散化, 將區間端點值有映射成一個盡量小的值。
但是該題求的是覆蓋情況, 如果按照單純的點對點的離散化, 那樣會出現錯誤答案。
例如: ?依次貼[1,10], [1,3], [5,10] , 離散化后1->1, 3->2, 5->3, 10->4
那么第一次讓離散后的區間[1,4]變成1,第二次讓[1,2]變成2,第三次讓[3,4]變成3,那么最終答案成了2, 其實是3。
問題之所在就在于:小的區間不會覆蓋大區間的所有部分。 解決方法就是在相鄰區間端點大于1時再添加一個二者中間的數當作“空白”區域。
不會離散化的先學學離散化吧, 一般用二分來維護比較簡單。
細節參見代碼:
~~~
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 1000000000;
const int maxn = 11111;
int T,n,ans,setv[maxn<<4],X[maxn<<3];
bool vis[maxn];
struct node {
int l, r;
node(int ll=0, int rr=0):l(ll), r(rr) {}
bool operator < (const node& rhs) const {
return l < rhs.l || (l == rhs.l && r < rhs.r);
}
}a[maxn];
void pushdown(int o) {
if(setv[o]) {
setv[o<<1] = setv[o<<1|1] = setv[o];
setv[o] = 0;
}
}
void update(int L, int R, int v, int l, int r, int o) {
int m = (l + r) >> 1;
if(L <= l && r <= R) {
setv[o] = v; return ;
}
pushdown(o);
if(L <= m) update(L, R, v, l, m, o<<1);
if(m < R) update(L, R, v, m+1, r, o<<1|1);
}
void query(int l, int r, int o) {
int m = (l + r) >> 1;
if(setv[o]) {
if(!vis[setv[o]]) {
vis[setv[o]] = true;
++ans;
}
return ;
}
if(l == r) return ;
query(l, m, o<<1);
query(m+1, r, o<<1|1);
}
int main() {
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
int cnt = 0;
for(int i=1;i<=n;i++) {
scanf("%d%d",&a[i].l,&a[i].r);
X[cnt++] = a[i].l; X[cnt++] = a[i].r;
}
sort(X, X+cnt);
cnt = unique(X, X+cnt) - X;
int m = cnt;
for(int i=1;i<cnt;i++) {
if(X[i] > X[i-1]+1) X[m++] = X[i] + 1;
}
sort(X, X + m);
memset(vis, false, (n+1)*sizeof(vis[0]));
memset(setv, 0, sizeof(setv));
for(int i=1;i<=n;i++) {
int lc = lower_bound(X, X + m, a[i].l) - X + 1;
int rc = lower_bound(X, X + m, a[i].r) - X + 1;
update(lc, rc, i, 1, m, 1);
}
ans = 0;
query(1, m, 1);
printf("%d\n",ans);
}
return 0;
}
~~~
- 前言
- 1608 - Non-boring sequences(折半遞歸。。暫且這么叫吧)
- 11491 - Erasing and Winning(貪心)
- 1619 - Feel Good(高效算法-利用數據結構優化-優先隊列)
- hdu-4127 Flood-it!(IDA*算法)
- UESTC 1132 醬神賞花 (用數據結構優化DP)
- HDU 2874 Connections between cities(LCA離線算法)
- Codeforces Round #317 A. Lengthening Sticks(組合+容斥)
- HDU 3085 Nightmare Ⅱ(雙向BFS)
- HDU 5592 ZYB&#39;s Premutation(二分+樹狀數組)
- Codeforces Round #320 (Div. 1) C. Weakness and Poorness(三分)
- HDU 5212 Code(容斥)
- HDU 5596 GTW likes gt(multiset)
- FZU 2159 WuYou(貪心)
- HDU 3450 Counting Sequences(DP + 樹狀數組)
- HDU 5493 Queue(二分+樹狀數組)
- HDU 1166 敵兵布陣(線段樹版)
- HDU 1394 Minimum Inversion Number(樹狀數組||線段樹)
- HDU 2795 Billboard(線段樹)
- POJ 2828 Buy Tickets(樹狀數組)
- 《完全版線段樹》- NotOnlySuccess
- POJ 2886 Who Gets the Most Candies?(樹狀數組+二分)
- HDU 1698 Just a Hook(線段樹區間修改)
- POJ 3468 A Simple Problem with Integers(線段樹|區間加減&amp;&amp;區間求和)
- POJ 2528 Mayor&#39;s posters(線段樹區間修改+離散化)
- HDU 5606 tree(并查集)
- POJ 3734 Blocks(矩陣優化+DP)
- POJ 3233 Matrix Power Series(矩陣優化)
- HDU 5607 graph(矩陣優化+概率DP)
- POJ 2777 Count Color(線段樹區間修改+位運算)
- POJ 1436 Horizontally Visible Segments(線段樹區間修改)
- UVA 1513 - Movie collection(樹狀數組)
- UVA 1232 - SKYLINE(線段樹區間更新)
- 11525 - Permutation(二分+樹狀數組)
- 11402 - Ahoy, Pirates!(線段樹區間更新(標記重疊的處理))
- Educational Codeforces Round 6 E. New Year Tree(DFS序+線段樹)