日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

poj 2528 线段树离散化+染色

發布時間:2023/12/4 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 poj 2528 线段树离散化+染色 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接
Mayor’s posters
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 92628 Accepted: 26452
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

由于題目的數據是達到了1e7,簡單的用線段樹大概率會超時,但是n只到1e4,所以這里考慮用離散化來寫。
首先我們先來講一下什么是離散化。
就拿上面的數據舉例:
把出現的下標從小到大排序,就有了:1 2 3 4 6 7 8 10。
所以我們定義其在數組中有 :
a[1] = 1, a[2] = 2, a[3] = 3, a[4] = 4, a[5] = 6, a[6] = 7, a[7] = 8, a[8] = 10.
一下子數據從原來的10,變成了8,所以這里最多只有2e4個數據,對應這道題,離散化是一個完全合理的算法。

一開始不知道線段樹是如何離散化的,當我看完線段樹是如何離散之后,相信滿滿的去寫這道題,發現我還是太年輕了, 這道題目離散化的過程有個坑。
例如:
1
3
1 6
5 6
1 3
這組數據,離散化后a[1] = 1, a[2] = 3, a[3] = 5, a[4] = 6
進行1 6后所有顏色都是1,
進行5 6后1 2 是顏色1,3 4 是顏色2,
進行1 3后1 2 是顏色3,3 4 是顏色2,
最后我們得到答案是 2,
但是仔細想想這個答案對了嗎,顯然是錯的,對于原來的數據3 ~ 5之間還是顏色1,
因此我們的答案應該是3。
所以在離散化的時候我們應該對與坐標間距離大于1的在中間插入一個數。這樣就可以保證顏色不會消失了。

#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #define mid ((l + r) >> 1) #define lson rt << 1, l, mid #define rson rt << 1 | 1, mid + 1, r #define ls rt << 1 #define rs rt << 1 | 1 using namespace std; const int N = 1e4 + 10; int visit[N << 3], pos[N << 3], li[N], ri[N], n, ans, color[N << 4]; void push_down(int rt) {if(color[rt]) {color[ls] = color[rs] = color[rt];color[rt] = 0;} } void update(int rt, int l, int r, int L, int R, int k) {if(l >= L && r <= R) {color[rt] = k;return ;}push_down(rt);if(L <= mid) update(lson, L, R, k);if(R > mid) update(rson, L, R, k); } void query(int rt, int l, int r) {if(color[rt]) {if(!visit[color[rt]]) {visit[color[rt]]++;ans++;}return ;}if(l == r) return ;query(lson);query(rson); } int main() {int m, t;scanf("%d", &t);while(t--) {memset(visit, 0, sizeof visit);memset(color, 0, sizeof color);scanf("%d", &m);n = 0;for(int i = 1; i <= m; i++) {scanf("%d %d", &li[i], &ri[i]);pos[++n] = li[i];pos[++n] = ri[i];}sort(pos + 1, pos + n + 1);int temp = n;n = 1;for(int i = 2; i <= temp; i++) if(pos[i] != pos[i - 1]) pos[++n] = pos[i];//去重。for(int i = n ; i > 1; i--) if(pos[i] - pos[i - 1] > 1) pos[++n] = pos[i - 1] + 1;//插入中間值。sort(pos + 1, pos + n + 1);for(int i = 1; i <= m; i++) {int l = lower_bound(pos + 1, pos + n + 1, li[i]) - pos;int r = lower_bound(pos + 1, pos + n + 1, ri[i]) - pos;update(1, 1, n, l, r, i);}ans = 0;query(1, 1, n);printf("%d\n", ans);}return 0; } 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的poj 2528 线段树离散化+染色的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。