POJ 2528 Mayor's posters(线段树)
?
題目大意
?
貼海報(bào)。每張海報(bào)的高度都是一樣的,唯獨(dú)寬度不一樣。每張海報(bào)只能占用整數(shù)倍的單位線段長(zhǎng)度,貼了 n(n<=10000) 張海報(bào)之后,有幾張能夠看見(jiàn)(有一個(gè)角能看見(jiàn)這張海報(bào)也算被看見(jiàn)了)?海報(bào)的寬度最大可以達(dá)到 10^7
?
做法分析
?
一看就是線段樹(shù)
先不考慮線段的長(zhǎng)度能夠達(dá)到 10^7
肯定是給每張海報(bào)一個(gè)顏色,然后在這張海報(bào)能夠占領(lǐng)的區(qū)間中涂上這個(gè)顏色。每次更新的時(shí)候就直接區(qū)間操作,記得使用懶操作
最后統(tǒng)計(jì)顏色就行了,統(tǒng)計(jì)顏色的時(shí)候用一個(gè)輔助數(shù)組,記錄這個(gè)顏色是否出現(xiàn)過(guò)
最后就是處理線段的長(zhǎng)度了,顯然離散化
但是這里有一個(gè)小問(wèn)題:題目所給的區(qū)間,例如是 [2, 4] ,表示的是 第 2 個(gè)到第 4 個(gè)長(zhǎng)度為 1 的小區(qū)間。我們這里不能直接根據(jù)數(shù)的大小離散化
排序后,假設(shè)當(dāng)前數(shù)是 x,如果 x-1 出現(xiàn)過(guò),那么 x 離散化后應(yīng)該是 x-1 的標(biāo)記 +1,否則應(yīng)該是上一個(gè)的標(biāo)記 +2
?
參考代碼
?
POJ 25281 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 #include <map> 6 #include <vector> 7 8 using namespace std; 9 10 const int N=20006; 11 12 int ans; 13 bool vs[N]; 14 15 struct interval_tree 16 { 17 struct data 18 { 19 int st, en, color; 20 data() {} 21 data(int a, int b, int c) 22 { 23 st=a, en=b, color=c; 24 } 25 }T[N<<3]; 26 27 void build(int id, int L, int R) 28 { 29 T[id]=data(L, R, 0); 30 if(L==R) return; 31 int mid=(L+R)>>1; 32 build(id<<1, L, mid); 33 build(id<<1|1, mid+1, R); 34 } 35 36 inline void pushDown(int id) 37 { 38 T[id<<1].color=T[id<<1|1].color=T[id].color; 39 T[id].color=0; 40 } 41 42 void update(int id, int L, int R, int color) 43 { 44 if(L<=T[id].st && T[id].en<=R) 45 { 46 T[id].color=color; 47 return; 48 } 49 if(T[id].color!=0) pushDown(id); 50 51 int mid=(T[id].st+T[id].en)>>1; 52 if(R<=mid) update(id<<1, L, R, color); 53 else if(L>mid) update(id<<1|1, L, R, color); 54 else update(id<<1, L, R, color), update(id<<1|1, L, R, color); 55 } 56 57 void query(int id) 58 { 59 if(T[id].color!=0 && !vs[T[id].color]) 60 { 61 ans++, vs[T[id].color]=1; 62 return; 63 } 64 if(T[id].st==T[id].en) return; 65 if(T[id].color!=0) pushDown(id); 66 query(id<<1), query(id<<1|1); 67 } 68 }tree; 69 70 vector <int> temp; 71 map <int, int> ihash; 72 73 struct node 74 { 75 int L, R; 76 node() {} 77 node(int a, int b) 78 { 79 L=a, R=b; 80 } 81 }op[N]; 82 int n, t, tot; 83 84 int main() 85 { 86 scanf("%d", &t); 87 for(int ca=1; ca<=t; ca++) 88 { 89 scanf("%d", &n); 90 temp.clear(), ihash.clear(), tot=0; 91 for(int i=0, a, b; i<n; i++) 92 { 93 scanf("%d%d", &a, &b); 94 op[i]=node(a, b); 95 temp.push_back(a), temp.push_back(b); 96 } 97 sort(temp.begin(), temp.end()); 98 for(int i=0; i<2*n; i++) 99 { 100 int u=temp[i]; 101 if(ihash.find(u)==ihash.end()) 102 { 103 if(ihash.find(u-1)==ihash.end()) 104 ihash.insert(make_pair(u, tot+1)), tot+=2; 105 else 106 ihash.insert(make_pair(u, tot)), tot++; 107 } 108 } 109 110 tree.build(1, 1, tot+2); 111 for(int i=0; i<n; i++) 112 tree.update(1, ihash[op[i].L], ihash[op[i].R], i+1); 113 for(int i=1; i<=n; i++) vs[i]=0; 114 ans=0; 115 tree.query(1); 116 printf("%d\n", ans); 117 } 118 return 0; 119 }
?
AC通道
?
POJ 2528 Mayor's posters
?
南洋理工 第 9 題 posters
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/zhj5chengfeng/archive/2013/03/26/2982737.html
總結(jié)
以上是生活随笔為你收集整理的POJ 2528 Mayor's posters(线段树)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 烫头发一般都是多少钱的?
- 下一篇: 个性签名鸡汤